2019-08-13 13:04:31 +02:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-11-01 13:40:30 +01:00
|
|
|
/**
|
2019-11-09 00:47:20 +01:00
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
2019-11-01 13:40:30 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2019-08-13 13:04:31 +02:00
|
|
|
|
|
|
|
namespace App\Command;
|
|
|
|
|
|
|
|
use App\Entity\PriceInformations\Currency;
|
2020-05-20 20:57:16 +02:00
|
|
|
use Brick\Math\BigDecimal;
|
2020-01-05 22:49:00 +01:00
|
|
|
use function count;
|
2019-08-13 13:04:31 +02:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-01-05 22:49:00 +01:00
|
|
|
use Exchanger\Exception\Exception;
|
|
|
|
use function strlen;
|
2019-08-13 13:04:31 +02:00
|
|
|
use Swap\Builder;
|
|
|
|
use Swap\Swap;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
|
|
|
|
|
class UpdateExchangeRatesCommand extends Command
|
|
|
|
{
|
|
|
|
protected static $defaultName = 'app:update-exchange-rates';
|
|
|
|
|
|
|
|
protected $base_current;
|
|
|
|
protected $em;
|
|
|
|
|
|
|
|
public function __construct(string $base_current, EntityManagerInterface $entityManager)
|
|
|
|
{
|
|
|
|
//$this->swap = $swap;
|
|
|
|
$this->base_current = $base_current;
|
|
|
|
|
|
|
|
$this->em = $entityManager;
|
|
|
|
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
protected function configure(): void
|
2019-08-13 13:04:31 +02:00
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setDescription('Updates the currency exchange rates.')
|
|
|
|
->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);
|
|
|
|
}
|
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
2019-08-13 13:04:31 +02:00
|
|
|
{
|
|
|
|
$io = new SymfonyStyle($input, $output);
|
|
|
|
|
|
|
|
//Check for valid base current
|
2020-01-05 22:49:00 +01:00
|
|
|
if (3 !== strlen($this->base_current)) {
|
2019-11-09 00:47:20 +01:00
|
|
|
$io->error('Choosen Base current is not valid. Check your settings!');
|
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
return 1;
|
2019-08-13 13:04:31 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
$io->note('Update currency exchange rates with base currency: '.$this->base_current);
|
2019-08-13 13:04:31 +02:00
|
|
|
|
|
|
|
$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:
|
|
|
|
$iso_code = $input->getArgument('iso_code');
|
|
|
|
$repo = $this->em->getRepository(Currency::class);
|
2019-11-09 00:47:20 +01:00
|
|
|
$candidates = [];
|
2019-08-13 13:04:31 +02:00
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
if (! empty($iso_code)) {
|
2019-08-13 13:04:31 +02:00
|
|
|
$candidates = $repo->findBy(['iso_code' => $iso_code]);
|
|
|
|
} else {
|
|
|
|
$candidates = $repo->findAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
$success_counter = 0;
|
|
|
|
|
|
|
|
//Iterate over each candidate and update exchange rate
|
|
|
|
foreach ($candidates as $currency) {
|
2020-05-20 20:57:16 +02:00
|
|
|
/** @var Currency $currency */
|
2019-08-13 13:04:31 +02:00
|
|
|
try {
|
2019-11-09 00:47:20 +01:00
|
|
|
$rate = $swap->latest($currency->getIsoCode().'/'.$this->base_current);
|
2020-05-20 20:57:16 +02:00
|
|
|
$currency->setExchangeRate(BigDecimal::of($rate->getValue()));
|
|
|
|
$io->note(sprintf('Set exchange rate of %s to %f', $currency->getIsoCode(), $currency->getExchangeRate()->toFloat()));
|
2019-08-13 13:04:31 +02:00
|
|
|
$this->em->persist($currency);
|
|
|
|
|
2019-11-09 00:47:20 +01:00
|
|
|
++$success_counter;
|
2020-01-05 22:49:00 +01:00
|
|
|
} catch (Exception $exception) {
|
2019-08-13 13:04:31 +02:00
|
|
|
$io->warning(sprintf('Error updating %s:', $currency->getIsoCode()));
|
2020-01-05 22:49:00 +01:00
|
|
|
$io->warning($exception->getMessage());
|
2019-08-13 13:04:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save to database
|
|
|
|
$this->em->flush();
|
|
|
|
|
2020-01-05 22:49:00 +01:00
|
|
|
$io->success(sprintf('%d (of %d) currency exchange rates were updated.', $success_counter, count($candidates)));
|
2020-03-15 13:56:31 +01:00
|
|
|
|
2020-02-01 17:00:03 +01:00
|
|
|
return 0;
|
2019-08-13 13:04:31 +02:00
|
|
|
}
|
|
|
|
}
|