Conditions::isEmptyElement()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 6
nc 6
nop 0
dl 0
loc 8
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Type\SAMLDateTimeValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
15
16
use function array_pop;
17
18
/**
19
 * Class representing SAML 2 Conditions element.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class Conditions extends AbstractSamlElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\AbstractSamlElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
{
25
    use SchemaValidatableElementTrait;
26
27
28
    /**
29
     * Initialize a Conditions element.
30
     *
31
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notBefore
32
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notOnOrAfter
33
     * @param \SimpleSAML\SAML2\XML\saml\AbstractCondition[] $condition
34
     * @param \SimpleSAML\SAML2\XML\saml\AudienceRestriction[] $audienceRestriction
35
     * @param \SimpleSAML\SAML2\XML\saml\OneTimeUse|null $oneTimeUse
36
     * @param \SimpleSAML\SAML2\XML\saml\ProxyRestriction|null $proxyRestriction
37
     */
38
    public function __construct(
39
        protected ?SAMLDateTimeValue $notBefore = null,
40
        protected ?SAMLDateTimeValue $notOnOrAfter = null,
41
        protected array $condition = [],
42
        protected array $audienceRestriction = [],
43
        protected ?OneTimeUse $oneTimeUse = null,
44
        protected ?ProxyRestriction $proxyRestriction = null,
45
    ) {
46
        /** SAML 2.0 Core specifications paragraph 2.5.1.2 */
47
        if ($notBefore !== null && $notOnOrAfter !== null) {
48
            Assert::true(
49
                $notBefore->toDateTime() < $notOnOrAfter->toDateTime(),
50
                "The value for NotBefore MUST be less than (earlier than) the value for NotOnOrAfter.",
51
                ProtocolViolationException::class,
52
            );
53
        }
54
55
        Assert::maxCount($condition, C::UNBOUNDED_LIMIT);
56
        Assert::allIsInstanceOf($condition, AbstractCondition::class);
57
        Assert::maxCount($audienceRestriction, C::UNBOUNDED_LIMIT);
58
        Assert::allIsInstanceOf($audienceRestriction, AudienceRestriction::class);
59
    }
60
61
62
    /**
63
     * Collect the value of the notBefore-property
64
     *
65
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
66
     */
67
    public function getNotBefore(): ?SAMLDateTimeValue
68
    {
69
        return $this->notBefore;
70
    }
71
72
73
    /**
74
     * Collect the value of the notOnOrAfter-property
75
     *
76
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
77
     */
78
    public function getNotOnOrAfter(): ?SAMLDateTimeValue
79
    {
80
        return $this->notOnOrAfter;
81
    }
82
83
84
    /**
85
     * Collect the value of the condition-property
86
     *
87
     * @return \SimpleSAML\SAML2\XML\saml\AbstractCondition[]
88
     */
89
    public function getCondition(): array
90
    {
91
        return $this->condition;
92
    }
93
94
95
    /**
96
     * Collect the value of the audienceRestriction-property
97
     *
98
     * @return \SimpleSAML\SAML2\XML\saml\AudienceRestriction[]
99
     */
100
    public function getAudienceRestriction(): array
101
    {
102
        return $this->audienceRestriction;
103
    }
104
105
106
    /**
107
     * Collect the value of the oneTimeUse-property
108
     *
109
     * @return \SimpleSAML\SAML2\XML\saml\OneTimeUse|null
110
     */
111
    public function getOneTimeUse(): ?OneTimeUse
112
    {
113
        return $this->oneTimeUse;
114
    }
115
116
117
    /**
118
     * Collect the value of the proxyRestriction-property
119
     *
120
     * @return \SimpleSAML\SAML2\XML\saml\ProxyRestriction|null
121
     */
122
    public function getProxyRestriction(): ?ProxyRestriction
123
    {
124
        return $this->proxyRestriction;
125
    }
126
127
128
    /**
129
     * Test if an object, at the state it's in, would produce an empty XML-element
130
     */
131
    public function isEmptyElement(): bool
132
    {
133
        return empty($this->getNotBefore())
134
            && empty($this->getNotOnOrAfter())
135
            && empty($this->getCondition())
136
            && empty($this->getAudienceRestriction())
137
            && empty($this->getOneTimeUse())
138
            && empty($this->getProxyRestriction());
139
    }
140
141
142
    /**
143
     * Convert XML into a Conditions object
144
     *
145
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
146
     *   if the qualified name of the supplied element is wrong
147
     */
148
    public static function fromXML(DOMElement $xml): static
149
    {
150
        Assert::same($xml->localName, 'Conditions', InvalidDOMElementException::class);
151
        Assert::same($xml->namespaceURI, Conditions::NS, InvalidDOMElementException::class);
152
153
        $condition = AbstractCondition::getChildrenOfClass($xml);
154
        $audienceRestriction = AudienceRestriction::getChildrenOfClass($xml);
155
        $oneTimeUse = OneTimeUse::getChildrenOfClass($xml);
156
        $proxyRestriction = ProxyRestriction::getChildrenOfClass($xml);
157
158
        Assert::maxCount(
159
            $oneTimeUse,
160
            1,
161
            'There MUST occur at most one <saml:OneTimeUse> element inside a <saml:Conditions>',
162
            ProtocolViolationException::class,
163
        );
164
        Assert::maxCount(
165
            $proxyRestriction,
166
            1,
167
            'There MUST occur at most one <saml:ProxyRestriction> element inside a <saml:Conditions>',
168
            ProtocolViolationException::class,
169
        );
170
171
        return new static(
172
            self::getOptionalAttribute($xml, 'NotBefore', SAMLDateTimeValue::class, null),
173
            self::getOptionalAttribute($xml, 'NotOnOrAfter', SAMLDateTimeValue::class, null),
174
            $condition,
175
            $audienceRestriction,
176
            array_pop($oneTimeUse),
177
            array_pop($proxyRestriction),
178
        );
179
    }
180
181
182
    /**
183
     * Convert this element to XML.
184
     */
185
    public function toXML(?DOMElement $parent = null): DOMElement
186
    {
187
        $e = $this->instantiateParentElement($parent);
188
189
        if ($this->getNotBefore() !== null) {
190
            $e->setAttribute('NotBefore', $this->getNotBefore()->getValue());
191
        }
192
193
        if ($this->getNotOnOrAfter() !== null) {
194
            $e->setAttribute('NotOnOrAfter', $this->getNotOnOrAfter()->getValue());
195
        }
196
197
        foreach ($this->getCondition() as $condition) {
198
            $condition->toXML($e);
199
        }
200
201
        foreach ($this->getAudienceRestriction() as $audienceRestriction) {
202
            $audienceRestriction->toXML($e);
203
        }
204
205
        $this->getOneTimeUse()?->toXML($e);
206
        $this->getProxyRestriction()?->toXML($e);
207
208
        return $e;
209
    }
210
}
211