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