ComponentPositionValidator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 24
c 3
b 0
f 0
dl 0
loc 44
ccs 0
cts 24
cp 0
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B validate() 0 31 6
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\Metadata\GetCollection;
17
use ApiPlatform\Metadata\IriConverterInterface;
18
use ApiPlatform\Metadata\UrlGeneratorInterface;
19
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...
20
use Silverback\ApiComponentsBundle\Validator\Constraints\ComponentPosition as ComponentPositionConstraint;
21
use Symfony\Component\Validator\Constraint;
22
use Symfony\Component\Validator\ConstraintValidator;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class ComponentPositionValidator extends ConstraintValidator
28
{
29
    private IriConverterInterface $iriConverter;
30
31
    public function __construct(IriConverterInterface $iriConverter)
32
    {
33
        $this->iriConverter = $iriConverter;
34
    }
35
36
    /**
37
     * @param ComponentPosition           $componentPosition
38
     * @param ComponentPositionConstraint $constraint
39
     */
40
    public function validate($componentPosition, Constraint $constraint): void
41
    {
42
        $collection = $componentPosition->componentGroup;
43
        if (!$collection) {
44
            return;
45
        }
46
        $component = $componentPosition->component;
47
        if (!$component) {
48
            return;
49
        }
50
51
        $resourceClass = $component::class;
52
        $iri = $this->iriConverter->getIriFromResource($resourceClass, UrlGeneratorInterface::ABS_PATH, (new GetCollection())->withClass($resourceClass));
53
54
        if ($allowedComponents = $collection->allowedComponents) {
55
            if (!\in_array($iri, $allowedComponents, true)) {
56
                $this->context->buildViolation($constraint->message)
57
                    ->setParameter('{{ iri }}', $iri)
0 ignored issues
show
Bug introduced by
It seems like $iri 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

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