Completed
Push — 8.x-1.x ( f12348...609e6e )
by
unknown
28:44
created

Security::setCustomSecurity()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
225
    {
226 7
        if ($this->isAccess()) {
227 6
            $route->setRequirement('_access', 'TRUE');
228
        }
229 7
        if ($this->hasPermission()) {
230 6
            $route->setRequirement('_permission', $this->getPermission());
231
        }
232 7
        if ($this->hasRole()) {
233 6
            $route->setRequirement('_role', $this->getRole());
234
        }
235 7
        if ($this->hasEntity()) {
236 6
            $route->setRequirement('_entity_access', $this->getEntity());
237
        }
238 7
        if ($this->hasCsrf()) {
239 6
            $route->setRequirement('_csrf_token', 'TRUE');
240
        }
241
242 7
        $this->setCustomSecurity($route, $class);
243 7
    }
244
245
    /**
246
     * @param RoutingRoute $route
247
     * @param \ReflectionClass $class
248
     */
249 7
    protected function setCustomSecurity(RoutingRoute $route, \ReflectionClass $class)
250
    {
251 7
        if ($this->hasCustom()) {
252 7
            if (strpos($this->getCustom(), '::') === false && $class->hasMethod($this->getCustom())) {
253 6
                $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...
254
            }
255 7
            $route->setRequirement('_custom_access', $this->getCustom());
256
        }
257 7
    }
258
}
259