Passed
Pull Request — master (#341)
by Tim
11:30 queued 09:12
created

RelayState::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 10
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\SOAP\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingAttributeException;
13
use SimpleSAML\XML\Exception\TooManyElementsException;
14
use SimpleSAML\XML\Exception\SchemaViolationException;
15
use SimpleSAML\XML\StringElementTrait;
16
17
use function boolval;
18
use function intval;
19
use function strval;
20
21
/**
22
 * Class representing the ECP RelayState element.
23
 *
24
 * @package simplesamlphp/saml2
25
 */
26
final class RelayState extends AbstractEcpElement
27
{
28
    use StringElementTrait;
29
30
    /**
31
     * Create a ECP RelayState element.
32
     *
33
     * @param bool $mustUnderstand
34
     * @param string $relayState
35
     */
36
    public function __construct(
37
        protected bool $mustUnderstand,
38
        string $relayState,
39
    ) {
40
        $this->setContent($relayState);
41
    }
42
43
44
    /**
45
     * Collect the value of the mustUnderstand-property
46
     *
47
     * @return bool
48
     */
49
    public function getMustUnderstand(): bool
50
    {
51
        return $this->mustUnderstand;
52
    }
53
54
55
    /**
56
     * Convert XML into a RelayState
57
     *
58
     * @param \DOMElement $xml The XML element we should load
59
     * @return static
60
     *
61
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
62
     *   if the qualified name of the supplied element is wrong
63
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
64
     *   if the supplied element is missing any of the mandatory attributes
65
     */
66
    public static function fromXML(DOMElement $xml): static
67
    {
68
        Assert::same($xml->localName, 'RelayState', InvalidDOMElementException::class);
69
        Assert::same($xml->namespaceURI, RelayState::NS, InvalidDOMElementException::class);
70
71
        // Assert required attributes
72
        Assert::true(
73
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
74
            'Missing env:actor attribute in <ecp:RelayState>.',
75
            MissingAttributeException::class,
76
        );
77
        Assert::true(
78
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand'),
79
            'Missing env:mustUnderstand attribute in <ecp:RelayState>.',
80
            MissingAttributeException::class,
81
        );
82
83
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
84
        $mustUnderstand = ($mustUnderstand === '') ? null : boolval(intval($mustUnderstand));
85
        Assert::nullOrBoolean(
86
            $mustUnderstand,
87
            'Invalid value of env:mustUnderstand attribute in <ecp:RelayState>.',
88
            ProtocolViolationException::class,
89
        );
90
91
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
92
        Assert::same(
93
            $actor,
94
            C::SOAP_ACTOR_NEXT,
95
            'Invalid value of env:actor attribute in <ecp:RelayState>.',
96
            ProtocolViolationException::class,
97
        );
98
99
        return new static($mustUnderstand, $xml->textContent);
0 ignored issues
show
Bug introduced by
It seems like $mustUnderstand can also be of type null; however, parameter $mustUnderstand of SimpleSAML\SAML2\XML\ecp\RelayState::__construct() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        return new static(/** @scrutinizer ignore-type */ $mustUnderstand, $xml->textContent);
Loading history...
100
    }
101
102
103
    /**
104
     * Convert this ECP RelayState to XML.
105
     *
106
     * @param \DOMElement|null $parent The element we should append this element to.
107
     * @return \DOMElement
108
     */
109
    public function toXML(DOMElement $parent = null): DOMElement
110
    {
111
        $e = $this->instantiateParentElement($parent);
112
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', strval(intval($this->getMustUnderstand())));
113
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
114
        $e->textContent = $this->getContent();
115
116
        return $e;
117
    }
118
}
119