Completed
Pull Request — master (#3)
by Alexis
06:28 queued 03:56
created

RouteExistsValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C validate() 0 27 7
1
<?php
2
3
namespace Alpixel\Bundle\MenuBundle\Validator\Constraints;
4
5
use Symfony\Bundle\FrameworkBundle\Routing\Router;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
class RouteExistsValidator extends ConstraintValidator
10
{
11
    private $router;
12
13
    public function __construct(Router $router)
14
    {
15
        $this->router = $router;
16
    }
17
18
    public function validate($value, Constraint $constraint)
19
    {
20
        $match = false;
21
        if (strpos($value, '/') === 0) {
22
            $routeCollection = $this->router->getRouteCollection()->all();
23
            foreach ($routeCollection as $name => $route) {
24
                if ($match = $route->getPath() === $value) {
25
                    break;
26
                }
27
            }
28
        } else {
29
            $handle = curl_init($value);
30
            curl_setopt($handle,  CURLOPT_RETURNTRANSFER, TRUE);
31
            $response = curl_exec($handle);
0 ignored issues
show
Unused Code introduced by
$response is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
            $httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
33
34
            if ($httpCode >= 200 && $httpCode < 300) {
35
                $match = true;
36
            }
37
        }
38
39
        if ($match === false) {
40
            $this->context->buildViolation($constraint->message)
41
                ->setParameter('%string%', $value)
42
                ->addViolation();
43
        }
44
    }
45
}
46