Passed
Push — master ( 0af655...b179bb )
by Tim
02:15
created

PDPDescriptor::toXML()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 16
nop 1
dl 0
loc 23
rs 9.5555
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A PDPDescriptor::toUnsignedXML() 0 17 4
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\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\TooManyElementsException;
11
use SimpleSAML\XML\Utils as XMLUtils;
12
13
use function preg_split;
14
15
/**
16
 * Class representing SAML 2 metadata PDPDescriptor.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
final class PDPDescriptor extends AbstractRoleDescriptor
21
{
22
    /**
23
     * List of AuthzService endpoints.
24
     *
25
     * @var \SimpleSAML\SAML2\XML\md\AuthzService[]
26
     */
27
    protected array $authzServiceEndpoints = [];
28
29
    /**
30
     * List of AssertionIDRequestService endpoints.
31
     *
32
     * @var \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
33
     */
34
    protected array $assertionIDRequestServiceEndpoints = [];
35
36
    /**
37
     * List of supported NameID formats.
38
     *
39
     * @var \SimpleSAML\SAML2\XML\md\NameIDFormat[]
40
     */
41
    protected array $NameIDFormats = [];
42
43
44
    /**
45
     * PDPDescriptor constructor.
46
     *
47
     * @param \SimpleSAML\SAML2\XML\md\AuthzService[] $authzServiceEndpoints
48
     * @param string[] $protocolSupportEnumeration
49
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
50
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
51
     * @param string|null $ID
52
     * @param int|null $validUntil
53
     * @param string|null $cacheDuration
54
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
55
     * @param string|null $errorURL
56
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
57
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors
58
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contacts
59
     */
60
    public function __construct(
61
        array $authzServiceEndpoints,
62
        array $protocolSupportEnumeration,
63
        array $assertionIDRequestService = [],
64
        array $nameIDFormats = [],
65
        ?string $ID = null,
66
        ?int $validUntil = null,
67
        ?string $cacheDuration = null,
68
        ?Extensions $extensions = null,
69
        ?string $errorURL = null,
70
        ?Organization $organization = null,
71
        array $keyDescriptors = [],
72
        array $contacts = []
73
    ) {
74
        parent::__construct(
75
            $protocolSupportEnumeration,
76
            $ID,
77
            $validUntil,
78
            $cacheDuration,
79
            $extensions,
80
            $errorURL,
81
            $keyDescriptors,
82
            $organization,
83
            $contacts
84
        );
85
86
        $this->setAuthzServiceEndpoints($authzServiceEndpoints);
87
        $this->setAssertionIDRequestServices($assertionIDRequestService);
88
        $this->setNameIDFormats($nameIDFormats);
89
    }
90
91
92
    /**
93
     * Get the AuthzService endpoints of this PDPDescriptor
94
     *
95
     * @return \SimpleSAML\SAML2\XML\md\AuthzService[]
96
     */
97
    public function getAuthzServiceEndpoints(): array
98
    {
99
        return $this->authzServiceEndpoints;
100
    }
101
102
103
    /**
104
     * Set the AuthzService endpoints for this PDPDescriptor
105
     *
106
     * @param \SimpleSAML\SAML2\XML\md\AuthzService[] $authzServices
107
     * @throws \SimpleSAML\Assert\AssertionFailedException
108
     */
109
    private function setAuthzServiceEndpoints(array $authzServices = []): void
110
    {
111
        Assert::minCount($authzServices, 1, 'At least one md:AuthzService endpoint must be present.');
112
        Assert::allIsInstanceOf(
113
            $authzServices,
114
            AuthzService::class,
115
            'All md:AuthzService endpoints must be an instance of AuthzService.'
116
        );
117
        $this->authzServiceEndpoints = $authzServices;
118
    }
119
120
121
    /**
122
     * Get the AssertionIDRequestService endpoints of this PDPDescriptor
123
     *
124
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
125
     */
126
    public function getAssertionIDRequestServices(): array
127
    {
128
        return $this->assertionIDRequestServiceEndpoints;
129
    }
130
131
132
    /**
133
     * Set the AssertionIDRequestService endpoints for this PDPDescriptor
134
     *
135
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestServices
136
     * @throws \SimpleSAML\Assert\AssertionFailedException
137
     */
138
    private function setAssertionIDRequestServices(array $assertionIDRequestServices): void
139
    {
140
        Assert::allIsInstanceOf(
141
            $assertionIDRequestServices,
142
            AssertionIDRequestService::class,
143
            'All md:AssertionIDRequestService endpoints must be an instance of AssertionIDRequestService.'
144
        );
145
        $this->assertionIDRequestServiceEndpoints = $assertionIDRequestServices;
146
    }
147
148
149
    /**
150
     * Get the NameIDFormats supported by this PDPDescriptor
151
     *
152
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
153
     */
154
    public function getNameIDFormats(): array
155
    {
156
        return $this->NameIDFormats;
157
    }
158
159
160
    /**
161
     * Set the NameIDFormats supported by this PDPDescriptor
162
     *
163
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
164
     */
165
    private function setNameIDFormats(array $nameIDFormats): void
166
    {
167
        Assert::allIsInstanceOf($nameIDFormats, NameIDFormat::class);
168
        $this->NameIDFormats = $nameIDFormats;
169
    }
170
171
172
    /**
173
     * Initialize an IDPSSODescriptor from a given XML document.
174
     *
175
     * @param \DOMElement $xml The XML element we should load.
176
     * @return \SimpleSAML\SAML2\XML\md\PDPDescriptor
177
     *
178
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
179
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
180
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
181
     */
182
    public static function fromXML(DOMElement $xml): static
183
    {
184
        Assert::same($xml->localName, 'PDPDescriptor', InvalidDOMElementException::class);
185
        Assert::same($xml->namespaceURI, PDPDescriptor::NS, InvalidDOMElementException::class);
186
187
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
188
        $validUntil = self::getAttribute($xml, 'validUntil', null);
189
        $orgs = Organization::getChildrenOfClass($xml);
190
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
191
192
        $extensions = Extensions::getChildrenOfClass($xml);
193
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
194
195
        return new static(
196
            AuthzService::getChildrenOfClass($xml),
197
            preg_split('/[\s]+/', trim($protocols)),
198
            AssertionIDRequestService::getChildrenOfClass($xml),
199
            NameIDFormat::getChildrenOfClass($xml),
200
            self::getAttribute($xml, 'ID', null),
201
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
202
            self::getAttribute($xml, 'cacheDuration', null),
203
            !empty($extensions) ? $extensions[0] : null,
204
            self::getAttribute($xml, 'errorURL', null),
205
            !empty($orgs) ? $orgs[0] : null,
206
            KeyDescriptor::getChildrenOfClass($xml),
207
            ContactPerson::getChildrenOfClass($xml)
208
        );
209
    }
210
211
212
    /**
213
     * Add this PDPDescriptor to an EntityDescriptor.
214
     *
215
     * @param \DOMElement $parent The EntityDescriptor we should append this IDPSSODescriptor to.
216
     * @return \DOMElement
217
     * @throws \Exception
218
     */
219
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
220
    {
221
        $e = parent::toUnsignedXML($parent);
222
223
        foreach ($this->getAuthzServiceEndpoints() as $ep) {
224
            $ep->toXML($e);
225
        }
226
227
        foreach ($this->getAssertionIDRequestServices() as $ep) {
228
            $ep->toXML($e);
229
        }
230
231
        foreach ($this->getNameIDFormats() as $nidFormat) {
232
            $nidFormat->toXML($e);
233
        }
234
235
        return $e;
236
    }
237
}
238