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

SubjectConfirmation::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
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\SAML2\XML\saml\SubjectConfirmationData;
11
use SimpleSAML\SOAP\Constants as C;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\Exception\MissingAttributeException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
use SimpleSAML\XML\Exception\SchemaViolationException;
16
17
use function boolval;
18
use function intval;
19
use function is_null;
20
use function is_numeric;
21
use function strval;
22
23
/**
24
 * Class representing the ECP SubjectConfirmation element.
25
 *
26
 * @package simplesamlphp/saml2
27
 */
28
final class SubjectConfirmation extends AbstractEcpElement
29
{
30
    /**
31
     * Create a ECP SubjectConfirmation element.
32
     *
33
     * @param bool $mustUnderstand
34
     * @param string $method
35
     * @param \SimpleSAML\SAML2\XML\saml\SubjectConfirmationData|null $subjectConfirmationData
36
     */
37
    public function __construct(
38
        protected bool $mustUnderstand,
39
        protected string $method,
40
        protected ?SubjectConfirmationData $subjectConfirmationData = null,
41
    ) {
42
        Assert::validURI($method, SchemaViolationException::class);
43
    }
44
45
46
    /**
47
     * Collect the value of the mustUnderstand-property
48
     *
49
     * @return bool
50
     */
51
    public function getMustUnderstand(): bool
52
    {
53
        return $this->mustUnderstand;
54
    }
55
56
57
    /**
58
     * Collect the value of the method-property
59
     *
60
     * @return string
61
     */
62
    public function getMethod(): string
63
    {
64
        return $this->method;
65
    }
66
67
68
    /**
69
     * Collect the value of the subjectConfirmationData-property
70
     *
71
     * @return \SimpleSAML\SAML2\XML\saml\SubjectConfirmationData|null
72
     */
73
    public function getSubjectConfirmationData(): ?SubjectConfirmationData
74
    {
75
        return $this->subjectConfirmationData;
76
    }
77
78
79
    /**
80
     * Convert XML into a SubjectConfirmation
81
     *
82
     * @param \DOMElement $xml The XML element we should load
83
     * @return static
84
     *
85
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
86
     *   if the qualified name of the supplied element is wrong
87
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
88
     *   if the supplied element is missing any of the mandatory attributes
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, 'SubjectConfirmation', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, SubjectConfirmation::NS, InvalidDOMElementException::class);
94
95
        // Assert required attributes
96
        Assert::true(
97
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
98
            'Missing env:actor attribute in <ecp:SubjectConfirmation>.',
99
            MissingAttributeException::class,
100
        );
101
        Assert::true(
102
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand'),
103
            'Missing env:mustUnderstand attribute in <ecp:SubjectConfirmation>.',
104
            MissingAttributeException::class,
105
        );
106
107
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
108
        $mustUnderstand = ($mustUnderstand === '') ? null : boolval(intval($mustUnderstand));
109
        Assert::nullOrBoolean(
110
            $mustUnderstand,
111
            'Invalid value of env:mustUnderstand attribute in <ecp:SubjectConfirmation>.',
112
            ProtocolViolationException::class,
113
        );
114
115
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
116
        Assert::same(
117
            $actor,
118
            C::SOAP_ACTOR_NEXT,
119
            'Invalid value of env:actor attribute in <ecp:SubjectConfirmation>.',
120
            ProtocolViolationException::class,
121
        );
122
123
        $subjectConfirmationData = SubjectConfirmationData::getChildrenOfClass($xml);
124
        Assert::maxCount(
125
            $subjectConfirmationData,
126
            1,
127
            'More than one <saml:SubjectConfirmationData> in <saml:SubjectConfirmation>.',
128
            TooManyElementsException::class,
129
        );
130
131
        return new static(
132
            $mustUnderstand,
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...irmation::__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

132
            /** @scrutinizer ignore-type */ $mustUnderstand,
Loading history...
133
            self::getAttribute($xml, 'Method'),
134
            array_pop($subjectConfirmationData),
135
        );
136
    }
137
138
139
    /**
140
     * Convert this ECP SubjectConfirmation to XML.
141
     *
142
     * @param \DOMElement|null $parent The element we should append this element to.
143
     * @return \DOMElement
144
     */
145
    public function toXML(DOMElement $parent = null): DOMElement
146
    {
147
        $e = $this->instantiateParentElement($parent);
148
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', strval(intval($this->getMustUnderstand())));
149
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
150
        $e->setAttribute('Method', $this->getMethod());
151
152
        $this->getSubjectConfirmationData()?->toXML($e);
153
154
        return $e;
155
    }
156
}
157