Passed
Push — master ( e176b8...7304d0 )
by Tim
02:30
created

AttributeAuthorityDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
nc 1
nop 14
dl 0
loc 33
rs 9.7666
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\XML\saml\Attribute;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\Utils as XMLUtils;
14
use SimpleSAML\XMLSecurity\XML\ds\Signature;
15
16
use function preg_split;
17
18
/**
19
 * Class representing SAML 2 metadata AttributeAuthorityDescriptor.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class AttributeAuthorityDescriptor extends AbstractRoleDescriptor
24
{
25
    /**
26
     * List of AttributeService endpoints.
27
     *
28
     * Array with EndpointType objects.
29
     *
30
     * @var \SimpleSAML\SAML2\XML\md\AttributeService[]
31
     */
32
    protected array $AttributeServices = [];
33
34
    /**
35
     * List of AssertionIDRequestService endpoints.
36
     *
37
     * Array with EndpointType objects.
38
     *
39
     * @var \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
40
     */
41
    protected array $AssertionIDRequestServices = [];
42
43
    /**
44
     * List of supported NameID formats.
45
     *
46
     * Array of strings.
47
     *
48
     * @var \SimpleSAML\SAML2\XML\md\NameIDFormat[]
49
     */
50
    protected array $NameIDFormats = [];
51
52
    /**
53
     * List of supported attribute profiles.
54
     *
55
     * Array with strings.
56
     *
57
     * @var \SimpleSAML\SAML2\XML\md\AttributeProfile[]
58
     */
59
    protected array $AttributeProfiles = [];
60
61
    /**
62
     * List of supported attributes.
63
     *
64
     * Array with \SimpleSAML\SAML2\XML\saml\Attribute objects.
65
     *
66
     * @var Attribute[]
67
     */
68
    protected array $Attributes = [];
69
70
71
    /**
72
     * AttributeAuthorityDescriptor constructor.
73
     *
74
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeServices
75
     * @param string[] $protocolSupportEnumeration
76
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
77
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
78
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfiles
79
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attributes
80
     * @param string|null $ID
81
     * @param int|null $validUntil
82
     * @param string|null $cacheDuration
83
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
84
     * @param string|null $errorURL
85
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
86
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors
87
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contacts
88
     */
89
    public function __construct(
90
        array $attributeServices,
91
        array $protocolSupportEnumeration,
92
        array $assertionIDRequestService = [],
93
        array $nameIDFormats = [],
94
        array $attributeProfiles = [],
95
        array $attributes = [],
96
        ?string $ID = null,
97
        ?int $validUntil = null,
98
        ?string $cacheDuration = null,
99
        ?Extensions $extensions = null,
100
        ?string $errorURL = null,
101
        ?Organization $organization = null,
102
        array $keyDescriptors = [],
103
        array $contacts = []
104
    ) {
105
        parent::__construct(
106
            $protocolSupportEnumeration,
107
            $ID,
108
            $validUntil,
109
            $cacheDuration,
110
            $extensions,
111
            $errorURL,
112
            $keyDescriptors,
113
            $organization,
114
            $contacts
115
        );
116
117
        $this->setAttributeServices($attributeServices);
118
        $this->setAssertionIDRequestServices($assertionIDRequestService);
119
        $this->setNameIDFormats($nameIDFormats);
120
        $this->setAttributeProfiles($attributeProfiles);
121
        $this->setAttributes($attributes);
122
    }
123
124
125
    /**
126
     * Collect the value of the AttributeService-property
127
     *
128
     * @return \SimpleSAML\SAML2\XML\md\AttributeService[]
129
     */
130
    public function getAttributeServices(): array
131
    {
132
        return $this->AttributeServices;
133
    }
134
135
136
    /**
137
     * Set the value of the AttributeService-property
138
     *
139
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeServices
140
     */
141
    protected function setAttributeServices(array $attributeServices): void
142
    {
143
        Assert::minCount(
144
            $attributeServices,
145
            1,
146
            'AttributeAuthorityDescriptor must contain at least one AttributeService.',
147
            MissingElementException::class,
148
        );
149
        Assert::allIsInstanceOf(
150
            $attributeServices,
151
            AttributeService::class,
152
            'AttributeService is not an instance of EndpointType.',
153
            InvalidDOMElementException::class,
154
        );
155
        $this->AttributeServices = $attributeServices;
156
    }
157
158
159
    /**
160
     * Collect the value of the NameIDFormat-property
161
     *
162
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
163
     */
164
    public function getNameIDFormats(): array
165
    {
166
        return $this->NameIDFormats;
167
    }
168
169
170
    /**
171
     * Set the value of the NameIDFormat-property
172
     *
173
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
174
     */
175
    protected function setNameIDFormats(array $nameIDFormats): void
176
    {
177
        Assert::allIsInstanceOf($nameIDFormats, NameIDFormat::class);
178
        $this->NameIDFormats = $nameIDFormats;
179
    }
180
181
182
    /**
183
     * Collect the value of the AssertionIDRequestService-property
184
     *
185
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
186
     */
187
    public function getAssertionIDRequestServices(): array
188
    {
189
        return $this->AssertionIDRequestServices;
190
    }
191
192
193
    /**
194
     * Set the value of the AssertionIDRequestService-property
195
     *
196
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestServices
197
     */
198
    protected function setAssertionIDRequestServices(array $assertionIDRequestServices): void
199
    {
200
        Assert::allIsInstanceOf($assertionIDRequestServices, AssertionIDRequestService::class);
201
202
        $this->AssertionIDRequestServices = $assertionIDRequestServices;
203
    }
204
205
206
    /**
207
     * Collect the value of the AttributeProfile-property
208
     *
209
     * @return \SimpleSAML\SAML2\XML\md\AttributeProfile[]
210
     */
211
    public function getAttributeProfiles(): array
212
    {
213
        return $this->AttributeProfiles;
214
    }
215
216
217
    /**
218
     * Set the value of the AttributeProfile-property
219
     *
220
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfiles
221
     */
222
    protected function setAttributeProfiles(array $attributeProfiles): void
223
    {
224
        Assert::allIsInstanceOf($attributeProfiles, AttributeProfile::class);
225
        $this->AttributeProfiles = $attributeProfiles;
226
    }
227
228
229
    /**
230
     * Collect the value of the Attribute-property
231
     *
232
     * @return Attribute[]
233
     */
234
    public function getAttributes(): array
235
    {
236
        return $this->Attributes;
237
    }
238
239
240
    /**
241
     * Set the value of the Attribute-property
242
     *
243
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attributes
244
     */
245
    protected function setAttributes(?array $attributes): void
246
    {
247
        Assert::allIsInstanceOf($attributes, Attribute::class);
248
        $this->Attributes = $attributes;
249
    }
250
251
252
    /**
253
     * Initialize an IDPSSODescriptor.
254
     *
255
     * @param \DOMElement $xml The XML element we should load.
256
     * @return self
257
     *
258
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
259
     *   if the qualified name of the supplied element is wrong
260
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
261
     *   if the supplied element is missing one of the mandatory attributes
262
     * @throws \SimpleSAML\XML\Exception\MissingElementException
263
     *   if one of the mandatory child-elements is missing
264
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
265
     *   if too many child-elements of a type are specified
266
     */
267
    public static function fromXML(DOMElement $xml): static
268
    {
269
        Assert::same($xml->localName, 'AttributeAuthorityDescriptor', InvalidDOMElementException::class);
270
        Assert::same($xml->namespaceURI, AttributeAuthorityDescriptor::NS, InvalidDOMElementException::class);
271
272
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
273
        $validUntil = self::getAttribute($xml, 'validUntil', null);
274
275
        $attrServices = AttributeService::getChildrenOfClass($xml);
276
        Assert::notEmpty(
277
            $attrServices,
278
            'Must have at least one AttributeService in AttributeAuthorityDescriptor.',
279
            MissingElementException::class
280
        );
281
282
        $assertIDReqServices = AssertionIDRequestService::getChildrenOfClass($xml);
283
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
284
        $attrProfiles = AttributeProfile::getChildrenOfClass($xml);
285
        $attributes = Attribute::getChildrenOfClass($xml);
286
287
        $orgs = Organization::getChildrenOfClass($xml);
288
        Assert::maxCount(
289
            $orgs,
290
            1,
291
            'More than one Organization found in this descriptor',
292
            TooManyElementsException::class
293
        );
294
295
        $extensions = Extensions::getChildrenOfClass($xml);
296
        Assert::maxCount(
297
            $extensions,
298
            1,
299
            'Only one md:Extensions element is allowed.',
300
            TooManyElementsException::class
301
        );
302
303
        $signature = Signature::getChildrenOfClass($xml);
304
        Assert::maxCount(
305
            $signature,
306
            1,
307
            'Only one ds:Signature element is allowed.',
308
            TooManyElementsException::class
309
        );
310
311
        $authority = new static(
312
            $attrServices,
313
            preg_split('/[\s]+/', trim($protocols)),
314
            $assertIDReqServices,
315
            $nameIDFormats,
316
            $attrProfiles,
317
            $attributes,
318
            self::getAttribute($xml, 'ID', null),
319
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
320
            self::getAttribute($xml, 'cacheDuration', null),
321
            !empty($extensions) ? $extensions[0] : null,
322
            self::getAttribute($xml, 'errorURL', null),
323
            !empty($orgs) ? $orgs[0] : null,
324
            KeyDescriptor::getChildrenOfClass($xml),
325
            ContactPerson::getChildrenOfClass($xml)
326
        );
327
328
        if (!empty($signature)) {
329
            $authority->setSignature($signature[0]);
330
            $authority->setXML($xml);
331
        }
332
        return $authority;
333
    }
334
335
336
    /**
337
     * Convert this assertion to an unsigned XML document.
338
     * This method does not sign the resulting XML document.
339
     *
340
     * @return \DOMElement The root element of the DOM tree
341
     */
342
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
343
    {
344
        $e = parent::toUnsignedXML($parent);
345
346
        foreach ($this->getAttributeServices() as $ep) {
347
            $ep->toXML($e);
348
        }
349
350
        foreach ($this->getAssertionIDRequestServices() as $ep) {
351
            $ep->toXML($e);
352
        }
353
354
        foreach ($this->getNameIDFormats() as $nidFormat) {
355
            $nidFormat->toXML($e);
356
        }
357
358
        foreach ($this->getAttributeProfiles() as $ap) {
359
            $ap->toXML($e);
360
        }
361
362
        foreach ($this->getAttributes() as $a) {
363
            $a->toXML($e);
364
        }
365
366
        return $e;
367
    }
368
}
369