UniqueRatesValidator::process()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.6
cc 3
nc 3
nop 3
crap 3
1
<?php
2
/*
3
 * This file is part of the Exchange Rate package, an RunOpenCode project.
4
 *
5
 * (c) 2017 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\Contract\ProcessorInterface;
13
use RunOpenCode\ExchangeRate\Contract\RateInterface;
14
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
15
use RunOpenCode\ExchangeRate\Exception\ConfigurationException;
16
17
/**
18
 * Class UniqueRatesValidator
19
 *
20
 * Checks if all rates are unique in collection (e.g. there should not be rates from same source with same currency code,
21
 * same rate type which is valid on same date).
22
 *
23
 * This processor should be added to processing queue as one of the last ones.
24
 *
25
 * @package RunOpenCode\ExchangeRate\Processor
26
 */
27
class UniqueRatesValidator implements ProcessorInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     *
32
     * @throws ConfigurationException
33
     */
34 2
    public function process($baseCurrencyCode, RatesConfigurationRegistryInterface $configurations, array $rates)
35
    {
36 2
        $registry = array();
37
38
        /**
39
         * @var RateInterface $rate
40
         */
41 2
        foreach ($rates as $rate) {
42
43 2
            $key = sprintf('%s_%s_%s_%s', $rate->getCurrencyCode(), $rate->getRateType(), $rate->getSourceName(), $rate->getDate()->format('Y-m-d'));
44
45 2
            if (array_key_exists($key, $registry)) {
46 1
                throw new ConfigurationException(sprintf('Currency code "%s" of rate type "%s" from source "%s" valid on date "%s" is duplicated.', $rate->getCurrencyCode(), $rate->getRateType(), $rate->getSourceName(), $rate->getDate()->format('Y-m-d')));
47
            }
48
49 2
            $registry[$key] = $rate->getSourceName();
50
        }
51
52 1
        return $rates;
53
    }
54
}
55