Completed
Push — master ( af642d...2d11c7 )
by Lars
02:35
created

Rule::isAllowed()   C

Complexity

Conditions 11
Paths 46

Size

Total Lines 60
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 36
CRAP Score 11

Importance

Changes 15
Bugs 1 Features 4
Metric Value
c 15
b 1
f 4
dl 0
loc 60
ccs 36
cts 36
cp 1
rs 6.2926
cc 11
eloc 33
nc 46
nop 3
crap 11

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace SimpleAcl;
3
4
use RecursiveIteratorIterator;
5
use SimpleAcl\Resource;
6
use SimpleAcl\Resource\ResourceAggregateInterface;
7
use SimpleAcl\Role;
8
use SimpleAcl\Role\RoleAggregateInterface;
9
10
/**
11
 * Used to connects Role and Resources together.
12
 *
13
 * @package SimpleAcl
14
 */
15
class Rule
16
{
17
  /**
18
   * Holds rule id.
19
   *
20
   * @var mixed
21
   */
22
  public $id;
23
24
  /**
25
   * Rule priority affect the order the rule is applied.
26
   *
27
   * @var int
28
   */
29
  protected $priority = 0;
30
31
  /**
32
   * Hold name of rule.
33
   *
34
   * @var string
35
   */
36
  protected $name;
37
38
  /**
39
   * Action used when determining is rule allow access to its Resource and Role.
40
   *
41
   * @var mixed
42
   */
43
  protected $action = false;
44
45
  /**
46
   * @var Role
47
   */
48
  protected $role;
49
50
  /**
51
   * @var Resource
52
   */
53
  protected $resource;
54
55
  /**
56
   * @var RoleAggregateInterface
57
   */
58
  protected $roleAggregate;
59
60
  /**
61
   * @var ResourceAggregateInterface
62
   */
63
  protected $resourceAggregate;
64
65
  /**
66
   * Create Rule with given name.
67
   *
68
   * @param $name
69
   */
70 49
  public function __construct($name)
71
  {
72 49
    $this->setId();
73 49
    $this->setName($name);
74 49
  }
75
76
  /**
77
   * Set aggregate objects.
78
   *
79
   * @param $roleAggregate
80
   * @param $resourceAggregate
81
   */
82 26
  public function resetAggregate($roleAggregate, $resourceAggregate)
83
  {
84 26
    if ($roleAggregate instanceof RoleAggregateInterface) {
85 4
      $this->setRoleAggregate($roleAggregate);
86 4
    } else {
87 23
      $this->roleAggregate = null;
88
    }
89
90 26
    if ($resourceAggregate instanceof ResourceAggregateInterface) {
91 3
      $this->setResourceAggregate($resourceAggregate);
92 3
    } else {
93 24
      $this->resourceAggregate = null;
94
    }
95 26
  }
96
97
  /**
98
   * @return ResourceAggregateInterface
99
   */
100 2
  public function getResourceAggregate()
101
  {
102 2
    return $this->resourceAggregate;
103
  }
104
105
  /**
106
   * @param ResourceAggregateInterface $resourceAggregate
107
   */
108 4
  public function setResourceAggregate(ResourceAggregateInterface $resourceAggregate)
109
  {
110 4
    $this->resourceAggregate = $resourceAggregate;
111 4
  }
112
113
  /**
114
   * @return RoleAggregateInterface
115
   */
116 2
  public function getRoleAggregate()
117
  {
118 2
    return $this->roleAggregate;
119
  }
120
121
  /**
122
   * @param RoleAggregateInterface $roleAggregate
123
   */
124 5
  public function setRoleAggregate(RoleAggregateInterface $roleAggregate)
125
  {
126 5
    $this->roleAggregate = $roleAggregate;
127 5
  }
128
129
  /**
130
   * @return mixed
131
   */
132 4
  public function getId()
133
  {
134 4
    return $this->id;
135
  }
136
137
  /**
138
   * @param mixed $id
139
   */
140 49
  public function setId($id = null)
141
  {
142 49
    if (null === $id) {
143 49
      $id = $this->generateId();
144 49
    }
145
146 49
    $this->id = $id;
147 49
  }
148
149
  /**
150
   * @param RuleResult|null $ruleResult
151
   *
152
   * @return bool|null
153
   */
154 32
  public function getAction(RuleResult $ruleResult = null)
155
  {
156 32
    $actionResult = $this->action;
157
158
    if (
159 32
        !is_callable($actionResult)
160 32
        ||
161 4
        null === $ruleResult
162 32
    ) {
163 29
      if (null !== $actionResult) {
164 29
        return (bool)$actionResult;
165
      } else {
166 3
        return null;
167
      }
168
    }
169
170 4
    $actionResult = call_user_func($this->action, $ruleResult);
171
172 4
    if (null !== $actionResult) {
173 4
      return (bool)$actionResult;
174
    } else {
175
      return null;
176
    }
177
  }
178
179
  /**
180
   * @param mixed $action
181
   */
182 40
  public function setAction($action)
183
  {
184 40
    $this->action = $action;
185 40
  }
186
187
  /**
188
   * Check owing Role & Resource and match its with $roleName & $resourceName;
189
   * if match was found depending on action allow or deny access to $resourceName for $roleName.
190
   *
191
   * @param        $needRuleName
192
   * @param string $needRoleName
193
   * @param string $needResourceName
194
   *
195
   * @return RuleResult|null null is returned if there is no matched Role & Resource in this rule.
196
   *                         RuleResult otherwise.
197
   */
198 27
  public function isAllowed($needRuleName, $needRoleName, $needResourceName)
199
  {
200 27
    static $roleCache = array();
201 27
    static $resourceCache = array();
202
203 27
    if ($this->isRuleMatched($needRuleName)) {
204
205 27
      if (null !== $this->role) {
206
207 27
        $roleNameTmp = $this->role->getName();
208
209 27
        if (!isset($roleCache[$roleNameTmp])) {
210 6
          $roles = iterator_to_array($this->role);
211
212 6
          $roleCache[$roleNameTmp] = $roles;
213 6
        } else {
214 27
          $roles = $roleCache[$roleNameTmp];
215
        }
216
217 27
      } else {
218 5
        $roles = array(null);
219
      }
220
221 27
      if (null !== $this->resource) {
222
223 27
        $resourceNameTmp = $this->getResource()->getName();
224
225 27
        if (!isset($resourceCache[$resourceNameTmp])) {
226 7
          $resources = iterator_to_array($this->getResource());
227
228 7
          $resourceCache[$resourceNameTmp] = $resources;
229 7
        } else {
230 27
          $resources = $resourceCache[$resourceNameTmp];
231
        }
232
233 27
      } else {
234 5
        $resources = array(null);
235
      }
236
237 27
      $roleDepth = 0;
238 27
      $resourceDepth = 0;
239
      
240 27
      foreach ($roles as $role) {
241 27
        $roleDepth = $role ? $roleDepth++ : 0;
242
243 27
        foreach ($resources as $resource) {
244 27
          $resourceDepth = $resource ? $resourceDepth++ : 0;
245
246 27
          $depth = $roleDepth + $resourceDepth;
247 27
          $result = $this->match($role, $resource, $needRoleName, $needResourceName, -$depth);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $result is correct as $this->match($role, $res...dResourceName, -$depth) (which targets SimpleAcl\Rule::match()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
248
249 27
          if ($result) {
250 26
            return $result;
251
          }
252 18
        }
253 18
      }
254 18
    }
255
256 18
    return null;
257
  }
258
259
  /**
260
   * Check if rule can be used.
261
   *
262
   * @param $neeRuleName
263
   *
264
   * @return bool
265
   */
266 25
  protected function isRuleMatched($neeRuleName)
267
  {
268 25
    return $this->getName() === $neeRuleName;
269
  }
270
271
  /**
272
   * @return string
273
   */
274 29
  public function getName()
275
  {
276 29
    return $this->name;
277
  }
278
279
  /**
280
   * @param string $name
281
   */
282 49
  public function setName($name)
283
  {
284 49
    $this->name = $name;
285 49
  }
286
287
  /**
288
   * @return Role
289
   */
290 7
  public function getRole()
291
  {
292 7
    return $this->role;
293
  }
294
295
  /**
296
   * @param Role|null $role
297
   */
298 37
  public function setRole(Role $role = null)
299
  {
300 37
    $this->role = $role;
301 37
  }
302
303
  /**
304
   * @return \SimpleAcl\Resource
305
   */
306 30
  public function getResource()
307
  {
308 30
    return $this->resource;
309
  }
310
311
  /**
312
   * @param \SimpleAcl\Resource $resource
313
   */
314 37
  public function setResource(\SimpleAcl\Resource $resource = null)
315
  {
316 37
    $this->resource = $resource;
317 37
  }
318
319
  /**
320
   * Check if $role and $resource match to need role and resource.
321
   *
322
   * @param Role|null                $role
323
   * @param \SimpleAcl\Resource $resource
324
   * @param string                   $needRoleName
325
   * @param string                   $needResourceName
326
   * @param                          $priority
327
   *
328
   * @return RuleResult|null
329
   */
330 26
  protected function match(Role $role = null, \SimpleAcl\Resource $resource = null, $needRoleName, $needResourceName, $priority)
331
  {
332
    if (
333
        (
334 26
            null === $role
335 26
            ||
336 26
            ($role && $role->getName() === $needRoleName)
337 26
        )
338 26
        &&
339
        (
340 26
            null === $resource
341 26
            ||
342 26
            ($resource && $resource->getName() === $needResourceName)
343 26
        )
344 26
    ) {
345 25
      return new RuleResult($this, $priority, $needRoleName, $needResourceName);
346
    }
347
348 17
    return null;
349
  }
350
351
  /**
352
   * @return int
353
   */
354 28
  public function getPriority()
355
  {
356 28
    return $this->priority;
357
  }
358
359
  /**
360
   * @param int $priority
361
   */
362 1
  public function setPriority($priority)
363
  {
364 1
    $this->priority = $priority;
365 1
  }
366
367
  /**
368
   * Creates an id for rule.
369
   *
370
   * @return string
371
   */
372 49
  protected function generateId()
373
  {
374 49
    static $idCountRuleSimpleAcl = 1;
375
376 49
    return $idCountRuleSimpleAcl++;
377
  }
378
}
379