Passed
Pull Request — master (#374)
by Tim
02:41
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\SOAP11\Constants as C;
12
use SimpleSAML\SOAP11\Type\MustUnderstandValue;
13
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
14
use SimpleSAML\XML\TypedTextContentTrait;
15
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingAttributeException};
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 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...
26
27
    /** @var string */
28
    public const TEXTCONTENT_TYPE = SAMLStringValue::class;
29
30
31
    /**
32
     * Convert XML into a RelayState
33
     *
34
     * @param \DOMElement $xml The XML element we should load
35
     * @return static
36
     *
37
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
38
     *   if the qualified name of the supplied element is wrong
39
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
40
     *   if the supplied element is missing any of the mandatory attributes
41
     */
42
    public static function fromXML(DOMElement $xml): static
43
    {
44
        Assert::same($xml->localName, 'RelayState', InvalidDOMElementException::class);
45
        Assert::same($xml->namespaceURI, RelayState::NS, InvalidDOMElementException::class);
46
47
        // Assert required attributes
48
        Assert::true(
49
            $xml->hasAttributeNS(C::NS_SOAP_ENV, 'actor'),
50
            'Missing env:actor attribute in <ecp:RelayState>.',
51
            MissingAttributeException::class,
52
        );
53
        Assert::true(
54
            $xml->hasAttributeNS(C::NS_SOAP_ENV, 'mustUnderstand'),
55
            'Missing env:mustUnderstand attribute in <ecp:RelayState>.',
56
            MissingAttributeException::class,
57
        );
58
59
        MustUnderstandValue::fromString($xml->getAttributeNS(C::NS_SOAP_ENV, 'mustUnderstand'));
60
61
        Assert::same(
62
            $xml->getAttributeNS(C::NS_SOAP_ENV, 'actor'),
63
            C::SOAP_ACTOR_NEXT,
64
            'Invalid value of env:actor attribute in <ecp:RelayState>.',
65
            ProtocolViolationException::class,
66
        );
67
68
        return new static(
69
            SAMLStringValue::fromString($xml->textContent),
70
        );
71
    }
72
73
74
    /**
75
     * Convert this ECP RelayState to XML.
76
     *
77
     * @param \DOMElement|null $parent The element we should append this element to.
78
     * @return \DOMElement
79
     */
80
    public function toXML(?DOMElement $parent = null): DOMElement
81
    {
82
        $e = $this->instantiateParentElement($parent);
83
84
        $e->setAttributeNS(C::NS_SOAP_ENV, 'SOAP-ENV:mustUnderstand', '1');
85
        $e->setAttributeNS(C::NS_SOAP_ENV, 'SOAP-ENV:actor', C::SOAP_ACTOR_NEXT);
86
        $e->textContent = $this->getContent()->getValue();
87
88
        return $e;
89
    }
90
}
91