Passed
Push — master ( c66cd3...78be34 )
by Tim
12:20
created

AbstractMetadataDocument::getValidUntil()   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\SchemaViolationException;
10
use SimpleSAML\XML\ExtendableAttributesTrait;
11
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
12
13
use function gmdate;
14
15
/**
16
 * Class to represent a metadata document
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
abstract class AbstractMetadataDocument extends AbstractSignedMdElement
21
{
22
    use ExtendableElementTrait;
23
24
25
    /**
26
     * Generic constructor for SAML metadata documents.
27
     *
28
     * @param string|null $id The ID for this document. Defaults to null.
29
     * @param int|null    $validUntil Unix time of validity for this document. Defaults to null.
30
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
31
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to null.
32
     */
33
    public function __construct(
34
        protected ?string $id = null,
35
        protected ?int $validUntil = null,
36
        protected ?string $cacheDuration = null,
37
        ?Extensions $extensions = null,
38
    ) {
39
        Assert::nullOrValidNCName($id, SchemaViolationException::class);
40
        Assert::nullOrValidDuration($cacheDuration, SchemaViolationException::class);
41
42
        $this->setExtensions($extensions);
43
    }
44
45
46
    /**
47
     * Collect the value of the id property.
48
     *
49
     * @return string|null
50
     */
51
    public function getId(): ?string
52
    {
53
        return $this->id;
54
    }
55
56
57
    /**
58
     * Collect the value of the validUntil property.
59
     *
60
     * @return int|null
61
     */
62
    public function getValidUntil(): ?int
63
    {
64
        return $this->validUntil;
65
    }
66
67
68
    /**
69
     * Collect the value of the cacheDuration property.
70
     *
71
     * @return string|null
72
     */
73
    public function getCacheDuration(): ?string
74
    {
75
        return $this->cacheDuration;
76
    }
77
78
79
    /**
80
     * @return \DOMElement
81
     */
82
    protected function getOriginalXML(): DOMElement
83
    {
84
        return $this->xml ?? $this->toUnsignedXML();
85
    }
86
87
88
    /**
89
     * @param \DOMElement|null $parent
90
     *
91
     * @return \DOMElement
92
     */
93
    public function toUnsignedXML(DOMElement $parent = null): DOMElement
94
    {
95
        $e = $this->instantiateParentElement($parent);
96
97
        if ($this->getId() !== null) {
98
            $e->setAttribute('ID', $this->getId());
99
        }
100
101
        if ($this->getValidUntil() !== null) {
102
            $e->setAttribute('validUntil', gmdate('Y-m-d\TH:i:s\Z', $this->getValidUntil()));
103
        }
104
105
        if ($this->getCacheDuration() !== null) {
106
            $e->setAttribute('cacheDuration', $this->getCacheDuration());
107
        }
108
109
        $extensions = $this->getExtensions();
110
        if ($extensions !== null && !$extensions->isEmptyElement()) {
111
            $extensions->toXML($e);
112
        }
113
114
        return $e;
115
    }
116
}
117