RequestAuthenticated   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 29
dl 0
loc 93
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMustUnderstand() 0 3 1
A fromXML() 0 35 2
A __construct() 0 3 1
A toXML() 0 11 2
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\Exception\ProtocolViolationException;
10
use SimpleSAML\SOAP\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingAttributeException;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
16
use function boolval;
17
use function strval;
18
19
/**
20
 * Class representing the ECP RequestAuthenticated element.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class RequestAuthenticated extends AbstractEcpElement implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
28
29
    /**
30
     * Create a ECP RequestAuthenticated element.
31
     *
32
     * @param bool|null $mustUnderstand
33
     */
34
    public function __construct(
35
        protected ?bool $mustUnderstand = false,
36
    ) {
37
    }
38
39
40
    /**
41
     * Collect the value of the mustUnderstand-property
42
     *
43
     * @return bool|null
44
     */
45
    public function getMustUnderstand(): ?bool
46
    {
47
        return $this->mustUnderstand;
48
    }
49
50
51
    /**
52
     * Convert XML into a RequestAuthenticated
53
     *
54
     * @param \DOMElement $xml The XML element we should load
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
58
     *   if the qualified name of the supplied element is wrong
59
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
60
     *   if the supplied element is missing any of the mandatory attributes
61
     */
62
    public static function fromXML(DOMElement $xml): static
63
    {
64
        Assert::same($xml->localName, 'RequestAuthenticated', InvalidDOMElementException::class);
65
        Assert::same($xml->namespaceURI, RequestAuthenticated::NS, InvalidDOMElementException::class);
66
67
        // Assert required attributes
68
        Assert::true(
69
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
70
            'Missing env:actor attribute in <ecp:RequestAuthenticated>.',
71
            MissingAttributeException::class,
72
        );
73
74
        $mustUnderstand = null;
75
        if ($xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand')) {
76
            $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
77
78
            Assert::nullOrOneOf(
79
                $mustUnderstand,
80
                ['0', '1'],
81
                'Invalid value of env:mustUnderstand attribute in <ecp:RequestAuthenticated>.',
82
                ProtocolViolationException::class,
83
            );
84
85
            $mustUnderstand = boolval($mustUnderstand);
86
        }
87
88
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
89
        Assert::same(
90
            $actor,
91
            'http://schemas.xmlsoap.org/soap/actor/next',
92
            'Invalid value of env:actor attribute in <ecp:RequestAuthenticated>.',
93
            ProtocolViolationException::class,
94
        );
95
96
        return new static($mustUnderstand);
97
    }
98
99
100
    /**
101
     * Convert this ECP RequestAuthentication to XML.
102
     *
103
     * @param \DOMElement|null $parent The element we should append this element to.
104
     * @return \DOMElement
105
     */
106
    public function toXML(?DOMElement $parent = null): DOMElement
107
    {
108
        $e = $this->instantiateParentElement($parent);
109
110
        if ($this->getMustUnderstand() !== null) {
111
            $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', strval(intval($this->getMustUnderstand())));
112
        }
113
114
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
115
116
        return $e;
117
    }
118
}
119