1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2016 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace RunOpenCode\ExchangeRate\Processor; |
11
|
|
|
|
12
|
|
|
use RunOpenCode\ExchangeRate\Exception\ConfigurationException; |
13
|
|
|
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface; |
14
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
15
|
|
|
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface; |
16
|
|
|
use RunOpenCode\ExchangeRate\Log\LoggerAwareTrait; |
17
|
|
|
use RunOpenCode\ExchangeRate\Utils\CurrencyCodeUtil; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class BaseCurrencyValidator |
21
|
|
|
* |
22
|
|
|
* Processor which checks weather all rates have same base currency code. |
23
|
|
|
* |
24
|
|
|
* This processor should be added to processing queue as one of the last ones. |
25
|
|
|
* |
26
|
|
|
* @package RunOpenCode\ExchangeRate\Processor |
27
|
|
|
*/ |
28
|
|
|
class BaseCurrencyValidator implements ProcessorInterface |
29
|
|
|
{ |
30
|
|
|
use LoggerAwareTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
4 |
|
public function process($baseCurrencyCode, RatesConfigurationRegistryInterface $configurations, array $rates) |
36
|
|
|
{ |
37
|
4 |
|
$baseCurrencyCode = CurrencyCodeUtil::clean($baseCurrencyCode); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RateInterface $rate |
41
|
|
|
*/ |
42
|
4 |
|
foreach ($rates as $rate) { |
43
|
|
|
|
44
|
4 |
|
if ($baseCurrencyCode !== $rate->getBaseCurrencyCode()) { |
|
|
|
|
45
|
2 |
|
$message = sprintf('Invalid base currency code "%s" of rate "%s" from source "%s" is not calculated.', $rate->getBaseCurrencyCode(), $rate->getCurrencyCode(), $rate->getSourceName()); |
46
|
|
|
|
47
|
2 |
|
$this->getLogger()->critical($message); |
48
|
2 |
|
throw new ConfigurationException($message); |
49
|
|
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
2 |
|
return $rates; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.