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

AuthnAuthorityDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 13
dl 0
loc 42
rs 9.536
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};
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\{InvalidDOMElementException, TooManyElementsException};
12
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
13
use SimpleSAML\XML\Type\{DurationValue, IDValue};
14
use SimpleSAML\XMLSecurity\XML\ds\Signature;
15
16
use function preg_split;
17
18
/**
19
 * Class representing SAML 2 metadata AuthnAuthorityDescriptor.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class AuthnAuthorityDescriptor extends AbstractRoleDescriptorType implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
26
27
    /**
28
     * AuthnAuthorityDescriptor constructor.
29
     *
30
     * @param array $authnQueryService
31
     * @param array $protocolSupportEnumeration
32
     * @param array $assertionIDRequestService
33
     * @param array $nameIDFormat
34
     * @param \SimpleSAML\XML\Type\IDValue|null $ID
35
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil
36
     * @param \SimpleSAML\XML\Type\DurationValue|null $cacheDuration
37
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
38
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $errorURL
39
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
40
     * @param array $keyDescriptor
41
     * @param array $contact
42
     * @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...
43
     */
44
    public function __construct(
45
        protected array $authnQueryService,
46
        array $protocolSupportEnumeration,
47
        protected array $assertionIDRequestService = [],
48
        protected array $nameIDFormat = [],
49
        ?IDValue $ID = null,
50
        ?SAMLDateTimeValue $validUntil = null,
51
        ?DurationValue $cacheDuration = null,
52
        ?Extensions $extensions = null,
53
        ?SAMLAnyURIValue $errorURL = null,
54
        ?Organization $organization = null,
55
        array $keyDescriptor = [],
56
        array $contact = [],
57
        array $namespacedAttributes = [],
58
    ) {
59
        Assert::maxCount($authnQueryService, C::UNBOUNDED_LIMIT);
60
        Assert::minCount($authnQueryService, 1, 'Missing at least one AuthnQueryService in AuthnAuthorityDescriptor.');
61
        Assert::allIsInstanceOf(
62
            $authnQueryService,
63
            AbstractEndpointType::class,
64
            'AuthnQueryService must be an instance of EndpointType',
65
        );
66
        Assert::maxCount($assertionIDRequestService, C::UNBOUNDED_LIMIT);
67
        Assert::allIsInstanceOf(
68
            $assertionIDRequestService,
69
            AbstractEndpointType::class,
70
            'AssertionIDRequestServices must be an instance of EndpointType',
71
        );
72
        Assert::maxCount($nameIDFormat, C::UNBOUNDED_LIMIT);
73
        Assert::allIsInstanceOf($nameIDFormat, NameIDFormat::class);
74
75
        parent::__construct(
76
            $protocolSupportEnumeration,
77
            $ID,
78
            $validUntil,
79
            $cacheDuration,
80
            $extensions,
81
            $errorURL,
82
            $keyDescriptor,
83
            $organization,
84
            $contact,
85
            $namespacedAttributes,
86
        );
87
    }
88
89
90
    /**
91
     * Collect the AuthnQueryService endpoints
92
     *
93
     * @return \SimpleSAML\SAML2\XML\md\AbstractEndpointType[]
94
     */
95
    public function getAuthnQueryService(): array
96
    {
97
        return $this->authnQueryService;
98
    }
99
100
101
    /**
102
     * Collect the AssertionIDRequestService endpoints
103
     *
104
     * @return \SimpleSAML\SAML2\XML\md\AbstractEndpointType[]
105
     */
106
    public function getAssertionIDRequestService(): array
107
    {
108
        return $this->assertionIDRequestService;
109
    }
110
111
112
    /**
113
     * Collect the values of the NameIDFormat
114
     *
115
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
116
     */
117
    public function getNameIDFormat(): array
118
    {
119
        return $this->nameIDFormat;
120
    }
121
122
123
    /**
124
     * Initialize an IDPSSODescriptor from an existing XML document.
125
     *
126
     * @param \DOMElement $xml The XML element we should load.
127
     * @return static
128
     *
129
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
130
     *   if the qualified name of the supplied element is wrong
131
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
132
     *   if the supplied element is missing one of the mandatory attributes
133
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
134
     *   if too many child-elements of a type are specified
135
     */
136
    public static function fromXML(DOMElement $xml): static
137
    {
138
        Assert::same($xml->localName, 'AuthnAuthorityDescriptor', InvalidDOMElementException::class);
139
        Assert::same($xml->namespaceURI, AuthnAuthorityDescriptor::NS, InvalidDOMElementException::class);
140
141
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
142
143
        $authnQueryServices = AuthnQueryService::getChildrenOfClass($xml);
144
        $assertionIDRequestServices = AssertionIDRequestService::getChildrenOfClass($xml);
145
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
146
147
        $orgs = Organization::getChildrenOfClass($xml);
148
        Assert::maxCount(
149
            $orgs,
150
            1,
151
            'More than one Organization found in this descriptor',
152
            TooManyElementsException::class,
153
        );
154
155
        $extensions = Extensions::getChildrenOfClass($xml);
156
        Assert::maxCount(
157
            $extensions,
158
            1,
159
            'Only one md:Extensions element is allowed.',
160
            TooManyElementsException::class,
161
        );
162
163
        $signature = Signature::getChildrenOfClass($xml);
164
        Assert::maxCount(
165
            $signature,
166
            1,
167
            'Only one ds:Signature element is allowed.',
168
            TooManyElementsException::class,
169
        );
170
171
        $authority = new static(
172
            $authnQueryServices,
173
            preg_split('/[\s]+/', trim($protocols->getValue())),
174
            $assertionIDRequestServices,
175
            $nameIDFormats,
176
            self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
177
            self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
178
            self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
179
            !empty($extensions) ? $extensions[0] : null,
180
            self::getOptionalAttribute($xml, 'errorURL', SAMLAnyURIValue::class, null),
181
            !empty($orgs) ? $orgs[0] : null,
182
            KeyDescriptor::getChildrenOfClass($xml),
183
            ContactPerson::getChildrenOfClass($xml),
184
            self::getAttributesNSFromXML($xml),
185
        );
186
187
        if (!empty($signature)) {
188
            $authority->setSignature($signature[0]);
189
            $authority->setXML($xml);
190
        }
191
192
        return $authority;
193
    }
194
195
196
    /**
197
     * Add this IDPSSODescriptor to an EntityDescriptor.
198
     *
199
     * @param \DOMElement|null $parent The EntityDescriptor we should append this AuthnAuthorityDescriptor to.
200
     *
201
     * @return \DOMElement
202
     * @throws \Exception
203
     */
204
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
205
    {
206
        $e = parent::toUnsignedXML($parent);
207
208
        foreach ($this->getAuthnQueryService() as $ep) {
209
            $ep->toXML($e);
210
        }
211
212
        foreach ($this->getAssertionIDRequestService() as $ep) {
213
            $ep->toXML($e);
214
        }
215
216
        foreach ($this->getNameIDFormat() as $nidFormat) {
217
            $nidFormat->toXML($e);
218
        }
219
220
        return $e;
221
    }
222
}
223