Passed
Pull Request — master (#374)
by Tim
02:32
created

RelayState::__construct()   A

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