Passed
Pull Request — master (#317)
by Tim
12:30
created

AuthnAuthorityDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

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