Completed
Push — master ( e3081b...b3412c )
by Nikola
03:22
created

UniqueRatesValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 6
loc 33
ccs 0
cts 14
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 6 23 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\AssetsInjection\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
18
/**
19
 * Class UniqueRatesValidator
20
 *
21
 * Checks if all rates are unique in collection (e.g. there should not be rates from same source with same currency code,
22
 * same rate type which is valid on same date).
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 UniqueRatesValidator implements ProcessorInterface
29
{
30
    use LoggerAwareTrait;
31
32
    /**
33
     * {@inheritdoc}
34
     *
35
     * @throws ConfigurationException
36
     */
37
    public function process($baseCurrencyCode, RatesConfigurationRegistryInterface $configurations, array $rates)
38
    {
39
        $registry = array();
40
41
        /**
42
         * @var RateInterface $rate
43
         */
44
        foreach ($rates as $rate) {
45
46
            $key = sprintf('%s_%s_%s_%s', $rate->getCurrencyCode(), $rate->getRateType(), $rate->getSourceName(), $rate->getDate()->format('Y-m-d'));
47
48 View Code Duplication
            if (array_key_exists($key, $registry)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
49
                $message = 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'));
50
51
                $this->getLogger()->critical($message);
52
                throw new ConfigurationException($message);
53
            }
54
55
            $registry[$key] = $rate->getSourceName();
56
        }
57
58
        return $rates;
59
    }
60
}
61