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

AbstractRoleDescriptor::toXML()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 16
nop 1
dl 0
loc 23
rs 9.6111
c 0
b 0
f 0
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;
10
11
use function filter_var;
12
use function implode;
13
use function is_null;
14
15
/**
16
 * Class representing SAML 2 RoleDescriptor element.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
abstract class AbstractRoleDescriptor extends AbstractMetadataDocument
21
{
22
    /**
23
     * List of supported protocols.
24
     *
25
     * @var string[]
26
     */
27
    protected array $protocolSupportEnumeration = [];
28
29
    /**
30
     * Error URL for this role.
31
     *
32
     * @var string|null
33
     */
34
    protected ?string $errorURL = null;
35
36
    /**
37
     * KeyDescriptor elements.
38
     *
39
     * Array of \SimpleSAML\SAML2\XML\md\KeyDescriptor elements.
40
     *
41
     * @var \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
42
     */
43
    protected array $KeyDescriptors = [];
44
45
    /**
46
     * Organization of this role.
47
     *
48
     * @var \SimpleSAML\SAML2\XML\md\Organization|null
49
     */
50
    protected ?Organization $Organization = null;
51
52
    /**
53
     * ContactPerson elements for this role.
54
     *
55
     * Array of \SimpleSAML\SAML2\XML\md\ContactPerson objects.
56
     *
57
     * @var \SimpleSAML\SAML2\XML\md\ContactPerson[]
58
     */
59
    protected array $ContactPersons = [];
60
61
62
    /**
63
     * Initialize a RoleDescriptor.
64
     *
65
     * @param string[] $protocolSupportEnumeration A set of URI specifying the protocols supported.
66
     * @param string|null $ID The ID for this document. Defaults to null.
67
     * @param int|null $validUntil Unix time of validity for this document. Defaults to null.
68
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
69
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An Extensions object. Defaults to null.
70
     * @param string|null $errorURL An URI where to redirect users for support. Defaults to null.
71
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors An array of KeyDescriptor elements. Defaults to an empty array.
72
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization The organization running this entity. Defaults to null.
73
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contacts An array of contacts for this entity. Defaults to an empty array.
74
     * @param \DOMAttr[] $namespacedAttributes
75
     */
76
    public function __construct(
77
        array $protocolSupportEnumeration,
78
        ?string $ID = null,
79
        ?int $validUntil = null,
80
        ?string $cacheDuration = null,
81
        ?Extensions $extensions = null,
82
        ?string $errorURL = null,
83
        array $keyDescriptors = [],
84
        ?Organization $organization = null,
85
        array $contacts = [],
86
        array $namespacedAttributes = []
87
    ) {
88
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
89
90
        $this->setProtocolSupportEnumeration($protocolSupportEnumeration);
91
        $this->setErrorURL($errorURL);
92
        $this->setKeyDescriptors($keyDescriptors);
93
        $this->setOrganization($organization);
94
        $this->setContactPersons($contacts);
95
    }
96
97
98
    /**
99
     * Collect the value of the errorURL property.
100
     *
101
     * @return string|null
102
     */
103
    public function getErrorURL()
104
    {
105
        return $this->errorURL;
106
    }
107
108
109
    /**
110
     * Set the value of the errorURL property.
111
     *
112
     * @param string|null $errorURL
113
     * @throws \SimpleSAML\SAML2\Exception\ProtocolViolationException
114
     */
115
    protected function setErrorURL(?string $errorURL = null): void
116
    {
117
        Assert::false(
118
            !is_null($errorURL) && !filter_var($errorURL, FILTER_VALIDATE_URL),
119
            '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
199
        $this->ContactPersons = $contactPersons;
200
    }
201
202
203
    /**
204
     * Collect the value of the KeyDescriptors property.
205
     *
206
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
207
     */
208
    public function getKeyDescriptors()
209
    {
210
        return $this->KeyDescriptors;
211
    }
212
213
214
    /**
215
     * Set the value of the KeyDescriptor property.
216
     *
217
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
218
     */
219
    protected function setKeyDescriptors(array $keyDescriptor): void
220
    {
221
        Assert::allIsInstanceOf(
222
            $keyDescriptor,
223
            KeyDescriptor::class,
224
            'All key descriptors must be an instance of md:KeyDescriptor',
225
        );
226
227
        $this->KeyDescriptors = $keyDescriptor;
228
    }
229
230
231
    /**
232
     * Convert this descriptor to an unsigned XML document.
233
     * This method does not sign the resulting XML document.
234
     *
235
     * @param \DOMElement|null $parent
236
     * @return \DOMElement The root element of the DOM tree
237
     */
238
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
239
    {
240
        $e = parent::toUnsignedXML($parent);
241
242
        $e->setAttribute('protocolSupportEnumeration', implode(' ', $this->protocolSupportEnumeration));
243
244
        if ($this->errorURL !== null) {
245
            $e->setAttribute('errorURL', $this->errorURL);
246
        }
247
248
        foreach ($this->KeyDescriptors as $kd) {
249
            $kd->toXML($e);
250
        }
251
252
        if ($this->Organization !== null) {
253
            $this->Organization->toXML($e);
254
        }
255
256
        foreach ($this->ContactPersons as $cp) {
257
            $cp->toXML($e);
258
        }
259
260
        return $e;
261
    }
262
}
263