Conditions   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 268
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 2

Test Coverage

Coverage 86.6%

Importance

Changes 0
Metric Value
dl 0
loc 268
c 0
b 0
f 0
wmc 35
lcom 4
cbo 2
ccs 84
cts 97
cp 0.866
rs 9.6

18 Methods

Rating   Name   Duplication   Size   Complexity  
A addItem() 0 6 1
A getAllItems() 0 4 1
A getAllAudienceRestrictions() 0 11 3
A getFirstAudienceRestriction() 0 10 3
A getAllOneTimeUses() 0 11 3
A getFirstOneTimeUse() 0 10 3
A getAllProxyRestrictions() 0 11 3
A getFirstProxyRestriction() 0 10 3
A setNotBefore() 0 6 1
A getNotBeforeTimestamp() 0 4 1
A getNotBeforeString() 0 8 2
A getNotBeforeDateTime() 0 8 2
A setNotOnOrAfter() 0 6 1
A getNotOnOrAfterTimestamp() 0 4 1
A getNotOnOrAfterString() 0 8 2
A getNotOnOrAfterDateTime() 0 8 2
A serialize() 0 13 2
A deserialize() 0 31 1
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\Model\Assertion;
13
14
use LightSaml\Helper;
15
use LightSaml\Model\Context\DeserializationContext;
16
use LightSaml\Model\Context\SerializationContext;
17
use LightSaml\Model\AbstractSamlModel;
18
use LightSaml\SamlConstants;
19
20
class Conditions extends AbstractSamlModel
21
{
22
    /**
23
     * @var int|null
24
     */
25
    protected $notBefore;
26
27
    /**
28
     * @var int|null
29
     */
30
    protected $notOnOrAfter;
31
32
    /**
33
     * @var array|AbstractCondition[]|AudienceRestriction[]|OneTimeUse[]|ProxyRestriction[]
34
     */
35
    protected $items = array();
36
37
    /**
38
     * @param AbstractCondition $item
39
     *
40
     * @return Conditions
41
     */
42 18
    public function addItem(AbstractCondition $item)
43
    {
44 18
        $this->items[] = $item;
45
46 18
        return $this;
47
    }
48
49
    /**
50
     * @return AbstractCondition[]|AudienceRestriction[]|OneTimeUse[]|ProxyRestriction[]|array
51
     */
52 6
    public function getAllItems()
53
    {
54 6
        return $this->items;
55
    }
56
57
    /**
58
     * @return \LightSaml\Model\Assertion\AudienceRestriction[]
59
     */
60 1
    public function getAllAudienceRestrictions()
61
    {
62 1
        $result = array();
63 1
        foreach ($this->items as $item) {
64 1
            if ($item instanceof AudienceRestriction) {
65 1
                $result[] = $item;
66
            }
67
        }
68
69 1
        return $result;
70
    }
71
72
    /**
73
     * @return \LightSaml\Model\Assertion\AudienceRestriction|null
74
     */
75 2
    public function getFirstAudienceRestriction()
76
    {
77 2
        foreach ($this->items as $item) {
78 2
            if ($item instanceof AudienceRestriction) {
79 2
                return $item;
80
            }
81
        }
82
83
        return null;
84
    }
85
86
    /**
87
     * @return \LightSaml\Model\Assertion\OneTimeUse[]
88
     */
89 1
    public function getAllOneTimeUses()
90
    {
91 1
        $result = array();
92 1
        foreach ($this->items as $item) {
93 1
            if ($item instanceof OneTimeUse) {
94 1
                $result[] = $item;
95
            }
96
        }
97
98 1
        return $result;
99
    }
100
101
    /**
102
     * @return \LightSaml\Model\Assertion\OneTimeUse|null
103
     */
104 1
    public function getFirstOneTimeUse()
105
    {
106 1
        foreach ($this->items as $item) {
107 1
            if ($item instanceof OneTimeUse) {
108 1
                return $item;
109
            }
110
        }
111
112
        return null;
113
    }
114
115
    /**
116
     * @return \LightSaml\Model\Assertion\ProxyRestriction[]
117
     */
118 1
    public function getAllProxyRestrictions()
119
    {
120 1
        $result = array();
121 1
        foreach ($this->items as $item) {
122 1
            if ($item instanceof ProxyRestriction) {
123 1
                $result[] = $item;
124
            }
125
        }
126
127 1
        return $result;
128
    }
129
130
    /**
131
     * @return \LightSaml\Model\Assertion\ProxyRestriction|null
132
     */
133 1
    public function getFirstProxyRestriction()
134
    {
135 1
        foreach ($this->items as $item) {
136 1
            if ($item instanceof ProxyRestriction) {
137 1
                return $item;
138
            }
139
        }
140
141
        return null;
142
    }
143
144
    /**
145
     * @param int|string|\DateTime|null $notBefore
146
     *
147
     * @return Conditions
148
     */
149 10
    public function setNotBefore($notBefore)
150
    {
151 10
        $this->notBefore = Helper::getTimestampFromValue($notBefore);
152
153 10
        return $this;
154
    }
155
156
    /**
157
     * @return int|null
158
     */
159 10
    public function getNotBeforeTimestamp()
160
    {
161 10
        return $this->notBefore;
162
    }
163
164
    /**
165
     * @return int|null
166
     */
167 5
    public function getNotBeforeString()
168
    {
169 5
        if ($this->notBefore) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->notBefore of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
170 5
            return Helper::time2string($this->notBefore);
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * @return \DateTime|null
178
     */
179
    public function getNotBeforeDateTime()
180
    {
181
        if ($this->notBefore) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->notBefore of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
182
            return new \DateTime('@'.$this->notBefore);
183
        }
184
185
        return null;
186
    }
187
188
    /**
189
     * @param int|null $notOnOrAfter
190
     *
191
     * @return Conditions
192
     */
193 10
    public function setNotOnOrAfter($notOnOrAfter)
194
    {
195 10
        $this->notOnOrAfter = Helper::getTimestampFromValue($notOnOrAfter);
196
197 10
        return $this;
198
    }
199
200
    /**
201
     * @return int|null
202
     */
203 4
    public function getNotOnOrAfterTimestamp()
204
    {
205 4
        return $this->notOnOrAfter;
206
    }
207
208
    /**
209
     * @return string|null
210
     */
211 5
    public function getNotOnOrAfterString()
212
    {
213 5
        if ($this->notOnOrAfter) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->notOnOrAfter of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
214 5
            return Helper::time2string($this->notOnOrAfter);
215
        }
216
217
        return null;
218
    }
219
220
    /**
221
     * @return \DateTime|null
222
     */
223
    public function getNotOnOrAfterDateTime()
224
    {
225
        if ($this->notOnOrAfter) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->notOnOrAfter of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
226
            return new \DateTime('@'.$this->notOnOrAfter);
227
        }
228
229
        return null;
230
    }
231
232
    /**
233
     * @param \DOMNode             $parent
234
     * @param SerializationContext $context
235
     *
236
     * @return void
237
     */
238 4
    public function serialize(\DOMNode $parent, SerializationContext $context)
239
    {
240 4
        $result = $this->createElement('Conditions', SamlConstants::NS_ASSERTION, $parent, $context);
241
242 4
        $this->attributesToXml(
243 4
            array('NotBefore', 'NotOnOrAfter'),
244 4
            $result
245
        );
246
247 4
        foreach ($this->items as $item) {
248 4
            $item->serialize($result, $context);
249
        }
250 4
    }
251
252
    /**
253
     * @param \DOMNode               $node
254
     * @param DeserializationContext $context
255
     */
256 3
    public function deserialize(\DOMNode $node, DeserializationContext $context)
257
    {
258 3
        $this->checkXmlNodeName($node, 'Conditions', SamlConstants::NS_ASSERTION);
259
260 3
        $this->attributesFromXml($node, array('NotBefore', 'NotOnOrAfter'));
0 ignored issues
show
Compatibility introduced by
$node of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
261
262 3
        $this->manyElementsFromXml(
263 3
            $node,
0 ignored issues
show
Compatibility introduced by
$node of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
264 3
            $context,
265 3
            'AudienceRestriction',
266 3
            'saml',
267 3
            'LightSaml\Model\Assertion\AudienceRestriction',
268 3
            'addItem'
269
        );
270 3
        $this->manyElementsFromXml(
271 3
            $node,
0 ignored issues
show
Compatibility introduced by
$node of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
272 3
            $context,
273 3
            'OneTimeUse',
274 3
            'saml',
275 3
            'LightSaml\Model\Assertion\OneTimeUse',
276 3
            'addItem'
277
        );
278 3
        $this->manyElementsFromXml(
279 3
            $node,
0 ignored issues
show
Compatibility introduced by
$node of type object<DOMNode> is not a sub-type of object<DOMElement>. It seems like you assume a child class of the class DOMNode to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
280 3
            $context,
281 3
            'ProxyRestriction',
282 3
            'saml',
283 3
            'LightSaml\Model\Assertion\ProxyRestriction',
284 3
            'addItem'
285
        );
286 3
    }
287
}
288