Passed
Push — master ( 8cd786...a11f07 )
by Tim
04:16 queued 02:07
created

RelayState::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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\Exception\TooManyElementsException;
14
use SimpleSAML\XML\Exception\SchemaViolationException;
15
use SimpleSAML\XML\StringElementTrait;
16
17
use function boolval;
18
use function intval;
19
use function strval;
20
21
/**
22
 * Class representing the ECP RelayState element.
23
 *
24
 * @package simplesamlphp/saml2
25
 */
26
final class RelayState extends AbstractEcpElement
27
{
28
    use StringElementTrait;
29
30
    /**
31
     * Create a ECP RelayState element.
32
     *
33
     * @param string $relayState
34
     */
35
    public function __construct(
36
        string $relayState,
37
    ) {
38
        $this->setContent($relayState);
39
    }
40
41
42
    /**
43
     * Convert XML into a RelayState
44
     *
45
     * @param \DOMElement $xml The XML element we should load
46
     * @return static
47
     *
48
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
49
     *   if the qualified name of the supplied element is wrong
50
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
51
     *   if the supplied element is missing any of the mandatory attributes
52
     */
53
    public static function fromXML(DOMElement $xml): static
54
    {
55
        Assert::same($xml->localName, 'RelayState', InvalidDOMElementException::class);
56
        Assert::same($xml->namespaceURI, RelayState::NS, InvalidDOMElementException::class);
57
58
        // Assert required attributes
59
        Assert::true(
60
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
61
            'Missing env:actor attribute in <ecp:RelayState>.',
62
            MissingAttributeException::class,
63
        );
64
        Assert::true(
65
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand'),
66
            'Missing env:mustUnderstand attribute in <ecp:RelayState>.',
67
            MissingAttributeException::class,
68
        );
69
70
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
71
        Assert::same(
72
            $mustUnderstand,
73
            '1',
74
            'Invalid value of env:mustUnderstand attribute in <ecp:RelayState>.',
75
            ProtocolViolationException::class,
76
        );
77
78
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
79
        Assert::same(
80
            $actor,
81
            C::SOAP_ACTOR_NEXT,
82
            'Invalid value of env:actor attribute in <ecp:RelayState>.',
83
            ProtocolViolationException::class,
84
        );
85
86
        return new static($xml->textContent);
87
    }
88
89
90
    /**
91
     * Convert this ECP RelayState to XML.
92
     *
93
     * @param \DOMElement|null $parent The element we should append this element to.
94
     * @return \DOMElement
95
     */
96
    public function toXML(DOMElement $parent = null): DOMElement
97
    {
98
        $e = $this->instantiateParentElement($parent);
99
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', '1');
100
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
101
        $e->textContent = $this->getContent();
102
103
        return $e;
104
    }
105
}
106