Skip to content

Commit

Permalink
Merge pull request #15 from mention-me/master
Browse files Browse the repository at this point in the history
Make Symfony 4 Compatible
  • Loading branch information
maZahaca authored May 10, 2022
2 parents f3a65a2 + 46ce123 commit 5ebb5bf
Show file tree
Hide file tree
Showing 8 changed files with 1,847 additions and 1,154 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';

protected CurrencyRateManager $currencyRateManager;
protected CurrencyManager $currencyManager;
protected 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
12 changes: 6 additions & 6 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@

<services>

<service id="redcode.currency.rate.manager" class="RedCode\CurrencyRateBundle\Manager\CurrencyRateManager">
<service id="redcode.currency.rate.manager" class="RedCode\CurrencyRateBundle\Manager\CurrencyRateManager" public="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument>%redcode.currency.rate.class%</argument>
</service>

<service id="redcode.currency.manager" class="RedCode\CurrencyRateBundle\Manager\CurrencyManager">
<service id="redcode.currency.manager" class="RedCode\CurrencyRateBundle\Manager\CurrencyManager" public="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<argument>%redcode.currency.class%</argument>
</service>

<service id="redcode.currency.rate.converter" class="RedCode\Currency\Rate\CurrencyConverter">
<service id="redcode.currency.rate.converter" class="RedCode\Currency\Rate\CurrencyConverter" public="true">
<argument type="service" id="redcode.currency.rate.provider.factory" />
<argument type="service" id="redcode.currency.rate.manager" />
<argument type="service" id="redcode.currency.manager" />
</service>

<service id="redcode.currency.rate.provider.factory" class="RedCode\Currency\Rate\Provider\ProviderFactory" />
<service id="redcode.currency.rate.provider.factory" class="RedCode\Currency\Rate\Provider\ProviderFactory" public="true" />

<service class="RedCode\Currency\Rate\Provider\CbrCurrencyRateProvider">
<service id="red_code.currency.rate.provider.cbr_currency_rate_provider" class="RedCode\Currency\Rate\Provider\CbrCurrencyRateProvider" public="true">
<argument type="service" id="redcode.currency.rate.manager" />
<argument type="service" id="redcode.currency.manager" />
<tag name="redcode.currency.rate.provider" />
</service>

<service class="RedCode\Currency\Rate\Provider\EcbCurrencyRateProvider">
<service id="red_code.currency.rate.provider.ecb_currency_rate_provider" class="RedCode\Currency\Rate\Provider\EcbCurrencyRateProvider" public="true">
<argument type="service" id="redcode.currency.rate.manager" />
<argument type="service" id="redcode.currency.manager" />
<tag name="redcode.currency.rate.provider" />
Expand Down
19 changes: 0 additions & 19 deletions Tests/bootstrap.php

This file was deleted.

13 changes: 0 additions & 13 deletions Tests/runTests.sh

This file was deleted.

24 changes: 12 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=5.4.0",
"doctrine/orm": ">=2.3.2",
"doctrine/dbal": ">=2.3.2",
"redcode/currency-rate": "0.2.*"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"fabpot/php-cs-fixer": "^1.10",
"satooshi/php-coveralls": "^0.6.1"
"php": "^7.4",
"doctrine/orm": "^2",
"doctrine/dbal": "^2",
"redcode/currency-rate": "0.2.*",
"symfony/symfony": "^4.4"
},
"require-dev": {
"fabpot/php-cs-fixer": "*"
},
"autoload": {
"psr-0": { "RedCode\\CurrencyRateBundle": "" }
},
"target-dir": "RedCode/CurrencyRateBundle"
"psr-4": {
"RedCode\\CurrencyRateBundle\\": ""
}
}
}
Loading

0 comments on commit 5ebb5bf

Please sign in to comment.