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

EntitiesDescriptor::toUnsignedXML()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 16
nop 1
dl 0
loc 21
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\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\Utils as XMLUtils;
13
use SimpleSAML\XMLSecurity\XML\ds\Signature;
14
15
/**
16
 * Class representing SAML 2 EntitiesDescriptor element.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
final class EntitiesDescriptor extends AbstractMetadataDocument
21
{
22
    /**
23
     * The name of this entity collection.
24
     *
25
     * @var string|null
26
     */
27
    protected ?string $Name = null;
28
29
    /** @var \SimpleSAML\SAML2\XML\md\EntityDescriptor[] */
30
    protected array $entityDescriptors = [];
31
32
    /** @var \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] */
33
    protected array $entitiesDescriptors = [];
34
35
36
    /**
37
     * EntitiesDescriptor constructor.
38
     *
39
     * @param \SimpleSAML\SAML2\XML\md\EntityDescriptor[] $entityDescriptors
40
     * @param \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] $entitiesDescriptors
41
     * @param string|null $name
42
     * @param string|null $ID
43
     * @param int|null $validUntil
44
     * @param string|null $cacheDuration
45
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
46
     * @param \DOMAttr[] $namespacedAttributes
47
     */
48
    public function __construct(
49
        array $entityDescriptors = [],
50
        array $entitiesDescriptors = [],
51
        ?string $name = null,
52
        ?string $ID = null,
53
        ?int $validUntil = null,
54
        ?string $cacheDuration = null,
55
        ?Extensions $extensions = null,
56
        array $namespacedAttributes = []
57
    ) {
58
        Assert::true(
59
            !empty($entitiesDescriptors) || !empty($entityDescriptors),
60
            'At least one md:EntityDescriptor or md:EntitiesDescriptor element is required.',
61
            ProtocolViolationException::class,
62
        );
63
64
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
65
66
        $this->setName($name);
67
        $this->setEntityDescriptors($entityDescriptors);
68
        $this->setEntitiesDescriptors($entitiesDescriptors);
69
    }
70
71
72
    /**
73
     * Initialize an EntitiesDescriptor from an existing XML document.
74
     *
75
     * @param \DOMElement $xml The XML element we should load.
76
     * @return \SimpleSAML\SAML2\XML\md\EntitiesDescriptor
77
     *
78
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
79
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
80
     */
81
    public static function fromXML(DOMElement $xml): object
82
    {
83
        Assert::same($xml->localName, 'EntitiesDescriptor', InvalidDOMElementException::class);
84
        Assert::same($xml->namespaceURI, EntitiesDescriptor::NS, InvalidDOMElementException::class);
85
86
        $validUntil = self::getAttribute($xml, 'validUntil', null);
87
        $orgs = Organization::getChildrenOfClass($xml);
88
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
89
90
        $extensions = Extensions::getChildrenOfClass($xml);
91
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
92
93
        $signature = Signature::getChildrenOfClass($xml);
94
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
95
96
        $entities = new self(
97
            EntityDescriptor::getChildrenOfClass($xml),
98
            EntitiesDescriptor::getChildrenOfClass($xml),
99
            self::getAttribute($xml, 'Name', null),
100
            self::getAttribute($xml, 'ID', null),
101
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
102
            self::getAttribute($xml, 'cacheDuration', null),
103
            !empty($extensions) ? $extensions[0] : null,
104
            self::getAttributesNSFromXML($xml)
105
        );
106
107
        if (!empty($signature)) {
108
            $entities->setSignature($signature[0]);
109
        }
110
111
        $entities->setXML($xml);
112
113
        return $entities;
114
    }
115
116
117
    /**
118
     * Get the EntitiesDescriptor children objects
119
     *
120
     * @return \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[]
121
     */
122
    public function getEntitiesDescriptors(): array
123
    {
124
        return $this->entitiesDescriptors;
125
    }
126
127
128
    /**
129
     * Set the EntitiesDescriptor children objects
130
     *
131
     * @param \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] $entitiesDescriptors
132
     */
133
    protected function setEntitiesDescriptors(array $entitiesDescriptors): void
134
    {
135
        Assert::allIsInstanceOf($entitiesDescriptors, EntitiesDescriptor::class);
136
        $this->entitiesDescriptors = $entitiesDescriptors;
137
    }
138
139
140
    /**
141
     * Get the EntityDescriptor children objects
142
     *
143
     * @return \SimpleSAML\SAML2\XML\md\EntityDescriptor[]
144
     */
145
    public function getEntityDescriptors(): array
146
    {
147
        return $this->entityDescriptors;
148
    }
149
150
151
152
    /**
153
     * Set the EntityDescriptor children objects
154
     *
155
     * @param \SimpleSAML\SAML2\XML\md\EntityDescriptor[] $entityDescriptors
156
     */
157
    protected function setEntityDescriptors(array $entityDescriptors): void
158
    {
159
        Assert::allIsInstanceOf($entityDescriptors, EntityDescriptor::class);
160
        $this->entityDescriptors = $entityDescriptors;
161
    }
162
163
164
165
    /**
166
     * Collect the value of the Name property.
167
     *
168
     * @return string|null
169
     */
170
    public function getName(): ?string
171
    {
172
        return $this->Name;
173
    }
174
175
176
    /**
177
     * Set the value of the Name property.
178
     *
179
     * @param string|null $name
180
     */
181
    protected function setName(?string $name = null): void
182
    {
183
        if ($name === null) {
184
            return;
185
        }
186
        $this->Name = $name;
187
    }
188
189
190
    /**
191
     * Convert this descriptor to an unsigned XML document.
192
     * This method does not sign the resulting XML document.
193
     *
194
     * @param \DOMElement|null $parent
195
     * @return \DOMElement The root element of the DOM tree
196
     */
197
    protected function toUnsignedXML(DOMElement $parent = null): DOMElement
198
    {
199
        $e = parent::toUnsignedXML($parent);
200
201
        foreach ($this->getAttributesNS() as $attr) {
202
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
203
        }
204
205
        if ($this->Name !== null) {
206
            $e->setAttribute('Name', $this->Name);
207
        }
208
209
        foreach ($this->entitiesDescriptors as $entitiesDescriptor) {
210
            $entitiesDescriptor->toXML($e);
211
        }
212
213
        foreach ($this->entityDescriptors as $entityDescriptor) {
214
            $entityDescriptor->toXML($e);
215
        }
216
217
        return $e;
218
    }
219
}
220