AttributeAuthorityDescriptor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 29
nc 1
nop 15
dl 0
loc 50
rs 9.456
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 DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
11
use SimpleSAML\SAML2\XML\saml\Attribute;
12
use SimpleSAML\XML\Constants as C;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\MissingElementException;
15
use SimpleSAML\XML\Exception\TooManyElementsException;
16
use SimpleSAML\XML\SchemaValidatableElementInterface;
17
use SimpleSAML\XML\SchemaValidatableElementTrait;
18
use SimpleSAML\XMLSecurity\XML\ds\Signature;
19
20
use function preg_split;
21
22
/**
23
 * Class representing SAML 2 metadata AttributeAuthorityDescriptor.
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
final class AttributeAuthorityDescriptor extends AbstractRoleDescriptorType implements
28
    SchemaValidatableElementInterface
29
{
30
    use SchemaValidatableElementTrait;
31
32
33
    /**
34
     * AttributeAuthorityDescriptor constructor.
35
     *
36
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeService
37
     * @param string[] $protocolSupportEnumeration
38
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
39
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormat
40
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfile
41
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attribute
42
     * @param string|null $ID
43
     * @param \DateTimeImmutable|null $validUntil
44
     * @param string|null $cacheDuration
45
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
46
     * @param string|null $errorURL
47
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
48
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
49
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contact
50
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
     */
52
    public function __construct(
53
        protected array $attributeService,
54
        array $protocolSupportEnumeration,
55
        protected array $assertionIDRequestService = [],
56
        protected array $nameIDFormat = [],
57
        protected array $attributeProfile = [],
58
        protected array $attribute = [],
59
        ?string $ID = null,
60
        ?DateTimeImmutable $validUntil = null,
61
        ?string $cacheDuration = null,
62
        ?Extensions $extensions = null,
63
        ?string $errorURL = null,
64
        ?Organization $organization = null,
65
        array $keyDescriptor = [],
66
        array $contact = [],
67
        array $namespacedAttributes = [],
68
    ) {
69
        Assert::maxCount($attributeService, C::UNBOUNDED_LIMIT);
70
        Assert::minCount(
71
            $attributeService,
72
            1,
73
            'AttributeAuthorityDescriptor must contain at least one AttributeService.',
74
            MissingElementException::class,
75
        );
76
        Assert::allIsInstanceOf(
77
            $attributeService,
78
            AttributeService::class,
79
            'AttributeService is not an instance of EndpointType.',
80
            InvalidDOMElementException::class,
81
        );
82
        Assert::maxCount($nameIDFormat, C::UNBOUNDED_LIMIT);
83
        Assert::allIsInstanceOf($nameIDFormat, NameIDFormat::class);
84
        Assert::maxCount($assertionIDRequestService, C::UNBOUNDED_LIMIT);
85
        Assert::allIsInstanceOf($assertionIDRequestService, AssertionIDRequestService::class);
86
        Assert::maxCount($attributeProfile, C::UNBOUNDED_LIMIT);
87
        Assert::allIsInstanceOf($attributeProfile, AttributeProfile::class);
88
        Assert::maxCount($attribute, C::UNBOUNDED_LIMIT);
89
        Assert::allIsInstanceOf($attribute, Attribute::class);
90
91
        parent::__construct(
92
            $protocolSupportEnumeration,
93
            $ID,
94
            $validUntil,
95
            $cacheDuration,
96
            $extensions,
97
            $errorURL,
98
            $keyDescriptor,
99
            $organization,
100
            $contact,
101
            $namespacedAttributes,
102
        );
103
    }
104
105
106
    /**
107
     * Collect the value of the AttributeService-property
108
     *
109
     * @return \SimpleSAML\SAML2\XML\md\AttributeService[]
110
     */
111
    public function getAttributeService(): array
112
    {
113
        return $this->attributeService;
114
    }
115
116
117
    /**
118
     * Collect the value of the NameIDFormat-property
119
     *
120
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
121
     */
122
    public function getNameIDFormat(): array
123
    {
124
        return $this->nameIDFormat;
125
    }
126
127
128
    /**
129
     * Collect the value of the AssertionIDRequestService-property
130
     *
131
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
132
     */
133
    public function getAssertionIDRequestService(): array
134
    {
135
        return $this->assertionIDRequestService;
136
    }
137
138
139
    /**
140
     * Collect the value of the AttributeProfile-property
141
     *
142
     * @return \SimpleSAML\SAML2\XML\md\AttributeProfile[]
143
     */
144
    public function getAttributeProfile(): array
145
    {
146
        return $this->attributeProfile;
147
    }
148
149
150
    /**
151
     * Collect the value of the Attribute-property
152
     *
153
     * @return \SimpleSAML\SAML2\XML\saml\Attribute[]
154
     */
155
    public function getAttributes(): array
156
    {
157
        return $this->attribute;
158
    }
159
160
161
    /**
162
     * Initialize an IDPSSODescriptor.
163
     *
164
     * @param \DOMElement $xml The XML element we should load.
165
     * @return static
166
     *
167
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
168
     *   if the qualified name of the supplied element is wrong
169
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
170
     *   if the supplied element is missing one of the mandatory attributes
171
     * @throws \SimpleSAML\XML\Exception\MissingElementException
172
     *   if one of the mandatory child-elements is missing
173
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
174
     *   if too many child-elements of a type are specified
175
     */
176
    public static function fromXML(DOMElement $xml): static
177
    {
178
        Assert::same($xml->localName, 'AttributeAuthorityDescriptor', InvalidDOMElementException::class);
179
        Assert::same($xml->namespaceURI, AttributeAuthorityDescriptor::NS, InvalidDOMElementException::class);
180
181
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
182
        $validUntil = self::getOptionalAttribute($xml, 'validUntil', null);
183
        SAMLAssert::nullOrValidDateTime($validUntil);
184
185
        $attrServices = AttributeService::getChildrenOfClass($xml);
186
        Assert::notEmpty(
187
            $attrServices,
188
            'Must have at least one AttributeService in AttributeAuthorityDescriptor.',
189
            MissingElementException::class,
190
        );
191
192
        $assertIDReqServices = AssertionIDRequestService::getChildrenOfClass($xml);
193
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
194
        $attrProfiles = AttributeProfile::getChildrenOfClass($xml);
195
        $attributes = Attribute::getChildrenOfClass($xml);
196
197
        $orgs = Organization::getChildrenOfClass($xml);
198
        Assert::maxCount(
199
            $orgs,
200
            1,
201
            'More than one Organization found in this descriptor',
202
            TooManyElementsException::class,
203
        );
204
205
        $extensions = Extensions::getChildrenOfClass($xml);
206
        Assert::maxCount(
207
            $extensions,
208
            1,
209
            'Only one md:Extensions element is allowed.',
210
            TooManyElementsException::class,
211
        );
212
213
        $signature = Signature::getChildrenOfClass($xml);
214
        Assert::maxCount(
215
            $signature,
216
            1,
217
            'Only one ds:Signature element is allowed.',
218
            TooManyElementsException::class,
219
        );
220
221
        $authority = new static(
222
            $attrServices,
223
            preg_split('/[\s]+/', trim($protocols)),
224
            $assertIDReqServices,
225
            $nameIDFormats,
226
            $attrProfiles,
227
            $attributes,
228
            self::getOptionalAttribute($xml, 'ID', null),
229
            $validUntil !== null ? new DateTimeImmutable($validUntil) : null,
230
            self::getOptionalAttribute($xml, 'cacheDuration', null),
231
            !empty($extensions) ? $extensions[0] : null,
232
            self::getOptionalAttribute($xml, 'errorURL', null),
233
            !empty($orgs) ? $orgs[0] : null,
234
            KeyDescriptor::getChildrenOfClass($xml),
235
            ContactPerson::getChildrenOfClass($xml),
236
            self::getAttributesNSFromXML($xml),
237
        );
238
239
        if (!empty($signature)) {
240
            $authority->setSignature($signature[0]);
241
            $authority->setXML($xml);
242
        }
243
        return $authority;
244
    }
245
246
247
    /**
248
     * Convert this assertion to an unsigned XML document.
249
     * This method does not sign the resulting XML document.
250
     *
251
     * @return \DOMElement The root element of the DOM tree
252
     */
253
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
254
    {
255
        $e = parent::toUnsignedXML($parent);
256
257
        foreach ($this->getAttributeService() as $ep) {
258
            $ep->toXML($e);
259
        }
260
261
        foreach ($this->getAssertionIDRequestService() as $ep) {
262
            $ep->toXML($e);
263
        }
264
265
        foreach ($this->getNameIDFormat() as $nidFormat) {
266
            $nidFormat->toXML($e);
267
        }
268
269
        foreach ($this->getAttributeProfile() as $ap) {
270
            $ap->toXML($e);
271
        }
272
273
        foreach ($this->getAttributes() as $a) {
274
            $a->toXML($e);
275
        }
276
277
        return $e;
278
    }
279
}
280