Passed
Push — master ( 7a371e...4a8d98 )
by Tim
02:17
created

PDPDescriptor::getAssertionIDRequestService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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\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
     * PDPDescriptor constructor.
24
     *
25
     * @param \SimpleSAML\SAML2\XML\md\AuthzService[] $authzServiceEndpoints
26
     * @param string[] $protocolSupportEnumeration
27
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
28
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormats
29
     * @param string|null $ID
30
     * @param int|null $validUntil
31
     * @param string|null $cacheDuration
32
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
33
     * @param string|null $errorURL
34
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
35
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors
36
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contacts
37
     */
38
    public function __construct(
39
        protected array $authzService,
40
        array $protocolSupportEnumeration,
41
        protected array $assertionIDRequestService = [],
42
        protected array $nameIDFormat = [],
43
        ?string $ID = null,
44
        ?int $validUntil = null,
45
        ?string $cacheDuration = null,
46
        ?Extensions $extensions = null,
47
        ?string $errorURL = null,
48
        ?Organization $organization = null,
49
        array $keyDescriptors = [],
50
        array $contacts = [],
51
    ) {
52
        Assert::minCount($authzService, 1, 'At least one md:AuthzService endpoint must be present.');
53
        Assert::allIsInstanceOf(
54
            $authzService,
55
            AuthzService::class,
56
            'All md:AuthzService endpoints must be an instance of AuthzService.',
57
        );
58
        Assert::allIsInstanceOf(
59
            $assertionIDRequestService,
60
            AssertionIDRequestService::class,
61
            'All md:AssertionIDRequestService endpoints must be an instance of AssertionIDRequestService.',
62
        );
63
        Assert::allIsInstanceOf($nameIDFormat, NameIDFormat::class);
64
65
        parent::__construct(
66
            $protocolSupportEnumeration,
67
            $ID,
68
            $validUntil,
69
            $cacheDuration,
70
            $extensions,
71
            $errorURL,
72
            $keyDescriptors,
73
            $organization,
74
            $contacts,
75
        );
76
    }
77
78
79
    /**
80
     * Get the AuthzService endpoints of this PDPDescriptor
81
     *
82
     * @return \SimpleSAML\SAML2\XML\md\AuthzService[]
83
     */
84
    public function getAuthzService(): array
85
    {
86
        return $this->authzService;
87
    }
88
89
90
    /**
91
     * Get the AssertionIDRequestService endpoints of this PDPDescriptor
92
     *
93
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
94
     */
95
    public function getAssertionIDRequestService(): array
96
    {
97
        return $this->assertionIDRequestService;
98
    }
99
100
101
    /**
102
     * Get the NameIDFormats supported by this PDPDescriptor
103
     *
104
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
105
     */
106
    public function getNameIDFormat(): array
107
    {
108
        return $this->nameIDFormat;
109
    }
110
111
112
    /**
113
     * Initialize an IDPSSODescriptor from a given XML document.
114
     *
115
     * @param \DOMElement $xml The XML element we should load.
116
     * @return \SimpleSAML\SAML2\XML\md\PDPDescriptor
117
     *
118
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
119
     *   if the qualified name of the supplied element is wrong
120
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
121
     *   if the supplied element is missing one of the mandatory attributes
122
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
123
     *   if too many child-elements of a type are specified
124
     */
125
    public static function fromXML(DOMElement $xml): static
126
    {
127
        Assert::same($xml->localName, 'PDPDescriptor', InvalidDOMElementException::class);
128
        Assert::same($xml->namespaceURI, PDPDescriptor::NS, InvalidDOMElementException::class);
129
130
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
131
        $validUntil = self::getAttribute($xml, 'validUntil', null);
132
        $orgs = Organization::getChildrenOfClass($xml);
133
        Assert::maxCount(
134
            $orgs,
135
            1,
136
            'More than one Organization found in this descriptor',
137
            TooManyElementsException::class,
138
        );
139
140
        $extensions = Extensions::getChildrenOfClass($xml);
141
        Assert::maxCount(
142
            $extensions,
143
            1,
144
            'Only one md:Extensions element is allowed.',
145
            TooManyElementsException::class,
146
        );
147
148
        return new static(
149
            AuthzService::getChildrenOfClass($xml),
150
            preg_split('/[\s]+/', trim($protocols)),
151
            AssertionIDRequestService::getChildrenOfClass($xml),
152
            NameIDFormat::getChildrenOfClass($xml),
153
            self::getAttribute($xml, 'ID', null),
154
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
155
            self::getAttribute($xml, 'cacheDuration', null),
156
            !empty($extensions) ? $extensions[0] : null,
157
            self::getAttribute($xml, 'errorURL', null),
158
            !empty($orgs) ? $orgs[0] : null,
159
            KeyDescriptor::getChildrenOfClass($xml),
160
            ContactPerson::getChildrenOfClass($xml),
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\ContactPerson was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
161
        );
162
    }
163
164
165
    /**
166
     * Add this PDPDescriptor to an EntityDescriptor.
167
     *
168
     * @param \DOMElement $parent The EntityDescriptor we should append this IDPSSODescriptor to.
169
     * @return \DOMElement
170
     * @throws \Exception
171
     */
172
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
173
    {
174
        $e = parent::toUnsignedXML($parent);
175
176
        foreach ($this->getAuthzService() as $ep) {
177
            $ep->toXML($e);
178
        }
179
180
        foreach ($this->getAssertionIDRequestService() as $ep) {
181
            $ep->toXML($e);
182
        }
183
184
        foreach ($this->getNameIDFormat() as $nidFormat) {
185
            $nidFormat->toXML($e);
186
        }
187
188
        return $e;
189
    }
190
}
191