Passed
Push — release-4-alpha ( 53e910...362819 )
by Tim
02:18
created

EntityDescriptor::setValidUntil()   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 1
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\DOMDocumentFactory;
9
use SAML2\SignedElementHelper;
10
use SAML2\Utils;
11
use SAML2\XML\Chunk;
12
13
/**
14
 * Class representing SAML 2 EntityDescriptor element.
15
 *
16
 * @package SimpleSAMLphp
17
 */
18
class EntityDescriptor extends SignedElementHelper
19
{
20
    /**
21
     * The entityID this EntityDescriptor represents.
22
     *
23
     * @var string|null
24
     */
25
    private $entityID = null;
26
27
    /**
28
     * The ID of this element.
29
     *
30
     * @var string|null
31
     */
32
    private $ID = null;
33
34
    /**
35
     * Extensions on this element.
36
     *
37
     * Array of extension elements.
38
     *
39
     * @var array
40
     */
41
    public $Extensions = [];
42
43
    /**
44
     * Array with all roles for this entity.
45
     *
46
     * Array of \SAML2\XML\md\RoleDescriptor objects (and subclasses of RoleDescriptor).
47
     *
48
     * @var (\SAML2\XML\md\UnknownRoleDescriptor|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SAML2\XML\md\UnknownRoleDescriptor| at position 3 could not be parsed: the token is null at position 3.
Loading history...
49
     *       \SAML2\XML\md\IDPSSODescriptor|
50
     *       \SAML2\XML\md\SPSSODescriptor|
51
     *       \SAML2\XML\md\AuthnAuthorityDescriptor|
52
     *       \SAML2\XML\md\AttributeAuthorityDescriptor|
53
     *       \SAML2\XML\md\PDPDescriptor)[]
54
     */
55
    public $RoleDescriptor = [];
56
57
    /**
58
     * AffiliationDescriptor of this entity.
59
     *
60
     * @var \SAML2\XML\md\AffiliationDescriptor|null
61
     */
62
    public $AffiliationDescriptor = null;
63
64
    /**
65
     * Organization of this entity.
66
     *
67
     * @var \SAML2\XML\md\Organization|null
68
     */
69
    public $Organization = null;
70
71
    /**
72
     * ContactPerson elements for this entity.
73
     *
74
     * @var \SAML2\XML\md\ContactPerson[]
75
     */
76
    public $ContactPerson = [];
77
78
    /**
79
     * AdditionalMetadataLocation elements for this entity.
80
     *
81
     * @var \SAML2\XML\md\AdditionalMetadataLocation[]
82
     */
83
    public $AdditionalMetadataLocation = [];
84
85
86
    /**
87
     * Initialize an EntitiyDescriptor.
88
     *
89
     * @param \DOMElement|null $xml The XML element we should load.
90
     * @throws \Exception
91
     */
92
    public function __construct(\DOMElement $xml = null)
93
    {
94
        parent::__construct($xml);
95
96
        if ($xml === null) {
97
            return;
98
        }
99
100
        if (!$xml->hasAttribute('entityID')) {
101
            throw new \Exception('Missing required attribute entityID on EntityDescriptor.');
102
        }
103
        $this->entityID = $xml->getAttribute('entityID');
104
105
        if ($xml->hasAttribute('ID')) {
106
            $this->ID = $xml->getAttribute('ID');
107
        }
108
        if ($xml->hasAttribute('validUntil')) {
109
            $this->validUntil = Utils::xsDateTimeToTimestamp($xml->getAttribute('validUntil'));
110
        }
111
        if ($xml->hasAttribute('cacheDuration')) {
112
            $this->cacheDuration = $xml->getAttribute('cacheDuration');
113
        }
114
115
        $this->Extensions = Extensions::getList($xml);
116
117
        foreach ($xml->childNodes as $node) {
118
            if (!($node instanceof \DOMElement)) {
119
                continue;
120
            }
121
122
            if ($node->namespaceURI !== Constants::NS_MD) {
123
                continue;
124
            }
125
126
            switch ($node->localName) {
127
                case 'RoleDescriptor':
128
                    $this->RoleDescriptor[] = new UnknownRoleDescriptor($node);
129
                    break;
130
                case 'IDPSSODescriptor':
131
                    $this->RoleDescriptor[] = new IDPSSODescriptor($node);
132
                    break;
133
                case 'SPSSODescriptor':
134
                    $this->RoleDescriptor[] = new SPSSODescriptor($node);
135
                    break;
136
                case 'AuthnAuthorityDescriptor':
137
                    $this->RoleDescriptor[] = new AuthnAuthorityDescriptor($node);
138
                    break;
139
                case 'AttributeAuthorityDescriptor':
140
                    $this->RoleDescriptor[] = new AttributeAuthorityDescriptor($node);
141
                    break;
142
                case 'PDPDescriptor':
143
                    $this->RoleDescriptor[] = new PDPDescriptor($node);
144
                    break;
145
                case 'AffiliationDescriptor':
146
                    if ($this->AffiliationDescriptor !== null) {
147
                        throw new \Exception('More than one AffiliationDescriptor in the entity.');
148
                    }
149
                    $this->AffiliationDescriptor = new AffiliationDescriptor($node);
150
                    break;
151
                case 'Organization':
152
                    if ($this->Organization !== null) {
153
                        throw new \Exception('More than one Organization in the entity.');
154
                    }
155
                    $this->Organization = new Organization($node);
156
                    break;
157
                case 'ContactPerson':
158
                    $this->ContactPerson[] = new ContactPerson($node);
159
                    break;
160
                case 'AdditionalMetadataLocation':
161
                    $this->AdditionalMetadataLocation[] = new AdditionalMetadataLocation($node);
162
                    break;
163
            }
164
        }
165
166
        if (empty($this->RoleDescriptor) && is_null($this->AffiliationDescriptor)) {
167
            throw new \Exception(
168
                'Must have either one of the RoleDescriptors or an AffiliationDescriptor in EntityDescriptor.'
169
            );
170
        } elseif (!empty($this->RoleDescriptor) && !is_null($this->AffiliationDescriptor)) {
171
            throw new \Exception(
172
                'AffiliationDescriptor cannot be combined with other RoleDescriptor elements in EntityDescriptor.'
173
            );
174
        }
175
    }
176
177
178
    /**
179
     * Collect the value of the entityID property.
180
     *
181
     * @return string|null
182
     */
183
    public function getEntityID() : string
184
    {
185
        return $this->entityID;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->entityID could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
186
    }
187
188
189
    /**
190
     * Set the value of the entityID-property
191
     * @param string $entityId
192
     * @return void
193
     */
194
    public function setEntityID(string $entityId)
195
    {
196
        $this->entityID = $entityId;
197
    }
198
199
200
    /**
201
     * Collect the value of the ID property.
202
     *
203
     * @return string|null
204
     */
205
    public function getID()
206
    {
207
        return $this->ID;
208
    }
209
210
211
    /**
212
     * Set the value of the ID property.
213
     *
214
     * @param string|null $Id
215
     * @return void
216
     */
217
    public function setID(string $Id = null)
218
    {
219
        $this->ID = $Id;
220
    }
221
222
223
    /**
224
     * Collect the value of the validUntil-property
225
     * @return int|null
226
     */
227
    public function getValidUntil()
228
    {
229
        return $this->validUntil;
230
    }
231
232
233
    /**
234
     * Set the value of the validUntil-property
235
     * @param int|null $validUntil
236
     * @return void
237
     */
238
    public function setValidUntil(int $validUntil = null)
239
    {
240
        $this->validUntil = $validUntil;
241
    }
242
243
244
    /**
245
     * Collect the value of the cacheDuration-property
246
     * @return string|null
247
     */
248
    public function getCacheDuration()
249
    {
250
        return $this->cacheDuration;
251
    }
252
253
254
    /**
255
     * Set the value of the cacheDuration-property
256
     * @param string|null $cacheDuration
257
     * @return void
258
     */
259
    public function setCacheDuration(string $cacheDuration = null)
260
    {
261
        $this->cacheDuration = $cacheDuration;
262
    }
263
264
265
    /**
266
     * Collect the value of the Extensions property.
267
     *
268
     * @return \SAML2\XML\Chunk[]
269
     */
270
    public function getExtensions() : array
271
    {
272
        return $this->Extensions;
273
    }
274
275
276
    /**
277
     * Set the value of the Extensions property.
278
     *
279
     * @param array $extensions
280
     * @return void
281
     */
282
    public function setExtensions(array $extensions)
283
    {
284
        $this->Extensions = $extensions;
285
    }
286
287
288
    /**
289
     * Add an Extension.
290
     *
291
     * @param \SAML2\XML\Chunk $extensions The Extensions
292
     * @return void
293
     */
294
    public function addExtension(Extensions $extension)
295
    {
296
        $this->Extensions[] = $extension;
297
    }
298
299
300
    /**
301
     * Collect the value of the RoleDescriptor property.
302
     *
303
     * @return (\SAML2\XML\md\UnknownRoleDescriptor|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SAML2\XML\md\UnknownRoleDescriptor| at position 3 could not be parsed: the token is null at position 3.
Loading history...
304
     *          \SAML2\XML\md\IDPSSODescriptor|
305
     *          \SAML2\XML\md\SPSSODescriptor|
306
     *          \SAML2\XML\md\AuthnAuthorityDescriptor|
307
     *          \SAML2\XML\md\AttributeAuthorityDescriptor|
308
     *          \SAML2\XML\md\PDPDescriptor)[]
309
     */
310
    public function getRoleDescriptor() : array
311
    {
312
        return $this->RoleDescriptor;
313
    }
314
315
316
    /**
317
     * Set the value of the RoleDescriptor property.
318
     *
319
     * @param (\SAML2\XML\md\UnknownRoleDescriptor|
320
     *         \SAML2\XML\md\IDPSSODescriptor|
321
     *         \SAML2\XML\md\SPSSODescriptor|
322
     *         \SAML2\XML\md\AuthnAuthorityDescriptor|
323
     *         \SAML2\XML\md\AttributeAuthorityDescriptor|
324
     *         \SAML2\XML\md\PDPDescriptor)[] $roleDescriptor
325
     * @return void
326
     */
327
    public function setRoleDescriptor(array $roleDescriptor)
328
    {
329
        $this->RoleDescriptor = $roleDescriptor;
330
    }
331
332
333
    /**
334
     * Add the value to the RoleDescriptor property.
335
     *
336
     * @param \SAML2\XML\md\UnknownRoleDescriptor|
337
     *        \SAML2\XML\md\IDPSSODescriptor|
338
     *        \SAML2\XML\md\SPSSODescriptor|
339
     *        \SAML2\XML\md\AuthnAuthorityDescriptor|
340
     *        \SAML2\XML\md\AttributeAuthorityDescriptor|
341
     *        \SAML2\XML\md\PDPDescriptor $roleDescriptor
342
     * @return void
343
     */
344
    public function addRoleDescriptor(RoleDescriptor $roleDescriptor)
345
    {
346
        $this->RoleDescriptor[] = $roleDescriptor;
347
    }
348
349
350
    /**
351
     * Collect the value of the AffiliationDescriptor property.
352
     *
353
     * @return \SAML2\XML\md\AffiliationDescriptor|null
354
     */
355
    public function getAffiliationDescriptor()
356
    {
357
        return $this->AffiliationDescriptor;
358
    }
359
360
361
    /**
362
     * Set the value of the AffliationDescriptor property.
363
     *
364
     * @param \SAML2\XML\md\AffiliationDescriptor|null $affiliationDescriptor
365
     * @return void
366
     */
367
    public function setAffiliationDescriptor(AffiliationDescriptor $affiliationDescriptor = null)
368
    {
369
        $this->AffiliationDescriptor = $affiliationDescriptor;
370
    }
371
372
373
    /**
374
     * Collect the value of the Organization property.
375
     *
376
     * @return \SAML2\XML\md\Organization|null
377
     */
378
    public function getOrganization()
379
    {
380
        return $this->Organization;
381
    }
382
383
384
    /**
385
     * Set the value of the Organization property.
386
     *
387
     * @param \SAML2\XML\md\Organization|null $organization
388
     * @return void
389
     */
390
    public function setOrganization(Organization $organization = null)
391
    {
392
        $this->Organization = $organization;
393
    }
394
395
396
    /**
397
     * Collect the value of the ContactPerson property.
398
     *
399
     * @return \SAML2\XML\md\ContactPerson[]
400
     */
401
    public function getContactPerson() : array
402
    {
403
        return $this->ContactPerson;
404
    }
405
406
407
    /**
408
     * Set the value of the ContactPerson property.
409
     *
410
     * @param array $contactPerson
411
     * @return void
412
     */
413
    public function setContactPerson(array $contactPerson)
414
    {
415
        $this->ContactPerson = $contactPerson;
416
    }
417
418
419
    /**
420
     * Add the value to the ContactPerson property.
421
     *
422
     * @param \SAML2\XML\md\ContactPerson $contactPerson
423
     * @return void
424
     */
425
    public function addContactPerson(ContactPerson $contactPerson)
426
    {
427
        $this->ContactPerson[] = $contactPerson;
428
    }
429
430
431
    /**
432
     * Collect the value of the AdditionalMetadataLocation property.
433
     *
434
     * @return \SAML2\XML\md\AdditionalMetadataLocation[]
435
     */
436
    public function getAdditionalMetadataLocation() : array
437
    {
438
        return $this->AdditionalMetadataLocation;
439
    }
440
441
442
    /**
443
     * Set the value of the AdditionalMetadataLocation property.
444
     *
445
     * @param array $additionalMetadataLocation
446
     * @return void
447
     */
448
    public function setAdditionalMetadataLocation(array $additionalMetadataLocation)
449
    {
450
        $this->AdditionalMetadataLocation = $additionalMetadataLocation;
451
    }
452
453
454
    /**
455
     * Add the value to the AdditionalMetadataLocation property.
456
     *
457
     * @param AdditionalMetadataLocation $additionalMetadataLocation
458
     * @return void
459
     */
460
    public function addAdditionalMetadataLocation(AdditionalMetadataLocation $additionalMetadataLocation)
461
    {
462
        $this->AdditionalMetadataLocation[] = $additionalMetadataLocation;
463
    }
464
465
466
    /**
467
     * Create this EntityDescriptor.
468
     *
469
     * @param \DOMElement|null $parent The EntitiesDescriptor we should append this EntityDescriptor to.
470
     * @return \DOMElement
471
     */
472
    public function toXML(\DOMElement $parent = null) : \DOMElement
473
    {
474
        if (empty($this->entityID)) {
475
            throw new \Exception('Cannot convert EntityDescriptor to XML without an EntityID set.');
476
        }
477
478
        if ($parent === null) {
479
            $doc = DOMDocumentFactory::create();
480
            $e = $doc->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
481
            $doc->appendChild($e);
482
        } else {
483
            $e = $parent->ownerDocument->createElementNS(Constants::NS_MD, 'md:EntityDescriptor');
484
            $parent->appendChild($e);
485
        }
486
487
        $e->setAttribute('entityID', $this->entityID);
488
489
        if ($this->ID !== null) {
490
            $e->setAttribute('ID', $this->ID);
491
        }
492
493
        if ($this->validUntil !== null) {
494
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
495
        }
496
497
        if ($this->cacheDuration !== null) {
498
            $e->setAttribute('cacheDuration', $this->cacheDuration);
499
        }
500
501
        Extensions::addList($e, $this->Extensions);
502
503
        foreach ($this->RoleDescriptor as $n) {
504
            $n->toXML($e);
505
        }
506
507
        if ($this->AffiliationDescriptor !== null) {
508
            $this->AffiliationDescriptor->toXML($e);
509
        }
510
511
        if ($this->Organization !== null) {
512
            $this->Organization->toXML($e);
513
        }
514
515
        foreach ($this->ContactPerson as $cp) {
516
            $cp->toXML($e);
517
        }
518
519
        foreach ($this->AdditionalMetadataLocation as $n) {
520
            $n->toXML($e);
521
        }
522
523
        /** @var \DOMElement $child */
524
        $child = $e->firstChild;
525
        $this->signElement($e, $child);
526
527
        return $e;
528
    }
529
}
530