Completed
Push — 8.x-1.x ( 695218...eb0c37 )
by
unknown
23:34 queued 17:45
created

AnnotatedRouteControllerLoader::setRouteConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
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 Drupal\controller_annotations\Configuration\Route as RouteConfiguration;
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 5
    protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
20
    {
21 5
        $this->setController($route, $class, $method);
22
23 5
        foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
24 5
            if ($configuration instanceof RouteConfiguration) {
25 5
                $this->setRouteConfiguration($route, $configuration);
26
            }
27
            elseif ($configuration instanceof Method) {
28 5
                $this->setMethodConfiguration($route, $configuration);
29
            } elseif ($configuration instanceof Security) {
30 5
                $this->setSecurityConfiguration($route, $configuration);
31 5
            } elseif ($configuration instanceof RouteConfiguration && $configuration->getService()) {
32 5
                throw new \LogicException('The service option can only be specified at class level.');
33
            }
34
        }
35 5
    }
36
37
    /**
38
     * @param Route $route
39
     * @param \ReflectionClass $class
40
     * @param \ReflectionMethod $method
41
     */
42 5
    private function setController(Route $route, \ReflectionClass $class, \ReflectionMethod $method)
43
    {
44 5
        $classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
45 5
        if ($classAnnot instanceof RouteConfiguration && $service = $classAnnot->getService()) {
46 5
            $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...
47
        } else {
48 5
            $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...
49
        }
50 5
    }
51
52
    /**
53
     * @param Route $route
54
     * @param RouteConfiguration $routeConfiguration
55
     */
56 5
    private function setRouteConfiguration(Route $route, RouteConfiguration $routeConfiguration)
57
    {
58 5
        if ($routeConfiguration->isAdmin()) {
59 5
            $route->setOption('_admin_route', true);
60
        }
61 5
    }
62
63
    /**
64
     * we need to make sure this is an array instead of a string which is different in Symfony Framework
65
     * otherwise the support for defining an array of methods will not work as expected
66
     *
67
     * @param Route $route
68
     * @param Method $method
69
     */
70 5
    private function setMethodConfiguration(Route $route, Method $method)
71
    {
72 5
        $route->setMethods($method->getMethods());
73 5
    }
74
75
    /**
76
     * @param Route $route
77
     * @param Security $security
78
     */
79 5
    private function setSecurityConfiguration(Route $route, Security $security)
80
    {
81 5
        if ($security->isAccess()) {
82 5
            $route->setRequirement('_access', 'TRUE');
83
        }
84 5
        if ($security->hasPermission()) {
85 5
            $route->setRequirement('_permission', $security->getPermission());
86
        }
87 5
        if ($security->hasRole()) {
88 5
            $route->setRequirement('_role', $security->getRole());
89
        }
90 5
        if ($security->hasEntity()) {
91 5
            $route->setRequirement('_entity_access', $security->getEntity());
92
        }
93 5
        if ($security->hasCsrf()) {
94 5
            $route->setRequirement('_csrf_token', 'TRUE');
95
        }
96 5
        if ($security->hasCustom()) {
97 5
            $route->setRequirement('_custom_access', $security->getCustom());
98
        }
99 5
    }
100
}
101