RuleResultCollection::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 4
cts 4
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace SimpleAcl;
4
5
use IteratorAggregate;
6
7
/**
8
 * Holds RuleResult sorted according priority.
9
 *
10
 * @package SimpleAcl
11
 */
12
class RuleResultCollection implements IteratorAggregate
13
{
14
  /**
15
   * @var array
16
   */
17
  private $collection;
18
19
  /**
20
   * __construct
21 33
   */
22
  public function __construct()
23 33
  {
24 33
    $this->collection = [];
25
  }
26
27
  /**
28
   * @return \ArrayIterator
29 3
   */
30
  public function getIterator(): \ArrayIterator
31 3
  {
32
    return new \ArrayIterator($this->collection);
33
  }
34
35
  /**
36
   * @param RuleResult $result
37 28
   */
38
  public function add(RuleResult $result = null)
39 28
  {
40 17
    if (!$result) {
41
      return;
42
    }
43 26
44 1
    if (null === $result->getAction()) {
45
      return;
46
    }
47 26
48 26
    $this->collection[] = $result;
49
  }
50
51
  /**
52
   * @return bool|mixed
53 32
   */
54
  public function get()
55 32
  {
56 21
    if (!$this->any()) {
57
      return false;
58
    }
59
60 25
    /** @var RuleResult $result */
61
    $result = array_pop($this->collection);
62 25
63
    return $result->getAction();
64
  }
65
66
  /**
67
   * @return bool
68 33
   */
69
  public function any(): bool
70 33
  {
71
    return \count($this->collection) > 0;
72
  }
73
}
74