NoExistingRouteValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 34
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A validate() 0 15 4
1
<?php
2
3
namespace Beelab\SimplePageBundle\Validator\Constraints;
4
5
use Symfony\Component\Routing\RouterInterface;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
/**
10
 * Validate that a path is not used by any existing route.
11
 */
12
class NoExistingRouteValidator extends ConstraintValidator
13
{
14
    /**
15
     * @var RouterInterface
16
     */
17
    private $router;
18
19
    /**
20
     * @var bool
21
     */
22
    private $showRoute;
23
24
    public function __construct(RouterInterface $router, bool $showRoute = true)
25
    {
26
        $this->router = $router;
27
        $this->showRoute = $showRoute;
28
    }
29
30
    public function validate($value, Constraint $constraint): void
31
    {
32
        $routes = $this->router->getRouteCollection();
33
        /* @var \Symfony\Component\Routing\Route $route */
34
        foreach ($routes as $name => $route) {
35
            if ($route->getPath() === '/'.$value) {
36
                if ($this->showRoute) {
37
                    $this->context->addViolation($constraint->message, ['{{ route }}' => $name]);
38
                } else {
39
                    $this->context->addViolation($constraint->messageWithoutRoute);
40
                }
41
                break;
42
            }
43
        }
44
    }
45
}
46