Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

ValidOptionNameValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 24 4
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Indexing\Validation\Constraint;
4
5
use AmaTeam\ElasticSearch\Indexing\Option\Infrastructure\Registry;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
class ValidOptionNameValidator extends ConstraintValidator
10
{
11
    /**
12
     * @inheritDoc
13
     * @param ValidOptionName $constraint
14
     */
15
    public function validate($value, Constraint $constraint)
16
    {
17
        $context = $this->context;
18
        if (!is_string($value)) {
19
            $context
20
                ->buildViolation($constraint->illegalValueMessage)
21
                ->setParameter('{{ type }}', gettype($value))
22
                ->addViolation();
23
            return;
24
        }
25
        $option = Registry::getInstance()->find($value);
26
        if (!$option) {
27
            $context
28
                ->buildViolation($constraint->missingOptionMessage)
29
                ->setParameter('{{ name }}', $value)
30
                ->addViolation();
31
            return;
32
        }
33
        if ($option->getId() !== $value) {
34
            $context
35
                ->buildViolation($constraint->message)
36
                ->setParameter('{{ name }}', $value)
37
                ->setParameter('{{ id }}', $option->getId())
38
                ->addViolation();
39
        }
40
    }
41
}
42