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