Passed
Pull Request — master (#280)
by Tim
02:13
created

AbstractRoleDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 10
dl 0
loc 19
rs 10
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 InvalidArgumentException;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML2\Constants;
11
12
use function filter_var;
13
use function implode;
14
use function is_null;
15
16
/**
17
 * Class representing SAML 2 RoleDescriptor element.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
abstract class AbstractRoleDescriptor extends AbstractMetadataDocument
22
{
23
    /**
24
     * List of supported protocols.
25
     *
26
     * @var string[]
27
     */
28
    protected array $protocolSupportEnumeration = [];
29
30
    /**
31
     * Error URL for this role.
32
     *
33
     * @var string|null
34
     */
35
    protected ?string $errorURL = null;
36
37
    /**
38
     * KeyDescriptor elements.
39
     *
40
     * Array of \SimpleSAML\SAML2\XML\md\KeyDescriptor elements.
41
     *
42
     * @var \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
43
     */
44
    protected array $KeyDescriptors = [];
45
46
    /**
47
     * Organization of this role.
48
     *
49
     * @var \SimpleSAML\SAML2\XML\md\Organization|null
50
     */
51
    protected ?Organization $Organization = null;
52
53
    /**
54
     * ContactPerson elements for this role.
55
     *
56
     * Array of \SimpleSAML\SAML2\XML\md\ContactPerson objects.
57
     *
58
     * @var \SimpleSAML\SAML2\XML\md\ContactPerson[]
59
     */
60
    protected array $ContactPersons = [];
61
62
63
    /**
64
     * Initialize a RoleDescriptor.
65
     *
66
     * @param string[] $protocolSupportEnumeration A set of URI specifying the protocols supported.
67
     * @param string|null $ID The ID for this document. Defaults to null.
68
     * @param int|null $validUntil Unix time of validity for this document. Defaults to null.
69
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
70
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An Extensions object. Defaults to null.
71
     * @param string|null $errorURL An URI where to redirect users for support. Defaults to null.
72
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors An array of KeyDescriptor elements. Defaults to an empty array.
73
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization The organization running this entity. Defaults to null.
74
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contacts An array of contacts for this entity. Defaults to an empty array.
75
     * @param \DOMAttr[] $namespacedAttributes
76
     */
77
    public function __construct(
78
        array $protocolSupportEnumeration,
79
        ?string $ID = null,
80
        ?int $validUntil = null,
81
        ?string $cacheDuration = null,
82
        ?Extensions $extensions = null,
83
        ?string $errorURL = null,
84
        array $keyDescriptors = [],
85
        ?Organization $organization = null,
86
        array $contacts = [],
87
        array $namespacedAttributes = []
88
    ) {
89
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
90
91
        $this->setProtocolSupportEnumeration($protocolSupportEnumeration);
92
        $this->setErrorURL($errorURL);
93
        $this->setKeyDescriptors($keyDescriptors);
94
        $this->setOrganization($organization);
95
        $this->setContactPersons($contacts);
96
    }
97
98
99
    /**
100
     * Collect the value of the errorURL property.
101
     *
102
     * @return string|null
103
     */
104
    public function getErrorURL()
105
    {
106
        return $this->errorURL;
107
    }
108
109
110
    /**
111
     * Set the value of the errorURL property.
112
     *
113
     * @param string|null $errorURL
114
     * @throws \InvalidArgumentException
115
     */
116
    protected function setErrorURL(?string $errorURL = null): void
117
    {
118
        if (!is_null($errorURL) && !filter_var($errorURL, FILTER_VALIDATE_URL)) {
119
            throw new InvalidArgumentException('RoleDescriptor errorURL is not a valid URL.');
120
        }
121
        $this->errorURL = $errorURL;
122
    }
123
124
125
    /**
126
     * Collect the value of the protocolSupportEnumeration property.
127
     *
128
     * @return string[]
129
     */
130
    public function getProtocolSupportEnumeration()
131
    {
132
        return $this->protocolSupportEnumeration;
133
    }
134
135
136
    /**
137
     * Set the value of the ProtocolSupportEnumeration property.
138
     *
139
     * @param string[] $protocols
140
     * @throws \SimpleSAML\Assert\AssertionFailedException if the qualified name of the supplied element is wrong
141
     */
142
    protected function setProtocolSupportEnumeration(array $protocols): void
143
    {
144
        Assert::minCount($protocols, 1, 'At least one protocol must be supported by this ' . static::class . '.');
145
        Assert::allStringNotEmpty($protocols, 'Cannot specify an empty string as a supported protocol.');
146
        Assert::oneOf(Constants::NS_SAMLP, $protocols, 'At least SAML 2.0 must be one of supported protocols.');
147
148
        $this->protocolSupportEnumeration = $protocols;
149
    }
150
151
152
    /**
153
     * Collect the value of the Organization property.
154
     *
155
     * @return \SimpleSAML\SAML2\XML\md\Organization|null
156
     */
157
    public function getOrganization()
158
    {
159
        return $this->Organization;
160
    }
161
162
163
    /**
164
     * Set the value of the Organization property.
165
     *
166
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
167
     */
168
    protected function setOrganization(?Organization $organization = null): void
169
    {
170
        $this->Organization = $organization;
171
    }
172
173
174
    /**
175
     * Collect the value of the ContactPersons property.
176
     *
177
     * @return \SimpleSAML\SAML2\XML\md\ContactPerson[]
178
     */
179
    public function getContactPersons()
180
    {
181
        return $this->ContactPersons;
182
    }
183
184
185
    /**
186
     * Set the value of the ContactPerson property.
187
     *
188
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contactPersons
189
     * @throws \SimpleSAML\Assert\AssertionFailedException
190
     */
191
    protected function setContactPersons(array $contactPersons): void
192
    {
193
        Assert::allIsInstanceOf(
194
            $contactPersons,
195
            ContactPerson::class,
196
            'All contacts must be an instance of md:ContactPerson'
197
        );
198
        $this->ContactPersons = $contactPersons;
199
    }
200
201
202
    /**
203
     * Collect the value of the KeyDescriptors property.
204
     *
205
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
206
     */
207
    public function getKeyDescriptors()
208
    {
209
        return $this->KeyDescriptors;
210
    }
211
212
213
    /**
214
     * Set the value of the KeyDescriptor property.
215
     *
216
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
217
     */
218
    protected function setKeyDescriptors(array $keyDescriptor): void
219
    {
220
        Assert::allIsInstanceOf(
221
            $keyDescriptor,
222
            KeyDescriptor::class,
223
            'All key descriptors must be an instance of md:KeyDescriptor'
224
        );
225
        $this->KeyDescriptors = $keyDescriptor;
226
    }
227
228
229
    /**
230
     * Convert this descriptor to an unsigned XML document.
231
     * This method does not sign the resulting XML document.
232
     *
233
     * @param \DOMElement|null $parent
234
     * @return \DOMElement The root element of the DOM tree
235
     */
236
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
237
    {
238
        $e = parent::toUnsignedXML($parent);
239
240
        $e->setAttribute('protocolSupportEnumeration', implode(' ', $this->protocolSupportEnumeration));
241
242
        if ($this->errorURL !== null) {
243
            $e->setAttribute('errorURL', $this->errorURL);
244
        }
245
246
        foreach ($this->KeyDescriptors as $kd) {
247
            $kd->toXML($e);
248
        }
249
250
        if ($this->Organization !== null) {
251
            $this->Organization->toXML($e);
252
        }
253
254
        foreach ($this->ContactPersons as $cp) {
255
            $cp->toXML($e);
256
        }
257
258
        return $e;
259
    }
260
}
261