RelayState::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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