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

ValidParameterNameValidator::validate()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 20

Duplication

Lines 7
Ratio 29.17 %

Importance

Changes 0
Metric Value
dl 7
loc 24
c 0
b 0
f 0
rs 8.6845
cc 4
eloc 20
nc 4
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping\Validation\Constraint;
6
7
use AmaTeam\ElasticSearch\Mapping\Type\Parameter\Infrastructure\Registry;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11
class ValidParameterNameValidator extends ConstraintValidator
12
{
13
    /**
14
     * @inheritDoc
15
     * @param ValidParameterName $constraint
16
     */
17
    public function validate($value, Constraint $constraint)
18
    {
19
        $context = $this->context;
20
        if (!is_string($value)) {
21
            $context
22
                ->buildViolation($constraint->illegalValueMessage)
23
                ->addViolation();
24
            return;
25
        }
26
        $parameter = Registry::getInstance()->find($value);
27
        if (!$parameter) {
28
            $context
29
                ->buildViolation($constraint->missingParameterMessage)
30
                ->setParameter('{{ name }}', $value)
31
                ->addViolation();
32
            return;
33
        }
34 View Code Duplication
        if ($value !== $parameter->getId()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
            $this
36
                ->context
37
                ->buildViolation($constraint->message)
38
                ->setParameter('{{ id }}', $parameter->getId())
39
                ->setParameter('{{ name }}', $value)
40
                ->addViolation();
41
        }
42
    }
43
}
44