Passed
Branch assertions (18e068)
by Tim
03:23
created

RoleDescriptor::getProtocolSupportEnumeration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\md;
6
7
use SAML2\Constants;
8
use SAML2\SignedElementHelper;
9
use SAML2\Utils;
10
use SAML2\XML\Chunk;
11
12
/**
13
 * Class representing SAML 2 RoleDescriptor element.
14
 *
15
 * @package SimpleSAMLphp
16
 */
17
class RoleDescriptor extends SignedElementHelper
18
{
19
    /**
20
     * The name of this descriptor element.
21
     *
22
     * @var string
23
     */
24
    private $elementName;
25
26
    /**
27
     * The ID of this element.
28
     *
29
     * @var string|null
30
     */
31
    public $ID = null;
32
33
    /**
34
     * How long this element is valid, as a unix timestamp.
35
     *
36
     * @var int|null
37
     */
38
    public $validUntil = null;
39
40
    /**
41
     * The length of time this element can be cached, as string.
42
     *
43
     * @var string|null
44
     */
45
    public $cacheDuration = null;
46
47
    /**
48
     * List of supported protocols.
49
     *
50
     * @var array
51
     */
52
    public $protocolSupportEnumeration = [];
53
54
    /**
55
     * Error URL for this role.
56
     *
57
     * @var string|null
58
     */
59
    public $errorURL = null;
60
61
    /**
62
     * Extensions on this element.
63
     *
64
     * Array of extension elements.
65
     *
66
     * @var array
67
     */
68
    public $Extensions = [];
69
70
    /**
71
     * KeyDescriptor elements.
72
     *
73
     * Array of \SAML2\XML\md\KeyDescriptor elements.
74
     *
75
     * @var \SAML2\XML\md\KeyDescriptor[]
76
     */
77
    public $KeyDescriptor = [];
78
79
    /**
80
     * Organization of this role.
81
     *
82
     * @var \SAML2\XML\md\Organization|null
83
     */
84
    public $Organization = null;
85
86
    /**
87
     * ContactPerson elements for this role.
88
     *
89
     * Array of \SAML2\XML\md\ContactPerson objects.
90
     *
91
     * @var \SAML2\XML\md\ContactPerson[]
92
     */
93
    public $ContactPerson = [];
94
95
96
    /**
97
     * Initialize a RoleDescriptor.
98
     *
99
     * @param string          $elementName The name of this element.
100
     * @param \DOMElement|null $xml         The XML element we should load.
101
     * @throws \Exception
102
     */
103
    protected function __construct(string $elementName, \DOMElement $xml = null)
104
    {
105
        parent::__construct($xml);
106
        $this->elementName = $elementName;
107
108
        if ($xml === null) {
109
            return;
110
        }
111
112
        if ($xml->hasAttribute('ID')) {
113
            $this->setID($xml->getAttribute('ID'));
114
        }
115
        if ($xml->hasAttribute('validUntil')) {
116
            $this->setValidUntil(Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil')));
117
        }
118
        if ($xml->hasAttribute('cacheDuration')) {
119
            $this->setCacheDuration($xml->getAttribute('cacheDuration'));
120
        }
121
122
        if (!$xml->hasAttribute('protocolSupportEnumeration')) {
123
            throw new \Exception('Missing protocolSupportEnumeration attribute on '.$xml->localName);
124
        }
125
        $this->setProtocolSupportEnumeration(preg_split('/[\s]+/', $xml->getAttribute('protocolSupportEnumeration')));
0 ignored issues
show
Bug introduced by
It seems like preg_split('/[\s]+/', $x...olSupportEnumeration')) can also be of type false; however, parameter $protocols of SAML2\XML\md\RoleDescrip...colSupportEnumeration() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        $this->setProtocolSupportEnumeration(/** @scrutinizer ignore-type */ preg_split('/[\s]+/', $xml->getAttribute('protocolSupportEnumeration')));
Loading history...
126
127
        if ($xml->hasAttribute('errorURL')) {
128
            $this->setErrorURL($xml->getAttribute('errorURL'));
129
        }
130
131
        $this->setExtensions(Extensions::getList($xml));
132
133
        foreach (Utils::xpQuery($xml, './saml_metadata:KeyDescriptor') as $kd) {
134
            $this->addKeyDescriptor(new KeyDescriptor($kd));
135
        }
136
137
        $organization = Utils::xpQuery($xml, './saml_metadata:Organization');
138
        if (count($organization) > 1) {
139
            throw new \Exception('More than one Organization in the entity.');
140
        } elseif (!empty($organization)) {
141
            $this->setOrganization(new Organization($organization[0]));
142
        }
143
144
        foreach (Utils::xpQuery($xml, './saml_metadata:ContactPerson') as $cp) {
145
            $this->addContactPerson(new ContactPerson($cp));
146
        }
147
    }
148
149
150
    /**
151
     * Collect the value of the ID-property
152
     * @return string|null
153
     */
154
    public function getID()
155
    {
156
        return $this->ID;
157
    }
158
159
160
    /**
161
     * Set the value of the ID-property
162
     * @param string|null $Id
163
     * @return void
164
     */
165
    public function setID(string $Id = null)
166
    {
167
        $this->ID = $Id;
168
    }
169
170
171
    /**
172
     * Collect the value of the validUntil-property
173
     * @return int|null
174
     */
175
    public function getValidUntil()
176
    {
177
        return $this->validUntil;
178
    }
179
180
181
    /**
182
     * Set the value of the validUntil-property
183
     * @param int|null $validUntil
184
     * @return void
185
     */
186
    public function setValidUntil(int $validUntil = null)
187
    {
188
        $this->validUntil = $validUntil;
189
    }
190
191
192
    /**
193
     * Collect the value of the cacheDuration-property
194
     * @return string|null
195
     */
196
    public function getCacheDuration()
197
    {
198
        return $this->cacheDuration;
199
    }
200
201
202
    /**
203
     * Set the value of the cacheDuration-property
204
     * @param string|null $cacheDuration
205
     * @return void
206
     */
207
    public function setCacheDuration(string $cacheDuration = null)
208
    {
209
        $this->cacheDuration = $cacheDuration;
210
    }
211
212
213
    /**
214
     * Collect the value of the Extensions-property
215
     * @return \SAML2\XML\Chunk[]
216
     */
217
    public function getExtensions() : array
218
    {
219
        return $this->Extensions;
220
    }
221
222
223
    /**
224
     * Set the value of the Extensions-property
225
     * @param array $extensions
226
     * @return void
227
     */
228
    public function setExtensions(array $extensions)
229
    {
230
        $this->Extensions = $extensions;
231
    }
232
233
234
    /**
235
     * Add an Extension.
236
     *
237
     * @param \SAML2\XML\Chunk $extensions The Extensions
238
     * @return void
239
     */
240
    public function addExtension(Extensions $extension)
241
    {
242
        $this->Extensions[] = $extension;
243
    }
244
245
246
    /**
247
     * Set the value of the errorURL-property
248
     * @param string|null $errorURL
249
     * @return void
250
     */
251
    public function setErrorURL(string $errorURL = null)
252
    {
253
        if (!is_null($errorURL) && !filter_var($errorURL, FILTER_VALIDATE_URL)) {
254
            throw new \InvalidArgumentException('RoleDescriptor errorURL is not a valid URL.');
255
        }
256
        $this->errorURL = $errorURL;
257
    }
258
259
260
    /**
261
     * Collect the value of the errorURL-property
262
     * @return string|null
263
     */
264
    public function getErrorURL()
265
    {
266
        return $this->errorURL;
267
    }
268
269
270
    /**
271
     * Collect the value of the ProtocolSupportEnumeration-property
272
     * @return string[]
273
     */
274
    public function getProtocolSupportEnumeration() : array
275
    {
276
        return $this->protocolSupportEnumeration;
277
    }
278
279
280
    /**
281
     * Set the value of the ProtocolSupportEnumeration-property
282
     * @param array $protocols
283
     * @return void
284
     */
285
    public function setProtocolSupportEnumeration(array $protocols)
286
    {
287
        $this->protocolSupportEnumeration = $protocols;
288
    }
289
290
291
    /**
292
     * Add the value to the ProtocolSupportEnumeration-property
293
     * @param string $protocol
294
     * @return void
295
     */
296
    public function addProtocolSupportEnumeration(string $protocol)
297
    {
298
        $this->protocolSupportEnumeration[] = $protocol;
299
    }
300
301
302
    /**
303
     * Collect the value of the Organization-property
304
     * @return \SAML2\XML\md\Organization|null
305
     */
306
    public function getOrganization()
307
    {
308
        return $this->Organization;
309
    }
310
311
312
    /**
313
     * Set the value of the Organization-property
314
     * @param \SAML2\XML\md\Organization|null $organization
315
     * @return void
316
     */
317
    public function setOrganization(Organization $organization = null)
318
    {
319
        $this->Organization = $organization;
320
    }
321
322
323
    /**
324
     * Collect the value of the ContactPerson-property
325
     * @return \SAML2\XML\md\ContactPerson[]
326
     */
327
    public function getContactPerson() : array
328
    {
329
        return $this->ContactPerson;
330
    }
331
332
333
    /**
334
     * Set the value of the ContactPerson-property
335
     * @param array $contactPerson
336
     * @return void
337
     */
338
    public function setContactPerson(array $contactPerson)
339
    {
340
        $this->ContactPerson = $contactPerson;
341
    }
342
343
344
    /**
345
     * Add the value to the ContactPerson-property
346
     * @param \SAML2\XML\md\ContactPerson $contactPerson
347
     * @return void
348
     */
349
    public function addContactPerson(ContactPerson $contactPerson)
350
    {
351
        $this->ContactPerson[] = $contactPerson;
352
    }
353
354
355
    /**
356
     * Collect the value of the KeyDescriptor-property
357
     * @return \SAML2\XML\md\KeyDescriptor[]
358
     */
359
    public function getKeyDescriptor() : array
360
    {
361
        return $this->KeyDescriptor;
362
    }
363
364
365
    /**
366
     * Set the value of the KeyDescriptor-property
367
     * @param array $keyDescriptor
368
     * @return void
369
     */
370
    public function setKeyDescriptor(array $keyDescriptor)
371
    {
372
        $this->KeyDescriptor = $keyDescriptor;
373
    }
374
375
376
    /**
377
     * Add the value to the KeyDescriptor-property
378
     * @param \SAML2\XML\md\KeyDescriptor $keyDescriptor
379
     * @return void
380
     */
381
    public function addKeyDescriptor(KeyDescriptor $keyDescriptor)
382
    {
383
        $this->KeyDescriptor[] = $keyDescriptor;
384
    }
385
386
387
    /**
388
     * Add this RoleDescriptor to an EntityDescriptor.
389
     *
390
     * @param \DOMElement $parent The EntityDescriptor we should append this endpoint to.
391
     * @return \DOMElement
392
     */
393
    protected function toXML(\DOMElement $parent) : \DOMElement
394
    {
395
        $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, $this->elementName);
396
        $parent->appendChild($e);
397
398
        if ($this->getID() !== null) {
399
            $e->setAttribute('ID', $this->getID());
400
        }
401
402
        if ($this->getValidUntil() !== null) {
403
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->getValidUntil()));
404
        }
405
406
        if ($this->getCacheDuration() !== null) {
407
            $e->setAttribute('cacheDuration', $this->getCacheDuration());
408
        }
409
410
        $e->setAttribute('protocolSupportEnumeration', implode(' ', $this->getProtocolSupportEnumeration()));
411
412
        if ($this->getErrorURL() !== null) {
413
            $e->setAttribute('errorURL', $this->getErrorURL());
414
        }
415
416
        Extensions::addList($e, $this->getExtensions());
417
418
        foreach ($this->getKeyDescriptor() as $kd) {
419
            $kd->toXML($e);
420
        }
421
422
        if ($this->getOrganization() !== null) {
423
            $this->getOrganization()->toXML($e);
424
        }
425
426
        foreach ($this->getContactPerson() as $cp) {
427
            $cp->toXML($e);
428
        }
429
430
        return $e;
431
    }
432
}
433