AbstractSubjectConfirmationData::toXML()   B
last analyzed

Complexity

Conditions 8
Paths 128

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 128
nop 1
dl 0
loc 29
rs 8.2111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\AssertionFailedException;
9
use SimpleSAML\SAML2\Assert\Assert;
10
use SimpleSAML\SAML2\Constants as C;
11
use SimpleSAML\SAML2\Type\EntityIDValue;
12
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
13
use SimpleSAML\SAML2\Type\SAMLStringValue;
14
use SimpleSAML\SAML2\Utils;
15
use SimpleSAML\XMLSchema\Type\NCNameValue;
16
use SimpleSAML\XMLSchema\XML\AbstractAnyType;
17
use SimpleSAML\XMLSchema\XML\Constants\NS;
18
19
use function strval;
20
21
/**
22
 * Abstract class representing SAML 2 SubjectConfirmationData element.
23
 *
24
 * @package simplesamlphp/saml2
25
 */
26
abstract class AbstractSubjectConfirmationData extends AbstractAnyType
27
{
28
    /** @var string */
29
    public const NS = C::NS_SAML;
30
31
    /** @var string */
32
    public const NS_PREFIX = 'saml';
33
34
    /** @var string */
35
    public const SCHEMA = 'resources/schemas/saml-schema-assertion-2.0.xsd';
36
37
    /** The namespace-attribute for the xs:any element */
38
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
39
40
    /** The namespace-attribute for the xs:anyAttribute element */
41
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
42
43
44
    /**
45
     * Initialize (and parse) a SubjectConfirmationData element.
46
     *
47
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notBefore
48
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $notOnOrAfter
49
     * @param \SimpleSAML\SAML2\Type\EntityIDValue|null $recipient
50
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo
51
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $address
52
     * @param \SimpleSAML\XML\SerializableElementInterface[] $children
53
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\list 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...
54
     */
55
    public function __construct(
56
        protected ?SAMLDateTimeValue $notBefore = null,
57
        protected ?SAMLDateTimeValue $notOnOrAfter = null,
58
        protected ?EntityIDValue $recipient = null,
59
        protected ?NCNameValue $inResponseTo = null,
60
        protected ?SAMLStringValue $address = null,
61
        array $children = [],
62
        array $namespacedAttributes = [],
63
    ) {
64
        if ($address !== null) {
65
            try {
66
                /**
67
                 * IPv4 addresses SHOULD be represented in the usual dotted-decimal format (e.g., "1.2.3.4").
68
                 * IPv6 addresses SHOULD be represented as defined by Section 2.2 of IETF RFC 3513 [RFC 3513]
69
                 * (e.g., "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210").
70
                 */
71
                Assert::ip($address->getValue());
72
            } catch (AssertionFailedException) {
73
                Utils::getContainer()->getLogger()->warning(
74
                    sprintf('Provided address (%s) is not a valid IPv4 or IPv6  address.', $address->getValue()),
75
                );
76
            }
77
        }
78
79
        $this->setElements($children);
80
        $this->setAttributesNS($namespacedAttributes);
81
    }
82
83
84
    /**
85
     * Collect the value of the NotBefore-property
86
     *
87
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
88
     */
89
    public function getNotBefore(): ?SAMLDateTimeValue
90
    {
91
        return $this->notBefore;
92
    }
93
94
95
    /**
96
     * Collect the value of the NotOnOrAfter-property
97
     *
98
     * @return \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null
99
     */
100
    public function getNotOnOrAfter(): ?SAMLDateTimeValue
101
    {
102
        return $this->notOnOrAfter;
103
    }
104
105
106
    /**
107
     * Collect the value of the Recipient-property
108
     *
109
     * @return \SimpleSAML\SAML2\Type\EntityIDValue|null
110
     */
111
    public function getRecipient(): ?EntityIDValue
112
    {
113
        return $this->recipient;
114
    }
115
116
117
    /**
118
     * Collect the value of the InResponseTo-property
119
     *
120
     * @return \SimpleSAML\XMLSchema\Type\NCNameValue|null
121
     */
122
    public function getInResponseTo(): ?NCNameValue
123
    {
124
        return $this->inResponseTo;
125
    }
126
127
128
    /**
129
     * Collect the value of the Address-property
130
     *
131
     * @return \SimpleSAML\SAML2\Type\SAMLStringValue|null
132
     */
133
    public function getAddress(): ?SAMLStringValue
134
    {
135
        return $this->address;
136
    }
137
138
139
    /**
140
     * Test if an object, at the state it's in, would produce an empty XML-element
141
     *
142
     * @return bool
143
     */
144
    public function isEmptyElement(): bool
145
    {
146
        return empty($this->getNotBefore())
147
            && empty($this->getNotOnOrAfter())
148
            && empty($this->getRecipient())
149
            && empty($this->getInResponseTo())
150
            && empty($this->getAddress())
151
            && empty($this->getElements())
152
            && empty($this->getAttributesNS());
153
    }
154
155
156
    /**
157
     * Convert this element to XML.
158
     *
159
     * @param  \DOMElement|null $parent The parent element we should append this element to.
160
     * @return \DOMElement This element, as XML.
161
     */
162
    public function toXML(?DOMElement $parent = null): DOMElement
163
    {
164
        $e = $this->instantiateParentElement($parent);
165
166
        if ($this->getNotBefore() !== null) {
167
            $e->setAttribute('NotBefore', strval($this->getNotBefore()));
168
        }
169
        if ($this->getNotOnOrAfter() !== null) {
170
            $e->setAttribute('NotOnOrAfter', strval($this->getNotOnOrAfter()));
171
        }
172
        if ($this->getRecipient() !== null) {
173
            $e->setAttribute('Recipient', strval($this->getRecipient()));
174
        }
175
        if ($this->getInResponseTo() !== null) {
176
            $e->setAttribute('InResponseTo', strval($this->getInResponseTo()));
177
        }
178
        if ($this->getAddress() !== null) {
179
            $e->setAttribute('Address', strval($this->getAddress()));
180
        }
181
182
        foreach ($this->getAttributesNS() as $attr) {
183
            $attr->toXML($e);
184
        }
185
186
        foreach ($this->getElements() as $n) {
187
            $n->toXML($e);
188
        }
189
190
        return $e;
191
    }
192
}
193