Completed
Push — 8.x-1.x ( 33bdc1...948162 )
by
unknown
24:44
created

AnnotatedRouteControllerLoader::setSecurityConfiguration()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 12
cts 12
cp 1
rs 8.8571
cc 6
eloc 11
nc 32
nop 2
crap 6
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 3
    protected function configureRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
20
    {
21 3
        $this->setController($route, $class, $method);
22
23 3
        foreach ($this->reader->getMethodAnnotations($method) as $configuration) {
24 3
            if ($configuration instanceof Method) {
25 3
                $this->setMethodConfiguration($route, $configuration);
26
            } elseif ($configuration instanceof Security) {
27 3
                $this->setSecurityConfiguration($route, $configuration);
28 3
            } elseif ($configuration instanceof FrameworkExtraBundleRoute && $configuration->getService()) {
29 3
                throw new \LogicException('The service option can only be specified at class level.');
30
            }
31
        }
32 3
    }
33
34
    /**
35
     * @param Route $route
36
     * @param \ReflectionClass $class
37
     * @param \ReflectionMethod $method
38
     */
39 3
    private function setController(Route $route, \ReflectionClass $class, \ReflectionMethod $method)
40
    {
41 3
        $classAnnot = $this->reader->getClassAnnotation($class, $this->routeAnnotationClass);
42 3
        if ($classAnnot instanceof FrameworkExtraBundleRoute && $service = $classAnnot->getService()) {
43 3
            $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 3
            $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 3
    }
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 3
    private function setMethodConfiguration(Route $route, Method $method)
57
    {
58 3
        $route->setMethods($method->getMethods());
59 3
    }
60
61
    /**
62
     * @param Route $route
63
     * @param Security $security
64
     */
65 3
    private function setSecurityConfiguration(Route $route, Security $security)
66
    {
67 3
        if ($security->isAccess()) {
68 3
            $route->setRequirement('_access', 'TRUE');
69
        }
70 3
        if ($security->hasPermission()) {
71 3
            $route->setRequirement('_permission', $security->getPermission());
72
        }
73 3
        if ($security->hasRole()) {
74 3
            $route->setRequirement('_role', $security->getRole());
75
        }
76 3
        if ($security->hasCsrf()) {
77 3
            $route->setRequirement('_csrf_token', 'TRUE');
78
        }
79 3
        if ($security->hasCustom()) {
80 3
            $route->setRequirement('_custom_access', $security->getCustom());
81
        }
82 3
    }
83
}
84