Passed
Push — develop ( 5d2ce0...8a9894 )
by Daniel
04:55
created

ComponentLocationValidator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
C validate() 0 24 7
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Validator\Constraints;
4
5
use Silverback\ApiComponentBundle\Entity\ValidComponentInterface;
6
use Silverback\ApiComponentBundle\Validator\ClassNameValidator;
7
use Silverback\ApiComponentBundle\Entity\Content\ComponentLocation as ComponentLocationEntity;
8
use Symfony\Component\Validator\Constraint;
9
use Symfony\Component\Validator\ConstraintValidator;
10
11
class ComponentLocationValidator extends ConstraintValidator
12
{
13
    /**
14
     * @param ComponentLocationEntity $entity
15
     * @param Constraint $constraint
16
     * @throws \ReflectionException
17
     */
18 3
    public function validate($entity, Constraint $constraint): void
19
    {
20 3
        if (!$entity instanceof ComponentLocationEntity) {
0 ignored issues
show
introduced by
The condition ! $entity instanceof Sil...ntent\ComponentLocation can never be true.
Loading history...
21 1
            throw new \InvalidArgumentException(
22 1
                sprintf('The ComponentLocationValidator should only be used with %s', ComponentLocationEntity::class)
23
            );
24
        }
25 2
        $content = $entity->getContent();
26 2
        if ($content instanceof ValidComponentInterface) {
27 2
            $validComponents = $content->getValidComponents();
28 2
            if ($validComponents->count()) {
29 2
                $componentIsValid = false;
30 2
                $component = $entity->getComponent();
31 2
                foreach ($validComponents as $validComponent) {
32 2
                    if (ClassNameValidator::isClassSame($validComponent, $component)) {
33 1
                        $componentIsValid = true;
34 2
                        break;
35
                    }
36
                }
37 2
                if (!$componentIsValid) {
38 1
                    $this->context->buildViolation($constraint->message)
39 1
                        ->atPath('component')
40 1
                        ->setParameter('{{ string }}', implode(', ', $validComponents->toArray()))
41 1
                        ->addViolation();
42
                }
43
            }
44
        }
45 2
    }
46
}
47