RoleDescriptor::deserialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 38
rs 9.424
cc 1
nc 1
nop 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\Model\Metadata;
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\Model\XmlDSig\Signature;
19
use LightSaml\SamlConstants;
20
21
abstract class RoleDescriptor extends AbstractSamlModel
22
{
23
    /** @var string|null */
24
    protected $id;
25
26
    /** @var int|null */
27
    protected $validUntil;
28
29
    /** @var string|null */
30
    protected $cacheDuration;
31
32
    /** @var string */
33
    protected $protocolSupportEnumeration = SamlConstants::PROTOCOL_SAML2;
34
35
    /** @var string|null */
36
    protected $errorURL;
37
38
    /** @var Signature[]|null */
39
    protected $signatures;
40
41
    /** @var KeyDescriptor[]|null */
42
    protected $keyDescriptors;
43
44
    /** @var Organization[]|null */
45
    protected $organizations;
46
47
    /** @var ContactPerson[]|null */
48
    protected $contactPersons;
49
50
    /**
51
     * @param string|null $cacheDuration
52
     *
53
     * @throws \InvalidArgumentException
54
     *
55
     * @return RoleDescriptor
56
     */
57
    public function setCacheDuration($cacheDuration)
58
    {
59
        Helper::validateDurationString($cacheDuration);
60
61
        $this->cacheDuration = $cacheDuration;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return string|null
68
     */
69
    public function getCacheDuration()
70
    {
71
        return $this->cacheDuration;
72
    }
73
74
    /**
75
     * @return RoleDescriptor
76
     */
77
    public function addContactPerson(ContactPerson $contactPerson)
78
    {
79
        if (false == is_array($this->contactPersons)) {
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...
80
            $this->contactPersons = [];
81
        }
82
        $this->contactPersons[] = $contactPerson;
83
84
        return $this;
85
    }
86
87
    /**
88
     * @return \LightSaml\Model\Metadata\ContactPerson[]|null
89
     */
90
    public function getAllContactPersons()
91
    {
92
        return $this->contactPersons;
93
    }
94
95
    /**
96
     * @param string|null $errorURL
97
     *
98
     * @return RoleDescriptor
99
     */
100
    public function setErrorURL($errorURL)
101
    {
102
        $this->errorURL = (string) $errorURL;
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return string|null
109
     */
110
    public function getErrorURL()
111
    {
112
        return $this->errorURL;
113
    }
114
115
    /**
116
     * @param string|null $id
117
     *
118
     * @return RoleDescriptor
119
     */
120
    public function setID($id)
121
    {
122
        $this->id = (string) $id;
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return string|null
129
     */
130
    public function getID()
131
    {
132
        return $this->id;
133
    }
134
135
    /**
136
     * @return RoleDescriptor
137
     */
138
    public function addKeyDescriptor(KeyDescriptor $keyDescriptor)
139
    {
140
        if (false == is_array($this->keyDescriptors)) {
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...
141
            $this->keyDescriptors = [];
142
        }
143
        $this->keyDescriptors[] = $keyDescriptor;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @return \LightSaml\Model\Metadata\KeyDescriptor[]|null
150
     */
151
    public function getAllKeyDescriptors()
152
    {
153
        return $this->keyDescriptors;
154
    }
155
156
    /**
157
     * @param string $use
158
     *
159
     * @return KeyDescriptor[]
160
     */
161
    public function getAllKeyDescriptorsByUse($use)
162
    {
163
        $result = [];
164
        foreach ($this->getAllKeyDescriptors() as $kd) {
165
            if ($kd->getUse() == $use) {
166
                $result[] = $kd;
167
            }
168
        }
169
170
        return $result;
171
    }
172
173
    /**
174
     * @param string|null $use
175
     *
176
     * @return KeyDescriptor|null
177
     */
178
    public function getFirstKeyDescriptor($use = null)
179
    {
180
        if ($this->getAllKeyDescriptors()) {
181
            foreach ($this->getAllKeyDescriptors() as $kd) {
182
                if (null == $use || $kd->getUse() == $use) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $use of type null|string against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
183
                    return $kd;
184
                }
185
            }
186
        }
187
188
        return;
189
    }
190
191
    /**
192
     * @return RoleDescriptor
193
     */
194
    public function addOrganization(Organization $organization)
195
    {
196
        if (false == is_array($this->organizations)) {
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...
197
            $this->organizations = [];
198
        }
199
        $this->organizations[] = $organization;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return Organization[]|null
206
     */
207
    public function getAllOrganizations()
208
    {
209
        return $this->organizations;
210
    }
211
212
    /**
213
     * @param string $protocolSupportEnumeration
214
     *
215
     * @return RoleDescriptor
216
     */
217
    public function setProtocolSupportEnumeration($protocolSupportEnumeration)
218
    {
219
        $this->protocolSupportEnumeration = (string) $protocolSupportEnumeration;
220
221
        return $this;
222
    }
223
224
    /**
225
     * @return string
226
     */
227
    public function getProtocolSupportEnumeration()
228
    {
229
        return $this->protocolSupportEnumeration;
230
    }
231
232
    /**
233
     * @return RoleDescriptor
234
     */
235
    public function addSignature(Signature $signature)
236
    {
237
        if (false == is_array($this->signatures)) {
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...
238
            $this->signatures = [];
239
        }
240
        $this->signatures[] = $signature;
241
242
        return $this;
243
    }
244
245
    /**
246
     * @return \LightSaml\Model\XmlDSig\Signature[]|null
247
     */
248
    public function getAllSignatures()
249
    {
250
        return $this->signatures;
251
    }
252
253
    /**
254
     * @param int|null $validUntil
255
     *
256
     * @return RoleDescriptor
257
     */
258
    public function setValidUntil($validUntil)
259
    {
260
        $this->validUntil = Helper::getTimestampFromValue($validUntil);
261
262
        return $this;
263
    }
264
265
    /**
266
     * @return string
267
     */
268
    public function getValidUntilString()
269
    {
270
        if ($this->validUntil) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->validUntil 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...
271
            return Helper::time2string($this->validUntil);
272
        }
273
274
        return;
275
    }
276
277
    /**
278
     * @return int
279
     */
280
    public function getValidUntilTimestamp()
281
    {
282
        return $this->validUntil;
283
    }
284
285
    /**
286
     * @return \DateTime|null
287
     */
288
    public function getValidUntilDateTime()
289
    {
290
        if ($this->validUntil) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->validUntil 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...
291
            return new \DateTime('@'.$this->validUntil);
292
        }
293
294
        return null;
295
    }
296
297
    /**
298
     * @return void
299
     */
300
    public function serialize(\DOMNode $parent, SerializationContext $context)
301
    {
302
        $this->attributesToXml(
303
            ['protocolSupportEnumeration', 'ID', 'validUntil', 'cacheDuration', 'errorURL'],
304
            $parent
305
        );
306
307
        $this->manyElementsToXml($this->getAllSignatures(), $parent, $context, null);
308
        $this->manyElementsToXml($this->getAllKeyDescriptors(), $parent, $context, null);
309
        $this->manyElementsToXml($this->getAllOrganizations(), $parent, $context, null);
310
        $this->manyElementsToXml($this->getAllContactPersons(), $parent, $context, null);
311
    }
312
313
    public function deserialize(\DOMNode $node, DeserializationContext $context)
314
    {
315
        $this->attributesFromXml(
316
            $node,
317
            ['protocolSupportEnumeration', 'ID', 'validUntil', 'cacheDuration', 'errorURL']
318
        );
319
320
        $this->manyElementsFromXml(
321
            $node,
322
            $context,
323
            'Signature',
324
            'ds',
325
            'LightSaml\Model\XmlDSig\Signature',
326
            'addSignature'
327
        );
328
        $this->manyElementsFromXml(
329
            $node,
330
            $context,
331
            'KeyDescriptor',
332
            'md',
333
            'LightSaml\Model\Metadata\KeyDescriptor',
334
            'addKeyDescriptor'
335
        );
336
        $this->manyElementsFromXml(
337
            $node,
338
            $context,
339
            'Organization',
340
            'md',
341
            'LightSaml\Model\Metadata\Organization',
342
            'addOrganization'
343
        );
344
        $this->manyElementsFromXml(
345
            $node,
346
            $context,
347
            'ContactPerson',
348
            'md',
349
            'LightSaml\Model\Metadata\ContactPerson',
350
            'addContactPerson'
351
        );
352
    }
353
}
354