1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Command; |
4
|
|
|
|
5
|
|
|
use App\Entity\PriceInformations\Currency; |
6
|
|
|
use App\Form\AdminPages\CurrencyAdminForm; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use PHPUnit\Runner\Exception; |
|
|
|
|
9
|
|
|
use Swap\Builder; |
10
|
|
|
use Swap\Swap; |
11
|
|
|
use Symfony\Component\Console\Command\Command; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
17
|
|
|
|
18
|
|
|
class UpdateExchangeRatesCommand extends Command |
19
|
|
|
{ |
20
|
|
|
protected static $defaultName = 'app:update-exchange-rates'; |
21
|
|
|
|
22
|
|
|
protected $base_current; |
23
|
|
|
protected $em; |
24
|
|
|
|
25
|
|
|
public function __construct(string $base_current, EntityManagerInterface $entityManager) |
26
|
|
|
{ |
27
|
|
|
//$this->swap = $swap; |
28
|
|
|
$this->base_current = $base_current; |
29
|
|
|
|
30
|
|
|
$this->em = $entityManager; |
31
|
|
|
|
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
|
|
->setDescription('Updates the currency exchange rates.') |
39
|
|
|
->addArgument('iso_code', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The ISO Codes of the currencies that should be updated.') |
40
|
|
|
->addOption('service', null, InputOption::VALUE_REQUIRED, |
41
|
|
|
'Which service should be used for requesting the exchange rates (e.g. fixer). See florianv/swap for full list.', |
42
|
|
|
'exchange_rates_api') |
43
|
|
|
->addOption('api_key', null, InputOption::VALUE_REQUIRED, |
44
|
|
|
'The API key to use for the service.', |
45
|
|
|
null); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
49
|
|
|
{ |
50
|
|
|
$io = new SymfonyStyle($input, $output); |
51
|
|
|
|
52
|
|
|
//Check for valid base current |
53
|
|
|
if (strlen($this->base_current) !== 3) { |
54
|
|
|
$io->error("Choosen Base current is not valid. Check your settings!"); |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$io->note('Update currency exchange rates with base currency: ' . $this->base_current); |
59
|
|
|
|
60
|
|
|
$service = $input->getOption('service'); |
61
|
|
|
$api_key = $input->getOption('api_key'); |
62
|
|
|
|
63
|
|
|
//Construct Swap with the given options |
64
|
|
|
$swap = (new Builder()) |
65
|
|
|
->add($service, ['access_key' => $api_key]) |
66
|
|
|
->build(); |
67
|
|
|
|
68
|
|
|
//Check what currencies we need to update: |
69
|
|
|
$iso_code = $input->getArgument('iso_code'); |
70
|
|
|
$repo = $this->em->getRepository(Currency::class); |
71
|
|
|
$candidates = array(); |
72
|
|
|
|
73
|
|
|
if (!empty($iso_code)) { |
74
|
|
|
$candidates = $repo->findBy(['iso_code' => $iso_code]); |
75
|
|
|
} else { |
76
|
|
|
$candidates = $repo->findAll(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$success_counter = 0; |
80
|
|
|
|
81
|
|
|
//Iterate over each candidate and update exchange rate |
82
|
|
|
foreach ($candidates as $currency) { |
83
|
|
|
try { |
84
|
|
|
$rate = $swap->latest($currency->getIsoCode() . '/' . $this->base_current); |
85
|
|
|
$currency->setExchangeRate($rate->getValue()); |
86
|
|
|
$io->note(sprintf('Set exchange rate of %s to %f', $currency->getIsoCode(), $currency->getExchangeRate())); |
87
|
|
|
$this->em->persist($currency); |
88
|
|
|
|
89
|
|
|
$success_counter++; |
90
|
|
|
} catch (\Exchanger\Exception\Exception $ex) { |
91
|
|
|
$io->warning(sprintf('Error updating %s:', $currency->getIsoCode())); |
92
|
|
|
$io->warning($ex->getMessage()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
//Save to database |
98
|
|
|
$this->em->flush(); |
99
|
|
|
|
100
|
|
|
$io->success(sprintf('%d (of %d) currency exchange rates were updated.', $success_counter, count($candidates))); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths