ExchangeRateValidator::validate()   D
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 21
cts 21
cp 1
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 23
nc 7
nop 2
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/*
3
 * This file is part of the Exchange Rate Bundle, 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\Bundle\ExchangeRate\Validator\Constraints;
11
12
use RunOpenCode\Bundle\ExchangeRate\Exception\InvalidArgumentException;
13
use RunOpenCode\ExchangeRate\Configuration;
14
use RunOpenCode\ExchangeRate\Contract\RateInterface;
15
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
16
use Symfony\Component\Validator\Constraint;
17
use Symfony\Component\Validator\ConstraintValidator;
18
19
/**
20
 * Class ExchangeRateValidator
21
 *
22
 * Exchange rate validator.
23
 *
24
 * @package RunOpenCode\Bundle\ExchangeRate\Validator\Constraints
25
 */
26
class ExchangeRateValidator extends ConstraintValidator
27
{
28
    /**
29
     * @var RatesConfigurationRegistryInterface
30
     */
31
    protected $ratesConfiguration;
32
33 13
    public function __construct(RatesConfigurationRegistryInterface $ratesConfiguration)
34
    {
35 13
        $this->ratesConfiguration = $ratesConfiguration;
36 13
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 13
    public function validate($rate, Constraint $constraint)
42
    {
43 13
        if (!$constraint instanceof ExchangeRate) {
44 1
            throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', ExchangeRate::class, get_class($constraint)));
45
        }
46
47 12
        if (null === $rate) {
48 1
            return;
49
        }
50
51 11
        if (!$rate instanceof RateInterface) {
52 1
            throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', RateInterface::class, get_class($rate)));
53
        }
54
55
56 10
        if ($rate->getCurrencyCode() && $rate->getRateType()) {
57
            /**
58
             * @var Configuration $rateConfiguration
59
             */
60 10
            foreach ($this->ratesConfiguration as $rateConfiguration) {
61
62
                if (
63 10
                    $rateConfiguration->getRateType() === $rate->getRateType()
64
                    &&
65 10
                    $rateConfiguration->getCurrencyCode() === $rate->getCurrencyCode()
66
                    &&
67 10
                    $rateConfiguration->getSourceName() === $rate->getSourceName()
68
                ) {
69 10
                    return;
70
                }
71
            }
72
            /**
73
             * @var ExchangeRate $constraint
74
             */
75 2
            $this->context
76 2
                ->buildViolation($constraint->message)
77 2
                ->setParameter('{{ rate_type }}', $rate->getRateType())
78 2
                ->setParameter('{{ currency_code }}', $rate->getCurrencyCode())
79 2
                ->setParameter('{{ source_name }}', $rate->getSourceName())
80 2
                ->setTranslationDomain('runopencode_exchange_rate')
81 2
                ->addViolation();
82
        }
83 2
    }
84
}
85