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