Passed
Push — master ( cddc8e...2b1927 )
by Daniel
07:07
created

ComponentPositionValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Validator\Constraints;
15
16
use ApiPlatform\Core\Api\IriConverterInterface;
17
use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Silverback\ApiComponents...aints\ComponentPosition. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use Silverback\ApiComponentsBundle\Validator\Constraints\ComponentPosition as ComponentPositionConstraint;
19
use Symfony\Component\Validator\Constraint;
20
use Symfony\Component\Validator\ConstraintValidator;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class ComponentPositionValidator extends ConstraintValidator
26
{
27
    private IriConverterInterface $iriConverter;
28
29
    public function __construct(IriConverterInterface $iriConverter)
30
    {
31
        $this->iriConverter = $iriConverter;
32
    }
33
34
    /**
35
     * @param ComponentPosition           $componentPosition
36
     * @param ComponentPositionConstraint $constraint
37
     */
38
    public function validate($componentPosition, Constraint $constraint): void
39
    {
40
        $collection = $componentPosition->componentCollection;
41
        if (!$collection) {
42
            return;
43
        }
44
        $component = $componentPosition->component;
45
        if (!$component) {
46
            return;
47
        }
48
49
        $iri = $this->iriConverter->getIriFromResourceClass(\get_class($component));
50
51
        if ($allowedComponents = $collection->allowedComponents) {
52
            if (!$allowedComponents->contains($iri)) {
53
                $this->context->buildViolation($constraint->message)
54
                    ->setParameter('{{ iri }}', $iri)
55
                    ->setParameter('{{ reference }}', $collection->reference)
0 ignored issues
show
Bug introduced by
It seems like $collection->reference can also be of type null; however, parameter $value of Symfony\Component\Valida...terface::setParameter() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                    ->setParameter('{{ reference }}', /** @scrutinizer ignore-type */ $collection->reference)
Loading history...
56
                    ->setParameter('{{ allowed }}', implode(',', $allowedComponents->toArray()))
57
                    ->addViolation();
58
            }
59
60
            return;
61
        }
62
63
        if ($componentPosition->component->isPositionRestricted()) {
64
            $this->context->buildViolation($constraint->restrictedMessage)
65
                ->setParameter('{{ iri }}', $iri)
66
                ->setParameter('{{ reference }}', $collection->reference)
67
                ->addViolation();
68
        }
69
    }
70
}
71