RelayState::toXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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