Completed
Push — master ( 13d085...a38278 )
by Nikola
08:03
created

BaseCurrencyValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 36
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B validate() 0 23 4
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 Symfony\Component\Validator\Constraint;
14
use Symfony\Component\Validator\ConstraintValidator;
15
16
/**
17
 * Class BaseCurrencyValidator
18
 *
19
 * @package RunOpenCode\Bundle\ExchangeRate\Validator\Constraints
20
 */
21
class BaseCurrencyValidator extends ConstraintValidator
22
{
23
    /**
24
     * @var string
25
     */
26
    private $baseCurrencyCode;
27
28 11
    public function __construct($baseCurrencyCode)
29
    {
30 11
        $this->baseCurrencyCode = $baseCurrencyCode;
31 11
    }
32
33 11
    public function validate($value, Constraint $constraint)
34
    {
35 11
        if (!$constraint instanceof BaseCurrency) {
36 1
            throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', BaseCurrency::class, get_class($constraint)));
37
        }
38
39 10
        if (null === $value) {
40 1
            return;
41
        }
42
43 9
        if ($this->baseCurrencyCode !== $value) {
44
45
            /**
46
             * @var BaseCurrency $constraint
47
             */
48 1
            $this->context
49 1
                ->buildViolation($constraint->message)
50 1
                ->setParameter('{{ base_currency_code }}', $this->baseCurrencyCode)
51 1
                ->setParameter('{{ value }}', $value)
52 1
                ->setTranslationDomain('runopencode_exchange_rate')
53 1
                ->addViolation();
54
        }
55 9
    }
56
}
57