CriteriaSet::addIfNone()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/*
4
 * This file is part of the LightSAML-Core package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace LightSaml\Criteria;
13
14
class CriteriaSet
15
{
16
    /** @var array|CriteriaInterface[] */
17
    protected $criterions = array();
18
19
    /**
20
     * @param CriteriaInterface[] $criterions
21
     */
22 32
    public function __construct(array $criterions = array())
23
    {
24 32
        foreach ($criterions as $criterion) {
25 14
            $this->add($criterion);
26
        }
27 32
    }
28
29
    /**
30
     * @param CriteriaInterface $criteria
31
     *
32
     * @return CriteriaSet
33
     */
34 43
    public function add(CriteriaInterface $criteria)
35
    {
36 43
        $this->criterions[] = $criteria;
37
38 43
        return $this;
39
    }
40
41
    /**
42
     * @param CriteriaInterface $criteria
43
     *
44
     * @return CriteriaSet
45
     */
46 1
    public function addIfNone(CriteriaInterface $criteria)
47
    {
48 1
        if (false == $this->has(get_class($criteria))) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
49 1
            $this->add($criteria);
50
        }
51
52 1
        return $this;
53
    }
54
55
    /**
56
     * @param CriteriaSet $criteriaSet
57
     *
58
     * @return CriteriaSet
59
     */
60 1
    public function addAll(CriteriaSet $criteriaSet)
61
    {
62 1
        foreach ($criteriaSet->all() as $criteria) {
63 1
            $this->add($criteria);
64
        }
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * @param mixed    $condition
71
     * @param callable $callback
72
     *
73
     * @return CriteriaSet
74
     */
75 7
    public function addIf($condition, $callback)
76
    {
77 7
        if ($condition) {
78 7
            $criteria = call_user_func($callback);
79 7
            if ($criteria) {
80 7
                $this->add($criteria);
81
            }
82
        }
83
84 7
        return $this;
85
    }
86
87
    /**
88
     * @return CriteriaInterface[]|array
89
     */
90 3
    public function all()
91
    {
92 3
        return $this->criterions;
93
    }
94
95
    /**
96
     * @param string $class
97
     *
98
     * @return array|CriteriaInterface[]
99
     */
100 15
    public function get($class)
101
    {
102 15
        $result = array();
103 15
        foreach ($this->criterions as $criteria) {
104 15
            if ($criteria instanceof $class) {
105 15
                $result[] = $criteria;
106
            }
107
        }
108
109 15
        return $result;
110
    }
111
112
    /**
113
     * @param string $class
114
     *
115
     * @return CriteriaInterface|null
116
     */
117 14
    public function getSingle($class)
118
    {
119 14
        foreach ($this->criterions as $criteria) {
120 14
            if ($criteria instanceof $class) {
121 14
                return $criteria;
122
            }
123
        }
124
125
        return null;
126
    }
127
128
    /**
129
     * @param string $class
130
     *
131
     * @return bool
132
     */
133 32
    public function has($class)
134
    {
135 32
        foreach ($this->criterions as $criteria) {
136 31
            if ($criteria instanceof $class) {
137 31
                return true;
138
            }
139
        }
140
141 13
        return false;
142
    }
143
}
144