Completed
Push — master ( b40b69...17fbce )
by Nikola
01:54
created

BaseCurrencyValidator::process()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 7
nc 4
nop 3
1
<?php
2
3
namespace RunOpenCode\ExchangeRate\Processor;
4
5
use Psr\Log\LoggerAwareTrait;
6
use RunOpenCode\AssetsInjection\Exception\ConfigurationException;
7
use RunOpenCode\ExchangeRate\Contract\ProcessorInterface;
8
use RunOpenCode\ExchangeRate\Contract\RateInterface;
9
use RunOpenCode\ExchangeRate\Utils\CurrencyCode;
10
11
class BaseCurrencyValidator implements ProcessorInterface
12
{
13
    use  LoggerAwareTrait;
14
15
    /**
16
     * {@inheritdoc}
17
     */
18
    public function process($baseCurrencyCode, array $rateConfigurations, array $rates)
19
    {
20
        if (!CurrencyCode::exists($baseCurrencyCode)) {
21
            throw new \RuntimeException(sprintf('Unknown base currency code "%s".', $baseCurrencyCode));
22
        }
23
24
        /**
25
         * @var RateInterface $rate
26
         */
27
        foreach ($rates as $rate) {
28
29
            if ($baseCurrencyCode !== $rate->getBaseCurrencyCode()) {
30
                throw new ConfigurationException(sprintf('Conversion of compound rate "%s" from source "%s" is not calculated.', $rate->getCurrencyCode(), $rate->getSourceName()));
31
            }
32
        }
33
34
        return $rates;
35
    }
36
}