Passed
Pull Request — master (#64)
by Daniel
37:02
created

ResourceIriValidator::validate()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
nc 5
nop 2
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 12
rs 9.9332
c 1
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 Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
17
use Symfony\Component\Routing\RouterInterface;
18
use Symfony\Component\Validator\Constraint;
19
use Symfony\Component\Validator\ConstraintValidator;
20
21
/**
22
 * @author Daniel West <[email protected]>
23
 */
24
class ResourceIriValidator extends ConstraintValidator
25
{
26
    private RouterInterface $router;
27
28
    public function __construct(RouterInterface $router)
29
    {
30
        $this->router = $router;
31
    }
32
33
    /**
34
     * @param ResourceIri $constraint
35
     */
36
    public function validate($iri, Constraint $constraint): void
37
    {
38
        try {
39
            $parameters = $this->router->match($iri);
40
            if (!isset($parameters['_api_resource_class'])) {
41
                $this->context->buildViolation($constraint->message)
42
                    ->setParameter('{{ value }}', $iri)
43
                    ->addViolation();
44
            }
45
        } catch (RoutingExceptionInterface $e) {
46
            $this->context->buildViolation($constraint->message)
47
                ->setParameter('{{ value }}', $iri)
48
                ->addViolation();
49
        }
50
    }
51
}
52