BaseCurrencyValidator::validate()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 23
ccs 13
cts 13
cp 1
rs 8.7972
c 1
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
crap 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 12
    public function __construct($baseCurrencyCode)
29
    {
30 12
        $this->baseCurrencyCode = $baseCurrencyCode;
31 12
    }
32
33 12
    public function validate($value, Constraint $constraint)
34
    {
35 12
        if (!$constraint instanceof BaseCurrency) {
36 1
            throw new InvalidArgumentException(sprintf('Expected instance of "%s", got "%s".', BaseCurrency::class, get_class($constraint)));
37
        }
38
39 11
        if (null === $value) {
40 2
            return;
41
        }
42
43 10
        if ($this->baseCurrencyCode !== $value) {
44
45
            /**
46
             * @var BaseCurrency $constraint
47
             */
48 2
            $this->context
49 2
                ->buildViolation($constraint->message)
50 2
                ->setParameter('{{ base_currency_code }}', $this->baseCurrencyCode)
51 2
                ->setParameter('{{ value }}', $value)
52 2
                ->setTranslationDomain('runopencode_exchange_rate')
53 2
                ->addViolation();
54
        }
55 10
    }
56
}
57