Part-DB-server/src/Command/Currencies/UpdateExchangeRatesCommand.php

96 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-02-22 18:14:36 +01:00
*
* 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);
namespace App\Command\Currencies;
2023-06-11 14:55:06 +02:00
use Symfony\Component\Console\Attribute\AsCommand;
use App\Entity\PriceInformations\Currency;
2022-12-18 17:28:42 +01:00
use App\Services\Tools\ExchangeRateUpdater;
use Doctrine\ORM\EntityManagerInterface;
2020-01-05 22:49:00 +01:00
use Exchanger\Exception\Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use function count;
use function strlen;
2023-06-11 14:55:06 +02:00
#[AsCommand('partdb:currencies:update-exchange-rates|partdb:update-exchange-rates|app:update-exchange-rates', 'Updates the currency exchange rates.')]
class UpdateExchangeRatesCommand extends Command
{
public function __construct(protected string $base_current, protected EntityManagerInterface $em, protected ExchangeRateUpdater $exchangeRateUpdater)
{
parent::__construct();
}
2020-01-05 15:46:58 +01:00
protected function configure(): void
{
2023-05-28 01:21:05 +02:00
$this->addArgument('iso_code', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The ISO Codes of the currencies that should be updated.');
}
2020-02-01 17:00:03 +01:00
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
//Check for valid base current
2020-01-05 22:49:00 +01:00
if (3 !== strlen($this->base_current)) {
$io->error('Chosen Base current is not valid. Check your settings!');
2023-06-11 14:55:06 +02:00
return Command::FAILURE;
}
$io->note('Update currency exchange rates with base currency: '.$this->base_current);
//Check what currencies we need to update:
$iso_code = $input->getArgument('iso_code');
$repo = $this->em->getRepository(Currency::class);
$candidates = empty($iso_code) ? $repo->findAll() : $repo->findBy(['iso_code' => $iso_code]);
$success_counter = 0;
//Iterate over each candidate and update exchange rate
foreach ($candidates as $currency) {
/** @var Currency $currency */
try {
$this->exchangeRateUpdater->update($currency);
$io->note(sprintf('Set exchange rate of %s to %f', $currency->getIsoCode(), $currency->getExchangeRate()->toFloat()));
$this->em->persist($currency);
++$success_counter;
2020-01-05 22:49:00 +01:00
} catch (Exception $exception) {
$io->warning(sprintf('Error updating %s:', $currency->getIsoCode()));
2020-01-05 22:49:00 +01:00
$io->warning($exception->getMessage());
}
}
//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
2023-06-11 14:55:06 +02:00
return Command::SUCCESS;
}
}