mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-28 20:50:06 +02:00
Updated UpdateExchangeRatesCommand to use the new service.
This commit is contained in:
parent
af42c3cca0
commit
696d3343a4
2 changed files with 13 additions and 25 deletions
|
@ -33,6 +33,12 @@ services:
|
||||||
resource: '../src/Controller'
|
resource: '../src/Controller'
|
||||||
tags: ['controller.service_arguments']
|
tags: ['controller.service_arguments']
|
||||||
|
|
||||||
|
####################################################################################################################
|
||||||
|
# Alias definitions
|
||||||
|
####################################################################################################################
|
||||||
|
Swap\Swap:
|
||||||
|
alias: 'florianv_swap.swap'
|
||||||
|
|
||||||
####################################################################################################################
|
####################################################################################################################
|
||||||
# Email
|
# Email
|
||||||
####################################################################################################################
|
####################################################################################################################
|
||||||
|
@ -145,7 +151,6 @@ services:
|
||||||
App\Services\ExchangeRateUpdater:
|
App\Services\ExchangeRateUpdater:
|
||||||
arguments:
|
arguments:
|
||||||
$base_currency: '%partdb.default_currency%'
|
$base_currency: '%partdb.default_currency%'
|
||||||
$swap: '@florianv_swap.swap'
|
|
||||||
|
|
||||||
###################################################################################################################
|
###################################################################################################################
|
||||||
# User system
|
# User system
|
||||||
|
|
|
@ -43,17 +43,14 @@ declare(strict_types=1);
|
||||||
namespace App\Command;
|
namespace App\Command;
|
||||||
|
|
||||||
use App\Entity\PriceInformations\Currency;
|
use App\Entity\PriceInformations\Currency;
|
||||||
use Brick\Math\BigDecimal;
|
use App\Services\ExchangeRateUpdater;
|
||||||
use function count;
|
use function count;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Exchanger\Exception\Exception;
|
use Exchanger\Exception\Exception;
|
||||||
use function strlen;
|
use function strlen;
|
||||||
use Swap\Builder;
|
|
||||||
use Swap\Swap;
|
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Input\InputArgument;
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
|
@ -63,13 +60,15 @@ class UpdateExchangeRatesCommand extends Command
|
||||||
|
|
||||||
protected $base_current;
|
protected $base_current;
|
||||||
protected $em;
|
protected $em;
|
||||||
|
protected $exchangeRateUpdater;
|
||||||
|
|
||||||
public function __construct(string $base_current, EntityManagerInterface $entityManager)
|
public function __construct(string $base_current, EntityManagerInterface $entityManager, ExchangeRateUpdater $exchangeRateUpdater)
|
||||||
{
|
{
|
||||||
//$this->swap = $swap;
|
//$this->swap = $swap;
|
||||||
$this->base_current = $base_current;
|
$this->base_current = $base_current;
|
||||||
|
|
||||||
$this->em = $entityManager;
|
$this->em = $entityManager;
|
||||||
|
$this->exchangeRateUpdater = $exchangeRateUpdater;
|
||||||
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
@ -78,13 +77,7 @@ class UpdateExchangeRatesCommand extends Command
|
||||||
{
|
{
|
||||||
$this
|
$this
|
||||||
->setDescription('Updates the currency exchange rates.')
|
->setDescription('Updates the currency exchange rates.')
|
||||||
->addArgument('iso_code', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The ISO Codes of the currencies that should be updated.')
|
->addArgument('iso_code', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The ISO Codes of the currencies that should be updated.');
|
||||||
->addOption('service', null, InputOption::VALUE_REQUIRED,
|
|
||||||
'Which service should be used for requesting the exchange rates (e.g. fixer). See florianv/swap for full list.',
|
|
||||||
'exchange_rates_api')
|
|
||||||
->addOption('api_key', null, InputOption::VALUE_REQUIRED,
|
|
||||||
'The API key to use for the service.',
|
|
||||||
null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
@ -93,25 +86,16 @@ class UpdateExchangeRatesCommand extends Command
|
||||||
|
|
||||||
//Check for valid base current
|
//Check for valid base current
|
||||||
if (3 !== strlen($this->base_current)) {
|
if (3 !== strlen($this->base_current)) {
|
||||||
$io->error('Choosen Base current is not valid. Check your settings!');
|
$io->error('Chosen Base current is not valid. Check your settings!');
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
$io->note('Update currency exchange rates with base currency: '.$this->base_current);
|
$io->note('Update currency exchange rates with base currency: '.$this->base_current);
|
||||||
|
|
||||||
$service = $input->getOption('service');
|
|
||||||
$api_key = $input->getOption('api_key');
|
|
||||||
|
|
||||||
//Construct Swap with the given options
|
|
||||||
$swap = (new Builder())
|
|
||||||
->add($service, ['access_key' => $api_key])
|
|
||||||
->build();
|
|
||||||
|
|
||||||
//Check what currencies we need to update:
|
//Check what currencies we need to update:
|
||||||
$iso_code = $input->getArgument('iso_code');
|
$iso_code = $input->getArgument('iso_code');
|
||||||
$repo = $this->em->getRepository(Currency::class);
|
$repo = $this->em->getRepository(Currency::class);
|
||||||
$candidates = [];
|
|
||||||
|
|
||||||
if (! empty($iso_code)) {
|
if (! empty($iso_code)) {
|
||||||
$candidates = $repo->findBy(['iso_code' => $iso_code]);
|
$candidates = $repo->findBy(['iso_code' => $iso_code]);
|
||||||
|
@ -125,8 +109,7 @@ class UpdateExchangeRatesCommand extends Command
|
||||||
foreach ($candidates as $currency) {
|
foreach ($candidates as $currency) {
|
||||||
/** @var Currency $currency */
|
/** @var Currency $currency */
|
||||||
try {
|
try {
|
||||||
$rate = $swap->latest($currency->getIsoCode().'/'.$this->base_current);
|
$this->exchangeRateUpdater->update($currency);
|
||||||
$currency->setExchangeRate(BigDecimal::of($rate->getValue()));
|
|
||||||
$io->note(sprintf('Set exchange rate of %s to %f', $currency->getIsoCode(), $currency->getExchangeRate()->toFloat()));
|
$io->note(sprintf('Set exchange rate of %s to %f', $currency->getIsoCode(), $currency->getExchangeRate()->toFloat()));
|
||||||
$this->em->persist($currency);
|
$this->em->persist($currency);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue