Passed
Push — master ( 6db269...70aff8 )
by Jaime Pérez
02:49
created

AbstractRoleDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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