RouteExistsValidator::validate()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 9.2
cc 4
eloc 11
nc 5
nop 2
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