PDPDescriptor::__construct()   A
last analyzed

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