AbstractMetadataDocument::getOriginalXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
10
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
11
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
12
use SimpleSAML\SAML2\XML\mdrpi\PublicationInfo;
13
use SimpleSAML\SAML2\XML\mdrpi\PublicationPath;
14
use SimpleSAML\SAML2\XML\mdrpi\RegistrationInfo;
15
use SimpleSAML\SAML2\XML\mdui\DiscoHints;
16
use SimpleSAML\SAML2\XML\mdui\UIInfo;
17
use SimpleSAML\XMLSchema\Type\DurationValue;
18
use SimpleSAML\XMLSchema\Type\IDValue;
19
20
/**
21
 * Class to represent a metadata document
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
abstract class AbstractMetadataDocument extends AbstractSignedMdElement
26
{
27
    use ExtendableElementTrait;
28
29
30
    /**
31
     * Generic constructor for SAML metadata documents.
32
     *
33
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $id The ID for this document. Defaults to null.
34
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil Unix time of validity for this document.
35
     *   Defaults to null.
36
     * @param \SimpleSAML\XMLSchema\Type\DurationValue|null $cacheDuration Maximum time this document can be cached.
37
     *   Defaults to null.
38
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to null.
39
     */
40
    public function __construct(
41
        protected ?IDValue $id = null,
42
        protected ?SAMLDateTimeValue $validUntil = null,
43
        protected ?DurationValue $cacheDuration = null,
44
        ?Extensions $extensions = null,
45
    ) {
46
        if ($extensions !== null) {
47
            $exts = $extensions->getList();
48
49
            /**
50
             * MDUI 2.1: this element MUST NOT appear more than once within a given <md:Extensions> element.
51
             */
52
            $uiInfo = array_values(array_filter($exts, function ($ext) {
53
                return $ext instanceof UIInfo;
54
            }));
55
            Assert::maxCount($uiInfo, 1, ProtocolViolationException::class);
56
57
            /**
58
             * MDUI 2.2: this element MUST NOT appear more than once within a given <md:Extensions> element.
59
             */
60
            $discoHints = array_values(array_filter($exts, function ($ext) {
61
                return $ext instanceof DiscoHints;
62
            }));
63
            Assert::maxCount($discoHints, 1, ProtocolViolationException::class);
64
65
            /**
66
             * MDRPI 2.1: this element MUST NOT appear more than once within a given <md:Extensions> element.
67
             */
68
            $regInfo = array_values(array_filter($exts, function ($ext) {
69
                return $ext instanceof RegistrationInfo;
70
            }));
71
            Assert::maxCount($regInfo, 1, ProtocolViolationException::class);
72
73
            /**
74
             * MDRPI 2.2: this element MUST NOT appear more than once within a given <md:Extensions> element.
75
             */
76
            $pubInfo = array_values(array_filter($exts, function ($ext) {
0 ignored issues
show
Unused Code introduced by
The assignment to $pubInfo is dead and can be removed.
Loading history...
77
                return $ext instanceof PublicationInfo;
78
            }));
79
            Assert::maxCount($regInfo, 1, ProtocolViolationException::class);
80
81
            /**
82
             * MDRPI 2.3: The <mdrpi:PublicationPath> element MUST NOT appear more than once within the
83
             * <md:Extensions> element of a given <md:EntitiesDescriptor> or <md:EntityDescriptor> element.
84
             */
85
            $pubPath = array_values(array_filter($exts, function ($ext) {
86
                return $ext instanceof PublicationPath;
87
            }));
88
            Assert::maxCount($pubPath, 1, ProtocolViolationException::class);
89
        }
90
91
        $this->setExtensions($extensions);
92
    }
93
94
95
    /**
96
     * Collect the value of the id property.
97
     *
98
     * @return \SimpleSAML\XMLSchema\Type\IDValue|null
99
     */
100
    public function getId(): ?IDValue
101
    {
102
        return $this->id;
103
    }
104
105
106
    /**
107
     * Collect the value of the validUntil property.
108
     *
109
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
110
     */
111
    public function getValidUntil(): ?SAMLDateTimeValue
112
    {
113
        return $this->validUntil;
114
    }
115
116
117
    /**
118
     * Collect the value of the cacheDuration property.
119
     *
120
     * @return \SimpleSAML\XMLSchema\Type\DurationValue|null
121
     */
122
    public function getCacheDuration(): ?DurationValue
123
    {
124
        return $this->cacheDuration;
125
    }
126
127
128
    /**
129
     * @return \DOMElement
130
     */
131
    protected function getOriginalXML(): DOMElement
132
    {
133
        return $this->isSigned() ? $this->getXML() : $this->toUnsignedXML();
134
    }
135
136
137
    /**
138
     * @param \DOMElement|null $parent
139
     *
140
     * @return \DOMElement
141
     */
142
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
143
    {
144
        $e = $this->instantiateParentElement($parent);
145
146
        if ($this->getId() !== null) {
147
            $e->setAttribute('ID', $this->getId()->getValue());
148
        }
149
150
        if ($this->getValidUntil() !== null) {
151
            $e->setAttribute('validUntil', $this->getValidUntil()->getValue());
152
        }
153
154
        if ($this->getCacheDuration() !== null) {
155
            $e->setAttribute('cacheDuration', $this->getCacheDuration()->getValue());
156
        }
157
158
        $extensions = $this->getExtensions();
159
        if ($extensions !== null && !$extensions->isEmptyElement()) {
160
            $extensions->toXML($e);
161
        }
162
163
        return $e;
164
    }
165
}
166