EntityDescriptor::getCacheDuration()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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\Context\DeserializationContext;
16
use LightSaml\Model\Context\SerializationContext;
17
use LightSaml\Model\XmlDSig\Signature;
18
use LightSaml\SamlConstants;
19
20
class EntityDescriptor extends Metadata
21
{
22
    /** @var string */
23
    protected $entityID;
24
25
    /** @var int|null */
26
    protected $validUntil;
27
28
    /** @var string|null */
29
    protected $cacheDuration;
30
31
    /** @var string|null */
32
    protected $id;
33
34
    /** @var Signature|null */
35
    protected $signature;
36
37
    /** @var IdpSsoDescriptor[]|SpSsoDescriptor[] */
38
    protected $items;
39
40
    /** @var Organization[]|null */
41
    protected $organizations;
42
43
    /** @var ContactPerson[]|null */
44
    protected $contactPersons;
45
46
    /**
47
     * @param string $filename
48
     *
49
     * @return EntityDescriptor
50
     */
51
    public static function load($filename)
52
    {
53
        return self::loadXml(file_get_contents($filename));
54
    }
55
56
    /**
57
     * @param string $xml
58
     *
59
     * @return EntityDescriptor
60
     */
61
    public static function loadXml($xml)
62
    {
63
        $context = new DeserializationContext();
64
        $context->getDocument()->loadXML($xml);
65
        $ed = new self();
66
        $ed->deserialize($context->getDocument(), $context);
67
68
        return $ed;
69
    }
70
71
    /**
72
     * @param string|null $entityId
73
     */
74
    public function __construct($entityId = null, array $items = [])
75
    {
76
        $this->entityID = $entityId;
77
        $this->items = $items;
78
    }
79
80
    /**
81
     * @return EntityDescriptor
82
     */
83
    public function addContactPerson(ContactPerson $contactPerson)
84
    {
85
        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...
86
            $this->contactPersons = [];
87
        }
88
        $this->contactPersons[] = $contactPerson;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return ContactPerson[]|null
95
     */
96
    public function getAllContactPersons()
97
    {
98
        return $this->contactPersons;
99
    }
100
101
    /**
102
     * @return ContactPerson|null
103
     */
104
    public function getFirstContactPerson()
105
    {
106
        if (is_array($this->contactPersons) && isset($this->contactPersons[0])) {
107
            return $this->contactPersons[0];
108
        }
109
110
        return null;
111
    }
112
113
    /**
114
     * @param \LightSaml\Model\Metadata\Organization $organization
115
     *
116
     * @return EntityDescriptor
117
     */
118
    public function addOrganization(Organization $organization)
119
    {
120
        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...
121
            $this->organizations = [];
122
        }
123
        $this->organizations[] = $organization;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return Organization[]|null
130
     */
131
    public function getAllOrganizations()
132
    {
133
        return $this->organizations;
134
    }
135
136
    /**
137
     * @return \LightSaml\Model\Metadata\Organization|null
138
     */
139
    public function getFirstOrganization()
140
    {
141
        if (is_array($this->organizations) && isset($this->organizations[0])) {
142
            return $this->organizations[0];
143
        }
144
145
        return null;
146
    }
147
148
    /**
149
     * @param string|null $cacheDuration
150
     *
151
     * @throws \InvalidArgumentException
152
     *
153
     * @return EntityDescriptor
154
     */
155
    public function setCacheDuration($cacheDuration)
156
    {
157
        Helper::validateDurationString($cacheDuration);
158
159
        $this->cacheDuration = $cacheDuration;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return string|null
166
     */
167
    public function getCacheDuration()
168
    {
169
        return $this->cacheDuration;
170
    }
171
172
    /**
173
     * @param string $entityID
174
     *
175
     * @return EntityDescriptor
176
     */
177
    public function setEntityID($entityID)
178
    {
179
        $this->entityID = (string) $entityID;
180
181
        return $this;
182
    }
183
184
    /**
185
     * @return string
186
     */
187
    public function getEntityID()
188
    {
189
        return $this->entityID;
190
    }
191
192
    /**
193
     * @param string|null $id
194
     *
195
     * @return EntityDescriptor
196
     */
197
    public function setID($id)
198
    {
199
        $this->id = null !== $id ? (string) $id : null;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return string|null
206
     */
207
    public function getID()
208
    {
209
        return $this->id;
210
    }
211
212
    /**
213
     * @param \LightSaml\Model\Metadata\IdpSsoDescriptor|\LightSaml\Model\Metadata\SpSsoDescriptor $item
214
     *
215
     * @throws \InvalidArgumentException
216
     *
217
     * @return EntityDescriptor
218
     */
219
    public function addItem($item)
220
    {
221
        if (false == $item instanceof IdpSsoDescriptor &&
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...
222
            false == $item instanceof SpSsoDescriptor
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...
223
        ) {
224
            throw new \InvalidArgumentException('EntityDescriptor item must be IdpSsoDescriptor or SpSsoDescriptor');
225
        }
226
227
        if (false == is_array($this->items)) {
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...
introduced by
The condition false == is_array($this->items) is always false.
Loading history...
228
            $this->items = [];
229
        }
230
231
        $this->items[] = $item;
232
233
        return $this;
234
    }
235
236
    /**
237
     * @return IdpSsoDescriptor[]|SpSsoDescriptor[]|SSODescriptor[]
238
     */
239
    public function getAllItems()
240
    {
241
        return $this->items;
242
    }
243
244
    /**
245
     * @return IdpSsoDescriptor[]
246
     */
247
    public function getAllIdpSsoDescriptors()
248
    {
249
        $result = [];
250
        foreach ($this->getAllItems() as $item) {
251
            if ($item instanceof IdpSsoDescriptor) {
252
                $result[] = $item;
253
            }
254
        }
255
256
        return $result;
257
    }
258
259
    /**
260
     * @return SpSsoDescriptor[]
261
     */
262
    public function getAllSpSsoDescriptors()
263
    {
264
        $result = [];
265
        foreach ($this->getAllItems() as $item) {
266
            if ($item instanceof SpSsoDescriptor) {
267
                $result[] = $item;
268
            }
269
        }
270
271
        return $result;
272
    }
273
274
    /**
275
     * @return IdpSsoDescriptor|null
276
     */
277
    public function getFirstIdpSsoDescriptor()
278
    {
279
        foreach ($this->getAllItems() as $item) {
280
            if ($item instanceof IdpSsoDescriptor) {
281
                return $item;
282
            }
283
        }
284
285
        return null;
286
    }
287
288
    /**
289
     * @return SpSsoDescriptor|null
290
     */
291
    public function getFirstSpSsoDescriptor()
292
    {
293
        foreach ($this->getAllItems() as $item) {
294
            if ($item instanceof SpSsoDescriptor) {
295
                return $item;
296
            }
297
        }
298
299
        return null;
300
    }
301
302
    /**
303
     * @param Signature|null $signature
304
     *
305
     * @return EntityDescriptor
306
     */
307
    public function setSignature(Signature $signature)
308
    {
309
        $this->signature = $signature;
310
311
        return $this;
312
    }
313
314
    /**
315
     * @return Signature|null
316
     */
317
    public function getSignature()
318
    {
319
        return $this->signature;
320
    }
321
322
    /**
323
     * @param int $validUntil
324
     *
325
     * @return EntityDescriptor
326
     */
327
    public function setValidUntil($validUntil)
328
    {
329
        $this->validUntil = Helper::getTimestampFromValue($validUntil);
330
331
        return $this;
332
    }
333
334
    /**
335
     * @return int|null
336
     */
337
    public function getValidUntilTimestamp()
338
    {
339
        return $this->validUntil;
340
    }
341
342
    /**
343
     * @return string|null
344
     */
345
    public function getValidUntilString()
346
    {
347
        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...
348
            return Helper::time2string($this->validUntil);
349
        }
350
351
        return null;
352
    }
353
354
    /**
355
     * @return \DateTime|null
356
     */
357
    public function getValidUntilDateTime()
358
    {
359
        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...
360
            return new \DateTime('@'.$this->validUntil);
361
        }
362
363
        return null;
364
    }
365
366
    /**
367
     * @return array|KeyDescriptor[]
368
     */
369
    public function getAllIdpKeyDescriptors()
370
    {
371
        $result = [];
372
        foreach ($this->getAllIdpSsoDescriptors() as $idp) {
373
            foreach ($idp->getAllKeyDescriptors() as $key) {
374
                $result[] = $key;
375
            }
376
        }
377
378
        return $result;
379
    }
380
381
    /**
382
     * @return array|KeyDescriptor[]
383
     */
384
    public function getAllSpKeyDescriptors()
385
    {
386
        $result = [];
387
        foreach ($this->getAllSpSsoDescriptors() as $sp) {
388
            foreach ($sp->getAllKeyDescriptors() as $key) {
389
                $result[] = $key;
390
            }
391
        }
392
393
        return $result;
394
    }
395
396
    /**
397
     * @return EndpointReference[]
398
     */
399
    public function getAllEndpoints()
400
    {
401
        $result = [];
402
        foreach ($this->getAllIdpSsoDescriptors() as $idpSsoDescriptor) {
403
            foreach ($idpSsoDescriptor->getAllSingleSignOnServices() as $sso) {
404
                $result[] = new EndpointReference($this, $idpSsoDescriptor, $sso);
405
            }
406
            foreach ($idpSsoDescriptor->getAllSingleLogoutServices() as $slo) {
407
                $result[] = new EndpointReference($this, $idpSsoDescriptor, $slo);
408
            }
409
        }
410
        foreach ($this->getAllSpSsoDescriptors() as $spSsoDescriptor) {
411
            foreach ($spSsoDescriptor->getAllAssertionConsumerServices() as $acs) {
412
                $result[] = new EndpointReference($this, $spSsoDescriptor, $acs);
413
            }
414
            foreach ($spSsoDescriptor->getAllSingleLogoutServices() as $slo) {
415
                $result[] = new EndpointReference($this, $spSsoDescriptor, $slo);
416
            }
417
        }
418
419
        return $result;
420
    }
421
422
    /**
423
     * @return void
424
     */
425
    public function serialize(\DOMNode $parent, SerializationContext $context)
426
    {
427
        $result = $this->createElement('EntityDescriptor', SamlConstants::NS_METADATA, $parent, $context);
428
429
        $this->attributesToXml(['entityID', 'validUntil', 'cacheDuration', 'ID'], $result);
430
431
        $this->manyElementsToXml($this->getAllItems(), $result, $context, null);
432
        if ($this->organizations) {
433
            $this->manyElementsToXml($this->organizations, $result, $context, null);
434
        }
435
        if ($this->contactPersons) {
436
            $this->manyElementsToXml($this->contactPersons, $result, $context, null);
437
        }
438
439
        $this->singleElementsToXml(['Signature'], $result, $context);
440
    }
441
442
    public function deserialize(\DOMNode $node, DeserializationContext $context)
443
    {
444
        $this->checkXmlNodeName($node, 'EntityDescriptor', SamlConstants::NS_METADATA);
445
446
        $this->attributesFromXml($node, ['entityID', 'validUntil', 'cacheDuration', 'ID']);
447
448
        $this->items = [];
449
450
        $this->manyElementsFromXml(
451
            $node,
452
            $context,
453
            'IDPSSODescriptor',
454
            'md',
455
            'LightSaml\Model\Metadata\IdpSsoDescriptor',
456
            'addItem'
457
        );
458
459
        $this->manyElementsFromXml(
460
            $node,
461
            $context,
462
            'SPSSODescriptor',
463
            'md',
464
            'LightSaml\Model\Metadata\SpSsoDescriptor',
465
            'addItem'
466
        );
467
468
        $this->manyElementsFromXml(
469
            $node,
470
            $context,
471
            'Organization',
472
            'md',
473
            'LightSaml\Model\Metadata\Organization',
474
            'addOrganization'
475
        );
476
477
        $this->manyElementsFromXml(
478
            $node,
479
            $context,
480
            'ContactPerson',
481
            'md',
482
            'LightSaml\Model\Metadata\ContactPerson',
483
            'addContactPerson'
484
        );
485
486
        $this->singleElementsFromXml($node, $context, [
487
            'Signature' => ['ds', 'LightSaml\Model\XmlDSig\SignatureXmlReader'],
488
        ]);
489
    }
490
}
491