BaseCurrencyValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 5
dl 0
loc 36
ccs 16
cts 16
cp 1
rs 10
c 1
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 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