Passed
Push — master ( eb6d7f...017f7a )
by Tim
02:40
created

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