RouteExistsValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A validate() 0 19 4
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\Validator\Constraints;
4
5
use Alpixel\Bundle\MenuBundle\Utils\URLChecker;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
class RouteExistsValidator extends ConstraintValidator
10
{
11
    private $checker;
12
13
    public function __construct(URLChecker $checker)
14
    {
15
        $this->checker = $checker;
16
    }
17
18
    public function validate($value, Constraint $constraint)
19
    {
20
        // If the first occurrence of $value is the # character it's an anchor any check is done
21
        if (strpos($value, '#') === 0) {
22
            return;
23
        }
24
25
        $match = true;
26
        $code = $this->checker->check($value);
27
        if ($code === URLChecker::URL_NOT_FOUND) {
28
            $match = false;
29
        }
30
31
        if ($match === false) {
32
            $this->context->buildViolation($constraint->message)
33
                ->setParameter('%string%', $value)
34
                ->addViolation();
35
        }
36
    }
37
}
38