|
1 | 1 | <?php |
| 2 | + |
2 | 3 | declare(strict_types=1); |
3 | 4 | // SPDX-FileCopyrightText: Chen Asraf <contact@casraf.dev> |
4 | 5 | // SPDX-License-Identifier: AGPL-3.0-or-later |
|
7 | 8 |
|
8 | 9 | use Exception; |
9 | 10 |
|
10 | | -use OCA\AutoCurrency\Service\CurrencyNotFound; |
11 | 11 | use OCA\AutoCurrency\Db\CospendProjectMapper; |
12 | | -use OCP\AppFramework\Db\DoesNotExistException; |
13 | | -use OCP\AppFramework\Db\MultipleObjectsReturnedException; |
14 | | - |
15 | 12 | use OCA\AutoCurrency\Db\Currency; |
16 | 13 | use OCA\AutoCurrency\Db\CurrencyMapper; |
17 | | -use OCP\ILogger; |
| 14 | + |
| 15 | +use OCP\AppFramework\Db\DoesNotExistException; |
| 16 | +use OCP\AppFramework\Db\MultipleObjectsReturnedException; |
| 17 | +use OCP\IConfig; |
| 18 | +use Psr\Log\LoggerInterface; |
18 | 19 |
|
19 | 20 | class FetchCurrenciesService { |
20 | | - private static $EXCHANGE_URL = 'https://api.exchangerate.host/latest?base={base}'; |
21 | | - private CurrencyMapper $currencyMapper; |
22 | | - private CospendProjectMapper $projectMapper; |
23 | | - private ILogger $logger; |
24 | | - |
25 | | - public function __construct(CurrencyMapper $currencyMapper, CospendProjectMapper $projectMapper, ILogger $logger) { |
26 | | - $this->currencyMapper = $currencyMapper; |
27 | | - $this->projectMapper = $projectMapper; |
28 | | - $this->logger = $logger; |
29 | | - } |
30 | | - |
31 | | - public function doCron(): void { |
32 | | - $projects = $this->projectMapper->findAll(); |
33 | | - $currencyMap = []; |
34 | | - |
35 | | - foreach ($projects as $project) { |
36 | | - $base = $this->getCurrencyName($project->getCurrencyname()); |
37 | | - |
38 | | - if (isset($currencyMap[$base])) { |
39 | | - $json = $currencyMap[$base]; |
40 | | - } else { |
41 | | - // request currency exchange rates from the API |
42 | | - $this->logger->info('Fetching exchange rates for base currency ' . $base); |
43 | | - $fp = fopen(str_replace('{base}', $base, FetchCurrenciesService::$EXCHANGE_URL), 'r'); |
44 | | - $data = stream_get_contents($fp); |
45 | | - fclose($fp); |
46 | | - $json = json_decode($data, true); |
47 | | - $this->logger->info('Fetched exchange rates for base currency: ' . json_encode($json)); |
48 | | - if ($json['success'] == false) { |
49 | | - $this->logger->error(new \Error('Failed to fetch exchange rates for base currency ' . $base)); |
50 | | - continue; |
51 | | - } |
52 | | - $currencyMap[$base] = $json; |
53 | | - } |
54 | | - |
55 | | - $currencies = $this->findAll($project->id); |
56 | | - |
57 | | - foreach ($currencies as $currency) { |
58 | | - $cur = $this->getCurrencyName($currency->getName()); |
59 | | - $newRate = floatval(number_format(1 / $json['rates'][$cur], 2)); |
60 | | - $currency->setExchangeRate($newRate); |
61 | | - $this->logger->info('Setting exchange rate for currency ' . $cur . ' to ' . $newRate); |
62 | | - $this->currencyMapper->update($currency); |
63 | | - } |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - private function getCurrencyName(string $name): string { |
68 | | - // find 3-letter currency code for the base currency |
69 | | - preg_match('/([A-Z]{3})/', $name, $matches); |
70 | | - |
71 | | - $this->logger->info('Matches: ' . json_encode($matches)); |
72 | | - |
73 | | - if (count($matches) === 2) { |
74 | | - $name = $matches[1]; |
75 | | - } |
76 | | - |
77 | | - return $name; |
78 | | - } |
79 | | - |
80 | | - /** |
81 | | - * @return list<Currency> |
82 | | - */ |
83 | | - public function findAll(string $projectId): array { |
84 | | - return $this->currencyMapper->findAll($projectId); |
85 | | - } |
86 | | - |
87 | | - /** |
88 | | - * @return never |
89 | | - */ |
90 | | - private function handleException(Exception $e) { |
91 | | - if ($e instanceof DoesNotExistException || |
92 | | - $e instanceof MultipleObjectsReturnedException) { |
93 | | - throw new CurrencyNotFound($e->getMessage()); |
94 | | - } else { |
95 | | - throw $e; |
96 | | - } |
97 | | - } |
| 21 | + private static $EXCHANGE_URL = 'https://api.exchangerate.host/latest?base={base}'; |
| 22 | + private IConfig $config; |
| 23 | + private CurrencyMapper $currencyMapper; |
| 24 | + private CospendProjectMapper $projectMapper; |
| 25 | + private LoggerInterface $logger; |
| 26 | + |
| 27 | + public function __construct( |
| 28 | + IConfig $config, |
| 29 | + CurrencyMapper $currencyMapper, |
| 30 | + CospendProjectMapper $projectMapper, |
| 31 | + LoggerInterface $logger, |
| 32 | + ) { |
| 33 | + $this->config = $config; |
| 34 | + $this->currencyMapper = $currencyMapper; |
| 35 | + $this->projectMapper = $projectMapper; |
| 36 | + $this->logger = $logger; |
| 37 | + } |
| 38 | + |
| 39 | + public function doCron(): void { |
| 40 | + $this->logger->info('Starting cron job to fetch currencies'); |
| 41 | + $projects = $this->projectMapper->findAll(); |
| 42 | + $currencyMap = []; |
| 43 | + |
| 44 | + foreach ($projects as $project) { |
| 45 | + $base = $this->getCurrencyName($project->getCurrencyname()); |
| 46 | + |
| 47 | + if (isset($currencyMap[$base])) { |
| 48 | + $json = $currencyMap[$base]; |
| 49 | + } else { |
| 50 | + // request currency exchange rates from the API |
| 51 | + $this->logger->info('Fetching exchange rates for base currency ' . $base); |
| 52 | + $fp = fopen(str_replace('{base}', $base, FetchCurrenciesService::$EXCHANGE_URL), 'r'); |
| 53 | + $data = stream_get_contents($fp); |
| 54 | + fclose($fp); |
| 55 | + $json = json_decode($data, true); |
| 56 | + $this->logger->info('Fetched exchange rates for base currency: ' . json_encode($json)); |
| 57 | + if ($json['success'] == false) { |
| 58 | + $this->logger->error(new \Error('Failed to fetch exchange rates for base currency ' . $base)); |
| 59 | + continue; |
| 60 | + } |
| 61 | + $currencyMap[$base] = $json; |
| 62 | + } |
| 63 | + |
| 64 | + $currencies = $this->findAll($project->id); |
| 65 | + |
| 66 | + foreach ($currencies as $currency) { |
| 67 | + $cur = $this->getCurrencyName($currency->getName()); |
| 68 | + $newRate = floatval(number_format(1 / $json['rates'][$cur], 2)); |
| 69 | + $currency->setExchangeRate($newRate); |
| 70 | + $this->logger->info('Setting exchange rate for currency ' . $cur . ' to ' . $newRate); |
| 71 | + $this->currencyMapper->update($currency); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + $lastUpdate = date('c'); |
| 76 | + $this->config->setAppValue('autocurrency', 'last_update', $lastUpdate); |
| 77 | + } |
| 78 | + |
| 79 | + private function getCurrencyName(string $name): string { |
| 80 | + // find 3-letter currency code for the base currency |
| 81 | + preg_match('/([A-Z]{3})/', $name, $matches); |
| 82 | + |
| 83 | + $this->logger->info('Matches: ' . json_encode($matches)); |
| 84 | + |
| 85 | + if (count($matches) === 2) { |
| 86 | + $name = $matches[1]; |
| 87 | + } |
| 88 | + |
| 89 | + return $name; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return list<Currency> |
| 94 | + */ |
| 95 | + public function findAll(string $projectId): array { |
| 96 | + return $this->currencyMapper->findAll($projectId); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return never |
| 101 | + */ |
| 102 | + private function handleException(Exception $e) { |
| 103 | + if ($e instanceof DoesNotExistException || |
| 104 | + $e instanceof MultipleObjectsReturnedException) { |
| 105 | + throw new CurrencyNotFound($e->getMessage()); |
| 106 | + } else { |
| 107 | + throw $e; |
| 108 | + } |
| 109 | + } |
98 | 110 | } |
0 commit comments