Passed
Push — master ( be1689...d3e6a5 )
by Tim
11:14
created

RequestAuthenticated::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\ecp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingAttributeException;
13
14
use function is_null;
15
use function is_numeric;
16
use function strval;
17
18
/**
19
 * Class representing the ECP RequestAuthenticated element.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class RequestAuthenticated extends AbstractEcpElement
24
{
25
    /** @var int|null $mustUnderstand */
26
    protected $mustUnderstand = null;
27
28
29
    /**
30
     * Create a ECP RequestAuthenticated element.
31
     *
32
     * @param int $mustUnderstand
33
     */
34
    public function __construct(int $mustUnderstand)
35
    {
36
        $this->setMustUnderstand($mustUnderstand);
37
    }
38
39
40
    /**
41
     * Collect the value of the mustUnderstand-property
42
     *
43
     * @return int
44
     */
45
    public function getMustUnderstand(): int
46
    {
47
        return $this->mustUnderstand;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->mustUnderstand could return the type null which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
48
    }
49
50
51
    /**
52
     * Set the value of the mustUnderstand-property
53
     *
54
     * @param int $mustUnderstand
55
     */
56
    private function setMustUnderstand(int $mustUnderstand): void
57
    {
58
        Assert::oneOf(
59
            $mustUnderstand,
60
            [0, 1],
61
            'Invalid value of SOAP-ENV:mustUnderstand attribute in <ecp:Response>.',
62
            ProtocolViolationException::class,
63
        );
64
        $this->mustUnderstand = $mustUnderstand;
65
    }
66
67
68
    /**
69
     * Convert XML into a RequestAuthenticated
70
     *
71
     * @param \DOMElement $xml The XML element we should load
72
     * @return self
73
     *
74
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
75
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing any of the mandatory attributes
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'RequestAuthenticated', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, RequestAuthenticated::NS, InvalidDOMElementException::class);
81
82
        // Assert required attributes
83
        Assert::true(
84
            $xml->hasAttributeNS(C::NS_SOAP, 'actor'),
85
            'Missing SOAP-ENV:actor attribute in <ecp:RequestAuthenticated>.',
86
            MissingAttributeException::class
87
        );
88
89
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP, 'mustUnderstand');
90
        $actor = $xml->getAttributeNS(C::NS_SOAP, 'actor');
91
92
        Assert::oneOf(
93
            $mustUnderstand,
94
            ['', '0', '1'],
95
            'Invalid value of SOAP-ENV:mustUnderstand attribute in <ecp:Response>.',
96
            ProtocolViolationException::class,
97
        );
98
        Assert::same(
99
            $actor,
100
            'http://schemas.xmlsoap.org/soap/actor/next',
101
            'Invalid value of SOAP-ENV:actor attribute in <ecp:Response>.',
102
            ProtocolViolationException::class,
103
        );
104
105
        $mustUnderstand = intval($mustUnderstand);
106
107
        return new static($mustUnderstand);
108
    }
109
110
111
    /**
112
     * Convert this ECP RequestAuthentication to XML.
113
     *
114
     * @param \DOMElement|null $parent The element we should append this element to.
115
     * @return \DOMElement
116
     */
117
    public function toXML(DOMElement $parent = null): DOMElement
118
    {
119
        $response = $this->instantiateParentElement($parent);
120
121
        $response->setAttributeNS(C::NS_SOAP, 'SOAP-ENV:mustUnderstand', strval($this->getMustUnderstand()));
122
        $response->setAttributeNS(C::NS_SOAP, 'SOAP-ENV:actor', 'http://schemas.xmlsoap.org/soap/actor/next');
123
124
        return $response;
125
    }
126
}
127