Completed
Branch rewrite-api-md (eb38e5)
by Tim
03:57
created

PDPDescriptor::setNameIDFormats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\md;
6
7
use DOMElement;
8
use SAML2\Constants;
9
use SAML2\Utils;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Class representing SAML 2 metadata PDPDescriptor.
14
 *
15
 * @package SimpleSAMLphp
16
 */
17
final class PDPDescriptor extends AbstractRoleDescriptor
18
{
19
    /**
20
     * List of AuthzService endpoints.
21
     *
22
     * @var \SAML2\XML\md\AuthzService[]
23
     */
24
    protected $authzServiceEndpoints = [];
25
26
    /**
27
     * List of AssertionIDRequestService endpoints.
28
     *
29
     * @var \SAML2\XML\md\AssertionIDRequestService[]
30
     */
31
    protected $assertionIDRequestServiceEndpoints = [];
32
33
    /**
34
     * List of supported NameID formats.
35
     *
36
     * @var string[]
37
     */
38
    protected $nameIDFormats = [];
39
40
41
    /**
42
     * PDPDescriptor constructor.
43
     *
44
     * @param \SAML2\XML\md\AuthzService[] $authServiceEndpoints
45
     * @param string[] $protocolSupportEnumeration
46
     * @param \SAML2\XML\md\AssertionIDRequestService[]|null $assertionIDRequestService
47
     * @param string[]|null $nameIDFormats
48
     * @param string|null $ID
49
     * @param int|null $validUntil
50
     * @param string|null $cacheDuration
51
     * @param \SAML2\XML\md\Extensions|null $extensions
52
     * @param string|null $errorURL
53
     * @param \SAML2\XML\md\KeyDescriptor[]|null $keyDescriptors
54
     * @param \SAML2\XML\md\Organization|null $organization
55
     * @param \SAML2\XML\md\ContactPerson[]|null $contacts
56
     */
57
    public function __construct(
58
        array $authServiceEndpoints,
59
        array $protocolSupportEnumeration,
60
        ?array $assertionIDRequestService = null,
61
        ?array $nameIDFormats = null,
62
        ?string $ID = null,
63
        ?int $validUntil = null,
64
        ?string $cacheDuration = null,
65
        ?Extensions $extensions = null,
66
        ?string $errorURL = null,
67
        ?array $keyDescriptors = null,
68
        ?Organization $organization = null,
69
        ?array $contacts = null
70
    ) {
71
        parent::__construct(
72
            $protocolSupportEnumeration,
73
            $ID,
74
            $validUntil,
75
            $cacheDuration,
76
            $extensions,
77
            $errorURL,
78
            $keyDescriptors,
79
            $organization,
80
            $contacts
81
        );
82
        $this->setAuthzServiceEndpoints($authServiceEndpoints);
83
        $this->setAssertionIDRequestServices($assertionIDRequestService);
84
        $this->setNameIDFormats($nameIDFormats);
85
    }
86
87
88
    /**
89
     * Initialize an IDPSSODescriptor from a given XML document.
90
     *
91
     * @param \DOMElement|null $xml The XML element we should load.
92
     * @return \SAML2\XML\md\PDPDescriptor
93
     * @throws \Exception
94
     */
95
    public static function fromXML(DOMElement $xml): object
96
    {
97
        $validUntil = self::getAttribute($xml, 'validUntil', null);
98
        $orgs = Organization::getChildrenOfClass($xml);
99
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor');
100
101
        $extensions = Extensions::getChildrenOfClass($xml);
102
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.');
103
104
        return new self(
105
            AuthzService::getChildrenOfClass($xml),
106
            preg_split('/[\s]+/', trim(self::getAttribute($xml, 'protocolSupportEnumeration'))),
0 ignored issues
show
Bug introduced by
It seems like preg_split('/[\s]+/', tr...lSupportEnumeration'))) can also be of type false; however, parameter $protocolSupportEnumeration of SAML2\XML\md\PDPDescriptor::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

106
            /** @scrutinizer ignore-type */ preg_split('/[\s]+/', trim(self::getAttribute($xml, 'protocolSupportEnumeration'))),
Loading history...
107
            AssertionIDRequestService::getChildrenOfClass($xml),
108
            Utils::extractStrings($xml, Constants::NS_MD, 'NameIDFormat'),
109
            self::getAttribute($xml, 'ID', null),
110
            $validUntil !== null ? Utils::xsDateTimeToTimestamp($validUntil) : null,
111
            self::getAttribute($xml, 'cacheDuration', null),
112
            !empty($extensions) ? $extensions[0] : null,
113
            self::getAttribute($xml, 'errorURL', null),
114
            KeyDescriptor::getChildrenOfClass($xml),
115
            !empty($orgs) ? $orgs[0] : null,
116
            ContactPerson::getChildrenOfClass($xml)
117
        );
118
    }
119
120
121
    /**
122
     * Get the AuthzService endpoints of this PDPDescriptor
123
     *
124
     * @return \SAML2\XML\md\AuthzService[]
125
     */
126
    public function getAuthzServiceEndpoints(): array
127
    {
128
        return $this->authzServiceEndpoints;
129
    }
130
131
132
    /**
133
     * Set the AuthzService endpoints for this PDPDescriptor
134
     *
135
     * @param \SAML2\XML\md\AuthzService[] $authzServices
136
     */
137
    protected function setAuthzServiceEndpoints(?array $authzServices = []): void
138
    {
139
        if ($authzServices === null) {
1 ignored issue
show
introduced by
The condition $authzServices === null is always false.
Loading history...
140
            return;
141
        }
142
        Assert::allIsInstanceOf(
143
            $authzServices,
144
            AuthzService::class,
145
            'All md:AuthzService endpoints must be an instance of AuthzService.'
146
        );
147
        $this->authzServiceEndpoints = $authzServices;
148
    }
149
150
151
    /**
152
     * Get the AssertionIDRequestService endpoints of this PDPDescriptor
153
     *
154
     * @return \SAML2\XML\md\AssertionIDRequestService[]
155
     */
156
    public function getAssertionIDRequestServices(): array
157
    {
158
        return $this->assertionIDRequestServiceEndpoints;
159
    }
160
161
162
    /**
163
     * Set the AssertionIDRequestService endpoints for this PDPDescriptor
164
     *
165
     * @param \SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestServices
166
     */
167
    public function setAssertionIDRequestServices(?array $assertionIDRequestServices): void
168
    {
169
        if ($assertionIDRequestServices === null) {
1 ignored issue
show
introduced by
The condition $assertionIDRequestServices === null is always false.
Loading history...
170
            return;
171
        }
172
        Assert::allIsInstanceOf(
173
            $assertionIDRequestServices,
174
            AssertionIDRequestService::class,
175
            'All md:AssertionIDRequestService endpoints must be an instance of AssertionIDRequestService.'
176
        );
177
        $this->assertionIDRequestServiceEndpoints = $assertionIDRequestServices;
178
    }
179
180
181
    /**
182
     * Get the NameIDFormats supported by this PDPDescriptor
183
     *
184
     * @return string[]
185
     */
186
    public function getNameIDFormats(): array
187
    {
188
        return $this->nameIDFormats;
189
    }
190
191
192
    /**
193
     * Set the NameIDFormats supported by this PDPDescriptor
194
     *
195
     * @param string[] $nameIDFormats
196
     */
197
    public function setNameIDFormats(?array $nameIDFormats): void
198
    {
199
        Assert::allStringNotEmpty($nameIDFormats, 'All NameIDFormat must be a non-empty string.');
200
        $this->nameIDFormats = $nameIDFormats;
201
    }
202
203
204
    /**
205
     * Add this PDPDescriptor to an EntityDescriptor.
206
     *
207
     * @param \DOMElement $parent The EntityDescriptor we should append this IDPSSODescriptor to.
208
     * @return \DOMElement
209
     * @throws \Exception
210
     */
211
    public function toXML(DOMElement $parent = null): DOMElement
212
    {
213
        $e = parent::toXML($parent);
214
215
        foreach ($this->authzServiceEndpoints as $ep) {
216
            $ep->toXML($e);
217
        }
218
219
        foreach ($this->assertionIDRequestServiceEndpoints as $ep) {
220
            $ep->toXML($e);
221
        }
222
223
        Utils::addStrings($e, Constants::NS_MD, 'md:NameIDFormat', false, $this->nameIDFormats);
224
225
        return $e;
226
    }
227
}
228