Passed
Branch assertions (18e068)
by Tim
03:23
created

SubjectConfirmationData::__construct()   C

Complexity

Conditions 11
Paths 161

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 25
nc 161
nop 1
dl 0
loc 36
rs 6.8083
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\saml;
6
7
use Assert\Assertion;
8
use RobRichards\XMLSecLibs\XMLSecurityDSig;
9
10
use SAML2\Constants;
11
use SAML2\Utils;
12
use SAML2\XML\Chunk;
13
use SAML2\XML\ds\KeyInfo;
14
15
/**
16
 * Class representing SAML 2 SubjectConfirmationData element.
17
 *
18
 * @package SimpleSAMLphp
19
 */
20
class SubjectConfirmationData
21
{
22
    /**
23
     * The time before this element is valid, as an unix timestamp.
24
     *
25
     * @var int|null
26
     */
27
    public $NotBefore = null;
28
29
    /**
30
     * The time after which this element is invalid, as an unix timestamp.
31
     *
32
     * @var int|null
33
     */
34
    public $NotOnOrAfter = null;
35
36
    /**
37
     * The Recipient this Subject is valid for. Either an entity or a location.
38
     *
39
     * @var string|null
40
     */
41
    public $Recipient = null;
42
43
    /**
44
     * The ID of the AuthnRequest this is a response to.
45
     *
46
     * @var string|null
47
     */
48
    public $InResponseTo = null;
49
50
    /**
51
     * The IP(v6) address of the user.
52
     *
53
     * @var string|null
54
     */
55
    public $Address = null;
56
57
    /**
58
     * The various key information elements.
59
     *
60
     * Array with various elements describing this key.
61
     * Unknown elements will be represented by \SAML2\XML\Chunk.
62
     *
63
     * @var (\SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk)[]
64
     */
65
    public $info = [];
66
67
68
    /**
69
     * Collect the value of the NotBefore-property
70
     * @return int|null
71
     */
72
    public function getNotBefore()
73
    {
74
        return $this->NotBefore;
75
    }
76
77
78
    /**
79
     * Set the value of the NotBefore-property
80
     * @param int|null $notBefore
81
     * @return void
82
     */
83
    public function setNotBefore(int $notBefore = null)
84
    {
85
        $this->NotBefore = $notBefore;
86
    }
87
88
89
    /**
90
     * Collect the value of the NotOnOrAfter-property
91
     * @return int|null
92
     */
93
    public function getNotOnOrAfter()
94
    {
95
        return $this->NotOnOrAfter;
96
    }
97
98
99
    /**
100
     * Set the value of the NotOnOrAfter-property
101
     * @param int|null $notOnOrAfter
102
     * @return void
103
     */
104
    public function setNotOnOrAfter(int $notOnOrAfter = null)
105
    {
106
        $this->NotOnOrAfter = $notOnOrAfter;
107
    }
108
109
110
    /**
111
     * Collect the value of the Recipient-property
112
     * @return string|null
113
     */
114
    public function getRecipient()
115
    {
116
        return $this->Recipient;
117
    }
118
119
120
    /**
121
     * Set the value of the Recipient-property
122
     * @param string|null $recipient
123
     * @return void
124
     */
125
    public function setRecipient(string $recipient = null)
126
    {
127
        $this->Recipient = $recipient;
128
    }
129
130
131
    /**
132
     * Collect the value of the InResponseTo-property
133
     * @return string|null
134
     */
135
    public function getInResponseTo()
136
    {
137
        return $this->InResponseTo;
138
    }
139
140
141
    /**
142
     * Set the value of the InResponseTo-property
143
     * @param string|null $inResponseTo
144
     * @return void
145
     */
146
    public function setInResponseTo(string $inResponseTo = null)
147
    {
148
        $this->InResponseTo = $inResponseTo;
149
    }
150
151
152
    /**
153
     * Collect the value of the Address-property
154
     * @return string|null
155
     */
156
    public function getAddress()
157
    {
158
        return $this->Address;
159
    }
160
161
162
    /**
163
     * Set the value of the Address-property
164
     * @param string|null $address
165
     * @return void
166
     */
167
    public function setAddress(string $address = null)
168
    {
169
        if (!is_null($address) && !filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
170
            throw new \InvalidArgumentException('Provided argument is not a valid IP address.');
171
        }
172
        $this->Address = $address;
173
    }
174
175
176
    /**
177
     * Collect the value of the info-property
178
     * @return (\SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk)[]
179
     */
180
    public function getInfo() : array
181
    {
182
        return $this->info;
183
    }
184
185
186
    /**
187
     * Set the value of the info-property
188
     * @param (\SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk)[] $info
189
     * @return void
190
     */
191
    public function setInfo(array $info)
192
    {
193
        $this->info = $info;
194
    }
195
196
197
    /**
198
     * Add the value to the info-property
199
     * @param \SAML2\XML\Chunk|\SAML2\XML\ds\KeyInfo $info
200
     * @return void
201
     */
202
    public function addInfo($info)
203
    {
204
        Assertion::true($info instanceof Chunk || $info instanceof KeyInfo);
205
        $this->info[] = $info;
206
    }
207
208
209
    /**
210
     * Initialize (and parse) a SubjectConfirmationData element.
211
     *
212
     * @param \DOMElement|null $xml The XML element we should load.
213
     */
214
    public function __construct(\DOMElement $xml = null)
215
    {
216
        if ($xml === null) {
217
            return;
218
        }
219
220
        if ($xml->hasAttribute('NotBefore')) {
221
            $this->setNotBefore(Utils::xsDateTimeToTimestamp($xml->getAttribute('NotBefore')));
222
        }
223
        if ($xml->hasAttribute('NotOnOrAfter')) {
224
            $this->setNotOnOrAfter(Utils::xsDateTimeToTimestamp($xml->getAttribute('NotOnOrAfter')));
225
        }
226
        if ($xml->hasAttribute('Recipient')) {
227
            $this->setRecipient($xml->getAttribute('Recipient'));
228
        }
229
        if ($xml->hasAttribute('InResponseTo')) {
230
            $this->setInResponseTo($xml->getAttribute('InResponseTo'));
231
        }
232
        if ($xml->hasAttribute('Address')) {
233
            $this->setAddress($xml->getAttribute('Address'));
234
        }
235
        for ($n = $xml->firstChild; $n !== null; $n = $n->nextSibling) {
236
            if (!($n instanceof \DOMElement)) {
237
                continue;
238
            }
239
            if ($n->namespaceURI !== XMLSecurityDSig::XMLDSIGNS) {
240
                $this->addInfo(new Chunk($n));
241
                continue;
242
            }
243
            switch ($n->localName) {
244
                case 'KeyInfo':
245
                    $this->addInfo(new KeyInfo($n));
246
                    break;
247
                default:
248
                    $this->addInfo(new Chunk($n));
249
                    break;
250
            }
251
        }
252
    }
253
254
255
    /**
256
     * Convert this element to XML.
257
     *
258
     * @param  \DOMElement $parent The parent element we should append this element to.
259
     * @return \DOMElement This element, as XML.
260
     */
261
    public function toXML(\DOMElement $parent)
262
    {
263
        $e = $parent->ownerDocument->createElementNS(Constants::NS_SAML, 'saml:SubjectConfirmationData');
264
        $parent->appendChild($e);
265
266
        if ($this->getNotBefore() !== null) {
267
            $e->setAttribute('NotBefore', gmdate('Y-m-d\TH:i:s\Z', $this->getNotBefore()));
268
        }
269
        if ($this->getNotOnOrAfter() !== null) {
270
            $e->setAttribute('NotOnOrAfter', gmdate('Y-m-d\TH:i:s\Z', $this->getNotOnOrAfter()));
271
        }
272
        if ($this->getRecipient() !== null) {
273
            $e->setAttribute('Recipient', $this->getRecipient());
274
        }
275
        if ($this->getInResponseTo() !== null) {
276
            $e->setAttribute('InResponseTo', $this->getInResponseTo());
277
        }
278
        if ($this->getAddress() !== null) {
279
            $e->setAttribute('Address', $this->getAddress());
280
        }
281
        /** @var \SAML2\XML\ds\KeyInfo|\SAML2\XML\Chunk $n */
282
        foreach ($this->getInfo() as $n) {
283
            $n->toXML($e);
284
        }
285
286
        return $e;
287
    }
288
}
289