Skip to content

Commit

Permalink
Switch to Command and use DI
Browse files Browse the repository at this point in the history
  • Loading branch information
edhgoose committed May 7, 2022
1 parent b10dc02 commit 310a789
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
25 changes: 17 additions & 8 deletions Command/CreateBaseCurrenciesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,32 @@

namespace RedCode\CurrencyRateBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use RedCode\CurrencyRateBundle\Manager\CurrencyManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class CreateBaseCurrenciesCommand extends ContainerAwareCommand
class CreateBaseCurrenciesCommand extends Command
{
private static $baseCurrencies = [
private static array $baseCurrencies = [
'AUD', 'AZN', 'GBP', 'AMD', 'BYR', 'BGN', 'BRL', 'HUF', 'DKK', 'USD', 'EUR', 'INR', 'KZT', 'CAD', 'KGS', 'CNY',
'MDL', 'NOK', 'PLN', 'RON', 'XDR', 'SGD', 'TJS', 'TRY', 'TMT', 'UZS', 'UAH', 'CZK', 'SEK', 'CHF', 'ZAR', 'KRW',
'JPY', 'RUB', 'HRK', 'HKD', 'IDR', 'ILS', 'MXN', 'MYR', 'NZD', 'PHP', 'THB',
];

protected static $defaultName = 'redcode:create:base:currencies';

private CurrencyManager $currencyManager;

public function __construct(CurrencyManager $currencyManager)
{
parent::__construct();

$this->currencyManager = $currencyManager;
}

/**
* {@inheritdoc}
*
Expand All @@ -24,7 +36,6 @@ class CreateBaseCurrenciesCommand extends ContainerAwareCommand
protected function configure()
{
$this
->setName('redcode:create:base:currencies')
->setDescription('Create base currencies');
}

Expand All @@ -37,13 +48,11 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$currencyManager = $this->getContainer()->get('redcode.currency.manager');

$totalCount = 0;

foreach (self::$baseCurrencies as $code) {
if (null === $currencyManager->getCurrency($code)) {
$currencyManager->addCurrency($code);
if (null === $this->currencyManager->getCurrency($code)) {
$this->currencyManager->addCurrency($code);
++$totalCount;
}
}
Expand Down
51 changes: 29 additions & 22 deletions Command/LoadCurrencyRatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use RedCode\Currency\Rate\Provider\ProviderFactory;
use RedCode\CurrencyRateBundle\Manager\CurrencyManager;
use RedCode\CurrencyRateBundle\Manager\CurrencyRateManager;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -16,19 +16,35 @@
/**
* @author maZahaca
*/
class LoadCurrencyRatesCommand extends ContainerAwareCommand
class LoadCurrencyRatesCommand extends Command
{
protected static $defaultName = 'redcode:currency:rate:load';

private CurrencyRateManager $currencyRateManager;
private CurrencyManager $currencyManager;
private ProviderFactory $providerFactory;

public function __construct(
CurrencyRateManager $currencyRateManager,
CurrencyManager $currencyManager,
ProviderFactory $providerFactory
) {
parent::__construct();

$this->currencyRateManager = $currencyRateManager;
$this->currencyManager = $currencyManager;
$this->providerFactory = $providerFactory;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('redcode:currency:rate:load')
->addArgument('providerName', InputArgument::OPTIONAL, 'Provider name for rates (cbr)', null)
->addArgument('date', InputArgument::OPTIONAL, 'Date for loading rates in format YYYY-MM-DD (default date now)', date('Y-m-d'))
->setDescription('Load currency rates from cbr.ru')
;
->addArgument('providerName', InputArgument::OPTIONAL, 'Provider name for rates (cbr)', null)
->addArgument('date', InputArgument::OPTIONAL, 'Date for loading rates in format YYYY-MM-DD (default date now)', date('Y-m-d'))
->setDescription('Load currency rates from cbr.ru');
}

/**
Expand All @@ -48,32 +64,23 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

/**
* @param \DateTime|null $date
* @param \DateTime|null $date
* @param ICurrencyRateProvider|string|null $provider
*
* @return ICurrencyRate[]
* @throws ProviderNotFoundException
*
* @return ICurrencyRate[]
*/
public function updateRates($date = null, $provider = null)
{
/** @var $currencyRateManager CurrencyRateManager */
$currencyRateManager = $this->getContainer()->get('redcode.currency.rate.manager');

/** @var $currencyManager CurrencyManager */
$currencyManager = $this->getContainer()->get('redcode.currency.manager');

/** @var $providerFactory ProviderFactory */
$providerFactory = $this->getContainer()->get('redcode.currency.rate.provider.factory');

if (!$date || !($date instanceof \DateTime)) {
$date = new \DateTime();
}

if ($provider && !($provider instanceof ICurrencyRateProvider)) {
if (is_string($provider)) {
$providerName = $provider;
$provider = $providerFactory->get($provider);
$provider = $this->providerFactory->get($provider);
if (!($provider instanceof ICurrencyRateProvider)) {
throw new ProviderNotFoundException("CurrencyRateProvider for name {$providerName} not found");
}
Expand All @@ -82,13 +89,13 @@ public function updateRates($date = null, $provider = null)
}
}

$providers = $provider === null ? $providerFactory->getAll() : [$provider];
$providers = $provider === null ? $this->providerFactory->getAll() : [$provider];

$resultRates = [];
foreach ($providers as $provider) {
/* @var $provider ICurrencyRateProvider */
$rates = $provider->getRates($currencyManager->getAll(), $date);
$currencyRateManager->saveRates($rates);
$rates = $provider->getRates($this->currencyManager->getAll(), $date);
$this->currencyRateManager->saveRates($rates);
$resultRates = array_merge($resultRates, $rates);
}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],
"minimum-stability": "stable",
"require": {
"php": "^7.3",
"php": "^7.4",
"doctrine/orm": "^2",
"doctrine/dbal": "^2",
"redcode/currency-rate": "0.2.*",
Expand Down

0 comments on commit 310a789

Please sign in to comment.