Conditions   A
last analyzed

Complexity

Total Complexity 35

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 35
eloc 81
c 1
b 0
f 0
dl 0
loc 256
rs 9.6

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getFirstAudienceRestriction() 0 9 3
A getNotBeforeDateTime() 0 7 2
A getNotOnOrAfterDateTime() 0 7 2
A getAllProxyRestrictions() 0 10 3
A getNotOnOrAfterTimestamp() 0 3 1
A getFirstProxyRestriction() 0 9 3
A deserialize() 0 29 1
A getFirstOneTimeUse() 0 9 3
A getAllItems() 0 3 1
A addItem() 0 5 1
A getNotOnOrAfterString() 0 7 2
A serialize() 0 11 2
A setNotOnOrAfter() 0 5 1
A getNotBeforeTimestamp() 0 3 1
A getNotBeforeString() 0 7 2
A getAllAudienceRestrictions() 0 10 3
A getAllOneTimeUses() 0 10 3
A setNotBefore() 0 5 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\AbstractSamlModel;
16
use LightSaml\Model\Context\DeserializationContext;
17
use LightSaml\Model\Context\SerializationContext;
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 = [];
36
37
    /**
38
     * @return Conditions
39
     */
40
    public function addItem(AbstractCondition $item)
41
    {
42
        $this->items[] = $item;
43
44
        return $this;
45
    }
46
47
    /**
48
     * @return AbstractCondition[]|AudienceRestriction[]|OneTimeUse[]|ProxyRestriction[]|array
49
     */
50
    public function getAllItems()
51
    {
52
        return $this->items;
53
    }
54
55
    /**
56
     * @return \LightSaml\Model\Assertion\AudienceRestriction[]
57
     */
58
    public function getAllAudienceRestrictions()
59
    {
60
        $result = [];
61
        foreach ($this->items as $item) {
62
            if ($item instanceof AudienceRestriction) {
63
                $result[] = $item;
64
            }
65
        }
66
67
        return $result;
68
    }
69
70
    /**
71
     * @return \LightSaml\Model\Assertion\AudienceRestriction|null
72
     */
73
    public function getFirstAudienceRestriction()
74
    {
75
        foreach ($this->items as $item) {
76
            if ($item instanceof AudienceRestriction) {
77
                return $item;
78
            }
79
        }
80
81
        return null;
82
    }
83
84
    /**
85
     * @return \LightSaml\Model\Assertion\OneTimeUse[]
86
     */
87
    public function getAllOneTimeUses()
88
    {
89
        $result = [];
90
        foreach ($this->items as $item) {
91
            if ($item instanceof OneTimeUse) {
92
                $result[] = $item;
93
            }
94
        }
95
96
        return $result;
97
    }
98
99
    /**
100
     * @return \LightSaml\Model\Assertion\OneTimeUse|null
101
     */
102
    public function getFirstOneTimeUse()
103
    {
104
        foreach ($this->items as $item) {
105
            if ($item instanceof OneTimeUse) {
106
                return $item;
107
            }
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * @return \LightSaml\Model\Assertion\ProxyRestriction[]
115
     */
116
    public function getAllProxyRestrictions()
117
    {
118
        $result = [];
119
        foreach ($this->items as $item) {
120
            if ($item instanceof ProxyRestriction) {
121
                $result[] = $item;
122
            }
123
        }
124
125
        return $result;
126
    }
127
128
    /**
129
     * @return \LightSaml\Model\Assertion\ProxyRestriction|null
130
     */
131
    public function getFirstProxyRestriction()
132
    {
133
        foreach ($this->items as $item) {
134
            if ($item instanceof ProxyRestriction) {
135
                return $item;
136
            }
137
        }
138
139
        return null;
140
    }
141
142
    /**
143
     * @param int|string|\DateTime|null $notBefore
144
     *
145
     * @return Conditions
146
     */
147
    public function setNotBefore($notBefore)
148
    {
149
        $this->notBefore = Helper::getTimestampFromValue($notBefore);
150
151
        return $this;
152
    }
153
154
    /**
155
     * @return int|null
156
     */
157
    public function getNotBeforeTimestamp()
158
    {
159
        return $this->notBefore;
160
    }
161
162
    /**
163
     * @return int|null
164
     */
165
    public function getNotBeforeString()
166
    {
167
        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 0. 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...
168
            return Helper::time2string($this->notBefore);
0 ignored issues
show
Bug Best Practice introduced by
The expression return LightSaml\Helper:...tring($this->notBefore) returns the type string which is incompatible with the documented return type integer|null.
Loading history...
169
        }
170
171
        return null;
172
    }
173
174
    /**
175
     * @return \DateTime|null
176
     */
177
    public function getNotBeforeDateTime()
178
    {
179
        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 0. 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...
180
            return new \DateTime('@'.$this->notBefore);
181
        }
182
183
        return null;
184
    }
185
186
    /**
187
     * @param int|null $notOnOrAfter
188
     *
189
     * @return Conditions
190
     */
191
    public function setNotOnOrAfter($notOnOrAfter)
192
    {
193
        $this->notOnOrAfter = Helper::getTimestampFromValue($notOnOrAfter);
194
195
        return $this;
196
    }
197
198
    /**
199
     * @return int|null
200
     */
201
    public function getNotOnOrAfterTimestamp()
202
    {
203
        return $this->notOnOrAfter;
204
    }
205
206
    /**
207
     * @return string|null
208
     */
209
    public function getNotOnOrAfterString()
210
    {
211
        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 0. 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...
212
            return Helper::time2string($this->notOnOrAfter);
213
        }
214
215
        return null;
216
    }
217
218
    /**
219
     * @return \DateTime|null
220
     */
221
    public function getNotOnOrAfterDateTime()
222
    {
223
        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 0. 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...
224
            return new \DateTime('@'.$this->notOnOrAfter);
225
        }
226
227
        return null;
228
    }
229
230
    /**
231
     * @return void
232
     */
233
    public function serialize(\DOMNode $parent, SerializationContext $context)
234
    {
235
        $result = $this->createElement('Conditions', SamlConstants::NS_ASSERTION, $parent, $context);
236
237
        $this->attributesToXml(
238
            ['NotBefore', 'NotOnOrAfter'],
239
            $result
240
        );
241
242
        foreach ($this->items as $item) {
243
            $item->serialize($result, $context);
244
        }
245
    }
246
247
    public function deserialize(\DOMNode $node, DeserializationContext $context)
248
    {
249
        $this->checkXmlNodeName($node, 'Conditions', SamlConstants::NS_ASSERTION);
250
251
        $this->attributesFromXml($node, ['NotBefore', 'NotOnOrAfter']);
252
253
        $this->manyElementsFromXml(
254
            $node,
255
            $context,
256
            'AudienceRestriction',
257
            'saml',
258
            'LightSaml\Model\Assertion\AudienceRestriction',
259
            'addItem'
260
        );
261
        $this->manyElementsFromXml(
262
            $node,
263
            $context,
264
            'OneTimeUse',
265
            'saml',
266
            'LightSaml\Model\Assertion\OneTimeUse',
267
            'addItem'
268
        );
269
        $this->manyElementsFromXml(
270
            $node,
271
            $context,
272
            'ProxyRestriction',
273
            'saml',
274
            'LightSaml\Model\Assertion\ProxyRestriction',
275
            'addItem'
276
        );
277
    }
278
}
279