Completed
Push — master ( bf73c4...f46316 )
by Nikola
02:17
created

ExchangeRateValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 41
ccs 0
cts 22
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C validate() 0 25 7
1
<?php
2
3
namespace RunOpenCode\Bundle\ExchangeRate\Validator\Constraints;
4
5
use RunOpenCode\ExchangeRate\Configuration;
6
use RunOpenCode\ExchangeRate\Contract\RateInterface;
7
use RunOpenCode\ExchangeRate\Contract\RatesConfigurationRegistryInterface;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11
class ExchangeRateValidator extends ConstraintValidator
12
{
13
    protected $ratesConfiguration;
14
15
    public function __construct(RatesConfigurationRegistryInterface $ratesConfiguration)
16
    {
17
        $this->ratesConfiguration = $ratesConfiguration;
18
    }
19
20
    /**
21
     * Validate rate.
22
     *
23
     * @param RateInterface $rate
24
     * @param Constraint $constraint
25
     */
26
    public function validate($rate, Constraint $constraint)
27
    {
28
        if ($rate->getCurrencyCode() && $rate->getRateType()) {
29
            /**
30
             * @var Configuration $rateConfiguration
31
             */
32
            foreach ($this->ratesConfiguration as $rateConfiguration) {
33
34
                if (
35
                    $rateConfiguration->getRateType() === $rate->getRateType()
36
                    &&
37
                    $rateConfiguration->getCurrencyCode() === $rate->getCurrencyCode()
38
                    &&
39
                    $rateConfiguration->getSource() === $rate->getSourceName()
40
                ) {
41
                    return;
42
                }
43
            }
44
            /**
45
             * @var ExchangeRate $constraint
46
             */
47
            $this->context->buildViolation($constraint->message)
48
                ->addViolation();
49
        }
50
    }
51
}
52