Security::modifyRoute()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 8.9777
c 0
b 0
f 0
cc 6
nc 32
nop 2
crap 6
1
<?php
2
3
namespace Drupal\controller_annotations\Configuration;
4
5
use Symfony\Component\Routing\Route as RoutingRoute;
6
7
/**
8
 * @Annotation
9
 */
10
class Security extends ConfigurationAnnotation implements RouteModifierMethodInterface, RouteModifierClassInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $permission;
16
17
    /**
18
     * @var string
19
     */
20
    protected $role;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $access;
26
27
    /**
28
     * @var string
29
     */
30
    protected $entity;
31
32
    /**
33
     * @var bool
34
     */
35
    protected $csrf;
36
37
    /**
38
     * @var string
39
     */
40
    protected $custom;
41
42
    /**
43
     * @return bool
44
     */
45 9
    public function hasPermission()
46
    {
47 9
        return !empty($this->permission);
48
    }
49
50
    /**
51
     * @return string
52
     */
53 8
    public function getPermission()
54
    {
55 8
        return $this->permission;
56
    }
57
58
    /**
59
     * @param string $permission
60
     * @return Security
61
     */
62 8
    public function setPermission($permission)
63
    {
64 8
        $this->permission = $permission;
65
66 8
        return $this;
67
    }
68
69
    /**
70
     * @return bool
71
     */
72 9
    public function hasRole()
73
    {
74 9
        return !empty($this->role);
75
    }
76
77
    /**
78
     * @return string
79
     */
80 8
    public function getRole()
81
    {
82 8
        return $this->role;
83
    }
84
85
    /**
86
     * @param string $role
87
     * @return Security
88
     */
89 8
    public function setRole($role)
90
    {
91 8
        $this->role = $role;
92
93 8
        return $this;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99 9
    public function isAccess()
100
    {
101 9
        return $this->access;
102
    }
103
104
    /**
105
     * @param bool $access
106
     * @return Security
107
     */
108 8
    public function setAccess($access)
109
    {
110 8
        $this->access = $access;
111
112 8
        return $this;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118 9
    public function hasEntity()
119
    {
120 9
        return !empty($this->entity);
121
    }
122
123
    /**
124
     * @return string
125
     */
126 8
    public function getEntity()
127
    {
128 8
        return $this->entity;
129
    }
130
131
    /**
132
     * @param string $entity
133
     * @return Security
134
     */
135 8
    public function setEntity($entity)
136
    {
137 8
        $this->entity = $entity;
138
139 8
        return $this;
140
    }
141
142
    /**
143
     * @return bool
144
     */
145 9
    public function hasCustom()
146
    {
147 9
        return !empty($this->custom);
148
    }
149
150
    /**
151
     * @return string
152
     */
153 9
    public function getCustom()
154
    {
155 9
        return $this->custom;
156
    }
157
158
    /**
159
     * @param string $custom
160
     * @return Security
161
     */
162 9
    public function setCustom($custom)
163
    {
164 9
        $this->custom = $custom;
165
166 9
        return $this;
167
    }
168
169
    /**
170
     * @return bool
171
     */
172 9
    public function hasCsrf()
173
    {
174 9
        return !empty($this->csrf);
175
    }
176
177
    /**
178
     * @param bool $csrf
179
     * @return Security
180
     */
181 8
    public function setCsrf($csrf)
182
    {
183 8
        $this->csrf = $csrf;
184
185 8
        return $this;
186
    }
187
188 7
    public function getAliasName()
189
    {
190 7
        return 'security';
191
    }
192
193 7
    public function allowArray()
194
    {
195 7
        return false;
196
    }
197
198
    /**
199
     * @param RoutingRoute $route
200
     * @param \ReflectionClass $class
201
     * @param \ReflectionMethod $method
202
     */
203 7
    public function modifyRouteClass(RoutingRoute $route, \ReflectionClass $class, \ReflectionMethod $method)
204
    {
205 7
        $this->modifyRoute($route, $class);
206 7
    }
207
208
    /**
209
     * @param RoutingRoute $route
210
     * @param \ReflectionClass $class
211
     * @param \ReflectionMethod $method
212
     */
213 9
    public function modifyRouteMethod(RoutingRoute $route, \ReflectionClass $class, \ReflectionMethod $method)
214
    {
215 9
        $this->modifyRoute($route, $class);
216 9
    }
217
218
    /**
219
     * @param RoutingRoute $route
220
     * @param \ReflectionClass $class
221
     */
222 9
    protected function modifyRoute(RoutingRoute $route, \ReflectionClass $class)
223
    {
224 9
        if ($this->isAccess()) {
225 8
            $route->setRequirement('_access', 'TRUE');
226
        }
227 9
        if ($this->hasPermission()) {
228 8
            $route->setRequirement('_permission', $this->getPermission());
229
        }
230 9
        if ($this->hasRole()) {
231 8
            $route->setRequirement('_role', $this->getRole());
232
        }
233 9
        if ($this->hasEntity()) {
234 8
            $route->setRequirement('_entity_access', $this->getEntity());
235
        }
236 9
        if ($this->hasCsrf()) {
237 8
            $route->setRequirement('_csrf_token', 'TRUE');
238
        }
239
240 9
        $this->setCustomSecurity($route, $class);
241 9
    }
242
243
    /**
244
     * @param RoutingRoute $route
245
     * @param \ReflectionClass $class
246
     */
247 9 View Code Duplication
    protected function setCustomSecurity(RoutingRoute $route, \ReflectionClass $class)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
248
    {
249 9
        if ($this->hasCustom()) {
250 9
            if (strpos($this->getCustom(), '::') === false && $class->hasMethod($this->getCustom())) {
251 8
                $this->setCustom(sprintf('%s::%s', $class->getName(), $this->getCustom()));
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
252
            }
253 9
            $route->setRequirement('_custom_access', $this->getCustom());
254
        }
255 9
    }
256
}
257