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