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

AbstractMetadataDocument::toUnsignedXML()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 32
nop 1
dl 0
loc 25
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace SimpleSAML\SAML2\XML\md;
4
5
use DOMElement;
6
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
7
use SimpleSAML\XML\ExtendableAttributesTrait;
8
9
use function gmdate;
10
11
/**
12
 * Class to represent a metadata document
13
 *
14
 * @package simplesamlphp/saml2
15
 */
16
abstract class AbstractMetadataDocument extends AbstractSignedMdElement
17
{
18
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AbstractMetadataDocument: $namespaceURI, $nodeName, $attributes, $value
Loading history...
19
    use ExtendableElementTrait;
20
21
    /**
22
     * The ID of this element.
23
     *
24
     * @var string|null
25
     */
26
    protected ?string $Id;
27
28
    /** @var \DOMElement $xml */
29
    protected DOMElement $xml;
30
31
    /**
32
     * How long this element is valid, as a unix timestamp.
33
     *
34
     * @var int|null
35
     */
36
    protected ?int $validUntil;
37
38
    /**
39
     * The length of time this element can be cached, as string.
40
     *
41
     * @var string|null
42
     */
43
    protected ?string $cacheDuration;
44
45
46
    /**
47
     * Generic constructor for SAML metadata documents.
48
     *
49
     * @param string|null $ID The ID for this document. Defaults to null.
50
     * @param int|null    $validUntil Unix time of validity for this document. Defaults to null.
51
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
52
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to null.
53
     * @param \DOMAttr[] $namespacedAttributes
54
     */
55
    public function __construct(
56
        ?string $ID = null,
57
        ?int $validUntil = null,
58
        ?string $cacheDuration = null,
59
        ?Extensions $extensions = null,
60
        $namespacedAttributes = []
61
    ) {
62
        $this->setID($ID);
63
        $this->setValidUntil($validUntil);
64
        $this->setCacheDuration($cacheDuration);
65
        $this->setExtensions($extensions);
66
        $this->setAttributesNS($namespacedAttributes);
67
    }
68
69
70
    /**
71
     * Collect the value of the ID property.
72
     *
73
     * @return string|null
74
     */
75
    public function getID(): ?string
76
    {
77
        return $this->Id;
78
    }
79
80
81
    /**
82
     * Set the value of the ID property.
83
     *
84
     * @param string|null $id
85
     */
86
    protected function setID(?string $id): void
87
    {
88
        $this->Id = $id;
89
    }
90
91
92
    /**
93
     * Collect the value of the validUntil property.
94
     *
95
     * @return int|null
96
     */
97
    public function getValidUntil(): ?int
98
    {
99
        return $this->validUntil;
100
    }
101
102
103
    /**
104
     * Set the value of the validUntil-property
105
     *
106
     * @param int|null $validUntil
107
     */
108
    protected function setValidUntil(?int $validUntil): void
109
    {
110
        $this->validUntil = $validUntil;
111
    }
112
113
114
    /**
115
     * Collect the value of the cacheDuration property.
116
     *
117
     * @return string|null
118
     */
119
    public function getCacheDuration(): ?string
120
    {
121
        return $this->cacheDuration;
122
    }
123
124
125
    /**
126
     * Set the value of the cacheDuration-property
127
     *
128
     * @param string|null $cacheDuration
129
     */
130
    protected function setCacheDuration(?string $cacheDuration): void
131
    {
132
        $this->cacheDuration = $cacheDuration;
133
    }
134
135
136
    /**
137
     * Get the XML element.
138
     *
139
     * @return \DOMElement
140
     */
141
    public function getXML(): DOMElement
142
    {
143
        return $this->xml;
144
    }
145
146
147
    /**
148
     * Set the XML element.
149
     *
150
     * @param \DOMElement $xml
151
     */
152
    protected function setXML(DOMElement $xml): void
153
    {
154
        $this->xml = $xml;
155
    }
156
157
158
    /**
159
     * @inheritDoc
160
     */
161
    protected function getOriginalXML(): DOMElement
162
    {
163
        return $this->xml ?? $this->toUnsignedXML();
164
    }
165
166
167
    /**
168
     * Create XML from this class
169
     *
170
     * @param \DOMElement|null $parent
171
     * @return \DOMElement
172
     */
173
    public function toXML(?DOMElement $parent = null): DOMElement
174
    {
175
        $e = $this->toUnsignedXML($parent);
176
177
        if ($this->signer !== null) {
178
            $signedXML = $this->doSign($e);
179
            $signedXML->insertBefore(
180
                $this->signature->toXML($signedXML),
181
                $signedXML->firstChild
182
            );
183
            return $signedXML;
184
        }
185
186
        return $e;
187
    }
188
189
190
    /**
191
     * @param \DOMElement|null $parent
192
     *
193
     * @return \DOMElement
194
     */
195
    protected function toUnsignedXML(DOMElement $parent = null): DOMElement
196
    {
197
        $e = $this->instantiateParentElement($parent);
198
199
        foreach ($this->getAttributesNS() as $attr) {
200
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
201
        }
202
203
        if ($this->Id !== null) {
204
            $e->setAttribute('ID', $this->Id);
205
        }
206
207
        if ($this->validUntil !== null) {
208
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->validUntil));
209
        }
210
211
        if ($this->cacheDuration !== null) {
212
            $e->setAttribute('cacheDuration', $this->cacheDuration);
213
        }
214
215
        if ($this->Extensions !== null && !$this->Extensions->isEmptyElement()) {
216
            $this->Extensions->toXML($e);
217
        }
218
219
        return $e;
220
    }
221
}
222