Completed
Push — 8.x-1.x ( 3dbfad...adcc4b )
by
unknown
49:51
created

AnnotatedRouteControllerLoader::setController()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 3
eloc 6
nc 2
nop 3
crap 3
1
<?php
2
3
namespace Drupal\controller_annotations\Routing;
4
5
use Drupal\controller_annotations\Configuration\Method;
6
use Drupal\controller_annotations\Configuration\Security;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route as FrameworkExtraBundleRoute;
8
use Sensio\Bundle\FrameworkExtraBundle\Routing\AnnotatedRouteControllerLoader as BaseAnnotatedRouteControllerLoader;
9
use Symfony\Component\Routing\Route;
10
11
class AnnotatedRouteControllerLoader extends BaseAnnotatedRouteControllerLoader
12
{
13
    /**
14
     * @param Route $route
15
     * @param \ReflectionClass $class
16
     * @param \ReflectionMethod $method
17
     * @param mixed $annot
18
     */
19 6
    protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
20
    {
21 6
        $this->setController($route, $class, $method);
22
23 6
        foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
24 6
            if ($configuration instanceof Method) {
25 6
                $this->setMethodConfiguration($route, $configuration);
26 6
            } elseif ($configuration instanceof Security) {
27 6
                $this->setSecurityConfiguration($route, $configuration);
28 6
            } elseif ($configuration instanceof FrameworkExtraBundleRoute && $configuration->getService()) {
29 6
                throw new \LogicException('The service option can only be specified at class level.');
30
            }
31
        }
32 6
    }
33
34
    /**
35
     * @param Route $route
36
     * @param \ReflectionClass $class
37
     * @param \ReflectionMethod $method
38
     */
39 6
    private function setController(Route $route, \ReflectionClass $class, \ReflectionMethod $method)
40
    {
41 6
        $classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
42 6
        if ($classAnnot instanceof FrameworkExtraBundleRoute && $service = $classAnnot->getService()) {
43 6
            $route->setDefault('_controller', $service.':'.$method->getName());
0 ignored issues
show
Bug introduced by
Consider using $method->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
44
        } else {
45 6
            $route->setDefault('_controller', $class->getName().'::'.$method->getName());
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
46
        }
47 6
    }
48
49
    /**
50
     * we need to make sure this is an array instead of a string which is different in Symfony Framework
51
     * otherwise the support for defining an array of methods will not work as expected
52
     *
53
     * @param Route $route
54
     * @param Method $method
55
     */
56 6
    private function setMethodConfiguration(Route $route, Method $method)
57
    {
58 6
        $route->setMethods($method->getMethods());
59 6
    }
60
61
    /**
62
     * @param Route $route
63
     * @param Security $security
64
     */
65 6
    private function setSecurityConfiguration(Route $route, Security $security)
66
    {
67 6
        if ($security->isAccess()) {
68 6
            $route->setRequirement('_access', 'TRUE');
69
        }
70 6
        if ($security->hasPermission()) {
71
            $route->setRequirement('_permission', $security->getPermission());
72
        }
73 6
        if ($security->hasRole()) {
74
            $route->setRequirement('_role', $security->getRole());
75
        }
76 6
        if ($security->hasAuth()) {
77
            $route->setRequirement('_auth', $security->getAuth());
78
        }
79 6
        if ($security->hasCsrf()) {
80
            $route->setRequirement('_csrf_token', 'TRUE');
81
        }
82 6
        if ($security->hasCustom()) {
83
            $route->setRequirement('_custom_access', $security->getCustom());
84
        }
85 6
    }
86
}
87