Passed
Push — master ( 045039...d7835c )
by Tim
02:26
created

AuthnAuthorityDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 12
dl 0
loc 28
rs 9.8333
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\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
use SimpleSAML\XML\Utils as XMLUtils;
12
use SimpleSAML\XMLSecurity\XML\ds\Signature;
13
14
use function preg_split;
15
16
/**
17
 * Class representing SAML 2 metadata AuthnAuthorityDescriptor.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class AuthnAuthorityDescriptor extends AbstractRoleDescriptor
22
{
23
    /**
24
     * List of AuthnQueryService endpoints.
25
     *
26
     * @var \SimpleSAML\SAML2\XML\md\AbstractEndpointType[]
27
     */
28
    protected array $AuthnQueryServices = [];
29
30
    /**
31
     * List of AssertionIDRequestService endpoints.
32
     *
33
     * @var \SimpleSAML\SAML2\XML\md\AbstractEndpointType[]
34
     */
35
    protected array $AssertionIDRequestServices = [];
36
37
    /**
38
     * List of supported NameID formats.
39
     *
40
     * Array of strings.
41
     *
42
     * @var \SimpleSAML\SAML2\XML\md\NameIDFormat[]
43
     */
44
    protected array $NameIDFormats = [];
45
46
47
    /**
48
     * AuthnAuthorityDescriptor constructor.
49
     *
50
     * @param array $authnQueryServices
51
     * @param array $protocolSupportEnumeration
52
     * @param array $assertionIDRequestServices
53
     * @param array $nameIDFormats
54
     * @param string|null $ID
55
     * @param int|null $validUntil
56
     * @param string|null $cacheDuration
57
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
58
     * @param string|null $errorURL
59
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
60
     * @param array $keyDescriptors
61
     * @param array $contacts
62
     */
63
    public function __construct(
64
        array $authnQueryServices,
65
        array $protocolSupportEnumeration,
66
        array $assertionIDRequestServices = [],
67
        array $nameIDFormats = [],
68
        string $ID = null,
69
        ?int $validUntil = null,
70
        ?string $cacheDuration = null,
71
        ?Extensions $extensions = null,
72
        ?string $errorURL = null,
73
        ?Organization $organization = null,
74
        array $keyDescriptors = [],
75
        array $contacts = []
76
    ) {
77
        parent::__construct(
78
            $protocolSupportEnumeration,
79
            $ID,
80
            $validUntil,
81
            $cacheDuration,
82
            $extensions,
83
            $errorURL,
84
            $keyDescriptors,
85
            $organization,
86
            $contacts
87
        );
88
        $this->setAuthnQueryServices($authnQueryServices);
89
        $this->setAssertionIDRequestService($assertionIDRequestServices);
90
        $this->setNameIDFormat($nameIDFormats);
91
    }
92
93
94
    /**
95
     * Collect the AuthnQueryService endpoints
96
     *
97
     * @return \SimpleSAML\SAML2\XML\md\AbstractEndpointType[]
98
     */
99
    public function getAuthnQueryServices(): array
100
    {
101
        return $this->AuthnQueryServices;
102
    }
103
104
105
    /**
106
     * Set the AuthnQueryService endpoints
107
     *
108
     * @param \SimpleSAML\SAML2\XML\md\AbstractEndpointType[] $authnQueryServices
109
     * @throws \SimpleSAML\Assert\AssertionFailedException
110
     */
111
    protected function setAuthnQueryServices(array $authnQueryServices): void
112
    {
113
        Assert::minCount($authnQueryServices, 1, 'Missing at least one AuthnQueryService in AuthnAuthorityDescriptor.');
114
        Assert::allIsInstanceOf(
115
            $authnQueryServices,
116
            AbstractEndpointType::class,
117
            'AuthnQueryService must be an instance of EndpointType'
118
        );
119
        $this->AuthnQueryServices = $authnQueryServices;
120
    }
121
122
123
    /**
124
     * Collect the AssertionIDRequestService endpoints
125
     *
126
     * @return \SimpleSAML\SAML2\XML\md\AbstractEndpointType[]
127
     */
128
    public function getAssertionIDRequestServices(): array
129
    {
130
        return $this->AssertionIDRequestServices;
131
    }
132
133
134
    /**
135
     * Set the AssertionIDRequestService endpoints
136
     *
137
     * @param \SimpleSAML\SAML2\XML\md\AbstractEndpointType[] $assertionIDRequestServices
138
     * @throws \SimpleSAML\Assert\AssertionFailedException
139
     */
140
    protected function setAssertionIDRequestService(array $assertionIDRequestServices = []): void
141
    {
142
        Assert::allIsInstanceOf(
143
            $assertionIDRequestServices,
144
            AbstractEndpointType::class,
145
            'AssertionIDRequestServices must be an instance of EndpointType'
146
        );
147
        $this->AssertionIDRequestServices = $assertionIDRequestServices;
148
    }
149
150
151
    /**
152
     * Collect the values of the NameIDFormat
153
     *
154
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
155
     */
156
    public function getNameIDFormats(): array
157
    {
158
        return $this->NameIDFormats;
159
    }
160
161
162
    /**
163
     * Set the values of the NameIDFormat
164
     *
165
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
166
     * @throws \SimpleSAML\Assert\AssertionFailedException
167
     */
168
    protected function setNameIDFormat(array $nameIDFormats): void
169
    {
170
        Assert::allIsInstanceOf($nameIDFormats, NameIDFormat::class);
171
        $this->NameIDFormats = $nameIDFormats;
172
    }
173
174
175
    /**
176
     * Initialize an IDPSSODescriptor from an existing XML document.
177
     *
178
     * @param \DOMElement $xml The XML element we should load.
179
     * @return self
180
     *
181
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
182
     *   if the qualified name of the supplied element is wrong
183
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
184
     *   if the supplied element is missing one of the mandatory attributes
185
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
186
     *   if too many child-elements of a type are specified
187
     */
188
    public static function fromXML(DOMElement $xml): static
189
    {
190
        Assert::same($xml->localName, 'AuthnAuthorityDescriptor', InvalidDOMElementException::class);
191
        Assert::same($xml->namespaceURI, AuthnAuthorityDescriptor::NS, InvalidDOMElementException::class);
192
193
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
194
195
        $authnQueryServices = AuthnQueryService::getChildrenOfClass($xml);
196
        $assertionIDRequestServices = AssertionIDRequestService::getChildrenOfClass($xml);
197
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
198
199
        $validUntil = self::getAttribute($xml, 'validUntil', null);
200
201
        $orgs = Organization::getChildrenOfClass($xml);
202
        Assert::maxCount(
203
            $orgs,
204
            1,
205
            'More than one Organization found in this descriptor',
206
            TooManyElementsException::class
207
        );
208
209
        $extensions = Extensions::getChildrenOfClass($xml);
210
        Assert::maxCount(
211
            $extensions,
212
            1,
213
            'Only one md:Extensions element is allowed.',
214
            TooManyElementsException::class
215
        );
216
217
        $signature = Signature::getChildrenOfClass($xml);
218
        Assert::maxCount(
219
            $signature,
220
            1,
221
            'Only one ds:Signature element is allowed.',
222
            TooManyElementsException::class
223
        );
224
225
        $authority = new static(
226
            $authnQueryServices,
227
            preg_split('/[\s]+/', trim($protocols)),
228
            $assertionIDRequestServices,
229
            $nameIDFormats,
230
            self::getAttribute($xml, 'ID', null),
231
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
232
            self::getAttribute($xml, 'cacheDuration', null),
233
            !empty($extensions) ? $extensions[0] : null,
234
            self::getAttribute($xml, 'errorURL', null),
235
            !empty($orgs) ? $orgs[0] : null,
236
            KeyDescriptor::getChildrenOfClass($xml),
237
            ContactPerson::getChildrenOfClass($xml)
238
        );
239
        if (!empty($signature)) {
240
            $authority->setSignature($signature[0]);
241
            $authority->setXML($xml);
242
        }
243
        return $authority;
244
    }
245
246
247
    /**
248
     * Add this IDPSSODescriptor to an EntityDescriptor.
249
     *
250
     * @param \DOMElement|null $parent The EntityDescriptor we should append this AuthnAuthorityDescriptor to.
251
     *
252
     * @return \DOMElement
253
     * @throws \Exception
254
     */
255
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
256
    {
257
        $e = parent::toUnsignedXML($parent);
258
259
        foreach ($this->getAuthnQueryServices() as $ep) {
260
            $ep->toXML($e);
261
        }
262
263
        foreach ($this->getAssertionIDRequestServices() as $ep) {
264
            $ep->toXML($e);
265
        }
266
267
        foreach ($this->getNameIDFormats() as $nidFormat) {
268
            $nidFormat->toXML($e);
269
        }
270
271
        return $e;
272
    }
273
}
274