Passed
Pull Request — master (#374)
by Tim
02:32
created

AttributeAuthorityDescriptor::__construct()   A

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