Completed
Push — 8.x-1.x ( 630823...f12348 )
by
unknown
30:40
created

Security::modifyRouteClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
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 6
    public function hasPermission()
47
    {
48 6
        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 6
    public function hasRole()
74
    {
75 6
        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 6
    public function isAccess()
101
    {
102 6
        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 6
    public function hasEntity()
120
    {
121 6
        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 6
    public function hasCustom()
147
    {
148 6
        return !empty($this->custom);
149
    }
150
151
    /**
152
     * @return string
153
     */
154 6
    public function getCustom()
155
    {
156 6
        return $this->custom;
157
    }
158
159
    /**
160
     * @param string $custom
161
     * @return Security
162
     */
163 6
    public function setCustom($custom)
164
    {
165 6
        $this->custom = $custom;
166
167 6
        return $this;
168
    }
169
170
    /**
171
     * @return bool
172
     */
173 6
    public function hasCsrf()
174
    {
175 6
        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);
207 5
    }
208
209
    /**
210
     * @param RoutingRoute $route
211
     * @param \ReflectionClass $class
212
     * @param \ReflectionMethod $method
213
     */
214 6
    public function modifyRouteMethod(RoutingRoute $route, \ReflectionClass $class, \ReflectionMethod $method)
215
    {
216 6
        $this->modifyRoute($route);
217 6
    }
218
219
    /**
220
     * @param RoutingRoute $route
221
     */
222 6
    protected function modifyRoute(RoutingRoute $route)
223
    {
224 6
        if ($this->isAccess()) {
225 6
            $route->setRequirement('_access', 'TRUE');
226
        }
227 6
        if ($this->hasPermission()) {
228 6
            $route->setRequirement('_permission', $this->getPermission());
229
        }
230 6
        if ($this->hasRole()) {
231 6
            $route->setRequirement('_role', $this->getRole());
232
        }
233 6
        if ($this->hasEntity()) {
234 6
            $route->setRequirement('_entity_access', $this->getEntity());
235
        }
236 6
        if ($this->hasCsrf()) {
237 6
            $route->setRequirement('_csrf_token', 'TRUE');
238
        }
239 6
        if ($this->hasCustom()) {
240 6
            $route->setRequirement('_custom_access', $this->getCustom());
241
        }
242 6
    }
243
}
244