EntitiesDescriptor   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 308
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 39
eloc 90
c 2
b 1
f 0
dl 0
loc 308
rs 9.28

21 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllItems() 0 3 1
A setID() 0 5 1
A getName() 0 3 1
A loadXml() 0 8 1
A load() 0 3 1
A getSignature() 0 3 1
A deserialize() 0 25 1
A setCacheDuration() 0 7 1
A serialize() 0 9 1
A getAllEntityDescriptors() 0 12 3
A getByEntityId() 0 9 3
B containsItem() 0 17 7
A setSignature() 0 5 1
A getValidUntilDateTime() 0 7 2
A getID() 0 3 1
A setName() 0 5 1
A getValidUntilString() 0 7 2
A getValidUntilTimestamp() 0 3 1
A getCacheDuration() 0 3 1
A setValidUntil() 0 9 2
A addItem() 0 16 6
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 EntitiesDescriptor extends Metadata
21
{
22
    /** @var int */
23
    protected $validUntil;
24
25
    /** @var string */
26
    protected $cacheDuration;
27
28
    /** @var string */
29
    protected $id;
30
31
    /** @var string */
32
    protected $name;
33
34
    /** @var Signature */
35
    protected $signature;
36
37
    /** @var EntitiesDescriptor[]|EntityDescriptor[] */
38
    protected $items = [];
39
40
    /**
41
     * @param string $filename
42
     *
43
     * @return EntitiesDescriptor
44
     */
45
    public static function load($filename)
46
    {
47
        return self::loadXml(file_get_contents($filename));
48
    }
49
50
    /**
51
     * @param string $xml
52
     *
53
     * @return EntitiesDescriptor
54
     */
55
    public static function loadXml($xml)
56
    {
57
        $context = new DeserializationContext();
58
        $context->getDocument()->loadXML($xml);
59
        $ed = new self();
60
        $ed->deserialize($context->getDocument(), $context);
61
62
        return $ed;
63
    }
64
65
    /**
66
     * @param string $cacheDuration
67
     *
68
     * @return EntitiesDescriptor
69
     *
70
     * @throws \InvalidArgumentException
71
     */
72
    public function setCacheDuration($cacheDuration)
73
    {
74
        Helper::validateDurationString($cacheDuration);
75
76
        $this->cacheDuration = $cacheDuration;
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return string
83
     */
84
    public function getCacheDuration()
85
    {
86
        return $this->cacheDuration;
87
    }
88
89
    /**
90
     * @param string $id
91
     *
92
     * @return EntitiesDescriptor
93
     */
94
    public function setID($id)
95
    {
96
        $this->id = (string) $id;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getID()
105
    {
106
        return $this->id;
107
    }
108
109
    /**
110
     * @param string $name
111
     *
112
     * @return EntitiesDescriptor
113
     */
114
    public function setName($name)
115
    {
116
        $this->name = (string) $name;
117
118
        return $this;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getName()
125
    {
126
        return $this->name;
127
    }
128
129
    /**
130
     * @return EntitiesDescriptor
131
     */
132
    public function setSignature(Signature $signature)
133
    {
134
        $this->signature = $signature;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @return \LightSaml\Model\XmlDSig\Signature
141
     */
142
    public function getSignature()
143
    {
144
        return $this->signature;
145
    }
146
147
    /**
148
     * @param int|string $validUntil
149
     *
150
     * @return EntitiesDescriptor
151
     *
152
     * @throws \InvalidArgumentException
153
     */
154
    public function setValidUntil($validUntil)
155
    {
156
        $value = Helper::getTimestampFromValue($validUntil);
157
        if ($value < 0) {
158
            throw new \InvalidArgumentException('Invalid validUntil');
159
        }
160
        $this->validUntil = $value;
161
162
        return $this;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getValidUntilString()
169
    {
170
        if ($this->validUntil) {
171
            return Helper::time2string($this->validUntil);
172
        }
173
174
        return null;
175
    }
176
177
    /**
178
     * @return int
179
     */
180
    public function getValidUntilTimestamp()
181
    {
182
        return $this->validUntil;
183
    }
184
185
    /**
186
     * @return \DateTime|null
187
     */
188
    public function getValidUntilDateTime()
189
    {
190
        if ($this->validUntil) {
191
            return new \DateTime('@'.$this->validUntil);
192
        }
193
194
        return null;
195
    }
196
197
    /**
198
     * @param EntitiesDescriptor|EntityDescriptor $item
199
     *
200
     * @return EntitiesDescriptor
201
     *
202
     * @throws \InvalidArgumentException
203
     */
204
    public function addItem($item)
205
    {
206
        if (false == $item instanceof self && false == $item instanceof EntityDescriptor) {
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...
207
            throw new \InvalidArgumentException('Expected EntitiesDescriptor or EntityDescriptor');
208
        }
209
        if ($item === $this) {
210
            throw new \InvalidArgumentException('Circular reference detected');
211
        }
212
        if ($item instanceof self) {
213
            if ($item->containsItem($this)) {
214
                throw new \InvalidArgumentException('Circular reference detected');
215
            }
216
        }
217
        $this->items[] = $item;
218
219
        return $this;
220
    }
221
222
    /**
223
     * @param EntitiesDescriptor|EntityDescriptor $item
224
     *
225
     * @return bool
226
     *
227
     * @throws \InvalidArgumentException
228
     */
229
    public function containsItem($item)
230
    {
231
        if (false == $item instanceof self && false == $item instanceof EntityDescriptor) {
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...
232
            throw new \InvalidArgumentException('Expected EntitiesDescriptor or EntityDescriptor');
233
        }
234
        foreach ($this->items as $i) {
235
            if ($i === $item) {
236
                return true;
237
            }
238
            if ($i instanceof self) {
239
                if ($i->containsItem($item)) {
240
                    return true;
241
                }
242
            }
243
        }
244
245
        return false;
246
    }
247
248
    /**
249
     * @return EntitiesDescriptor[]|EntityDescriptor[]
250
     */
251
    public function getAllItems()
252
    {
253
        return $this->items;
254
    }
255
256
    /**
257
     * @return EntityDescriptor[]
258
     */
259
    public function getAllEntityDescriptors()
260
    {
261
        $result = [];
262
        foreach ($this->items as $item) {
263
            if ($item instanceof self) {
264
                $result = array_merge($result, $item->getAllEntityDescriptors());
265
            } else {
266
                $result[] = $item;
267
            }
268
        }
269
270
        return $result;
271
    }
272
273
    /**
274
     * @param string $entityId
275
     *
276
     * @return EntityDescriptor|null
277
     */
278
    public function getByEntityId($entityId)
279
    {
280
        foreach ($this->getAllEntityDescriptors() as $entityDescriptor) {
281
            if ($entityDescriptor->getEntityID() == $entityId) {
282
                return $entityDescriptor;
283
            }
284
        }
285
286
        return null;
287
    }
288
289
    /**
290
     * @return void
291
     */
292
    public function serialize(\DOMNode $parent, SerializationContext $context)
293
    {
294
        $result = $this->createElement('EntitiesDescriptor', SamlConstants::NS_METADATA, $parent, $context);
295
296
        $this->attributesToXml(['validUntil', 'cacheDuration', 'ID', 'Name'], $result);
297
298
        $this->singleElementsToXml(['Signature'], $result, $context);
299
300
        $this->manyElementsToXml($this->getAllItems(), $result, $context);
301
    }
302
303
    public function deserialize(\DOMNode $node, DeserializationContext $context)
304
    {
305
        $this->checkXmlNodeName($node, 'EntitiesDescriptor', SamlConstants::NS_METADATA);
306
307
        $this->attributesFromXml($node, ['validUntil', 'cacheDuration', 'ID', 'Name']);
308
309
        $this->singleElementsFromXml($node, $context, [
310
            'Signature' => ['ds', 'LightSaml\Model\XmlDSig\SignatureXmlReader'],
311
        ]);
312
313
        $this->manyElementsFromXml(
314
            $node,
315
            $context,
316
            'EntityDescriptor',
317
            'md',
318
            'LightSaml\Model\Metadata\EntityDescriptor',
319
            'addItem'
320
        );
321
        $this->manyElementsFromXml(
322
            $node,
323
            $context,
324
            'EntitiesDescriptor',
325
            'md',
326
            'LightSaml\Model\Metadata\EntitiesDescriptor',
327
            'addItem'
328
        );
329
    }
330
}
331