Passed
Push — master ( 8cd786...a11f07 )
by Tim
04:16 queued 02:07
created

SubjectConfirmation::getMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
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 0
dl 0
loc 3
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 string $method
34
     * @param \SimpleSAML\SAML2\XML\saml\SubjectConfirmationData|null $subjectConfirmationData
35
     */
36
    public function __construct(
37
        protected string $method,
38
        protected ?SubjectConfirmationData $subjectConfirmationData = null,
39
    ) {
40
        Assert::validURI($method, SchemaViolationException::class);
41
    }
42
43
44
    /**
45
     * Collect the value of the method-property
46
     *
47
     * @return string
48
     */
49
    public function getMethod(): string
50
    {
51
        return $this->method;
52
    }
53
54
55
    /**
56
     * Collect the value of the subjectConfirmationData-property
57
     *
58
     * @return \SimpleSAML\SAML2\XML\saml\SubjectConfirmationData|null
59
     */
60
    public function getSubjectConfirmationData(): ?SubjectConfirmationData
61
    {
62
        return $this->subjectConfirmationData;
63
    }
64
65
66
    /**
67
     * Convert XML into a SubjectConfirmation
68
     *
69
     * @param \DOMElement $xml The XML element we should load
70
     * @return static
71
     *
72
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
73
     *   if the qualified name of the supplied element is wrong
74
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
75
     *   if the supplied element is missing any of the mandatory attributes
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'SubjectConfirmation', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, SubjectConfirmation::NS, InvalidDOMElementException::class);
81
82
        // Assert required attributes
83
        Assert::true(
84
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'actor'),
85
            'Missing env:actor attribute in <ecp:SubjectConfirmation>.',
86
            MissingAttributeException::class,
87
        );
88
        Assert::true(
89
            $xml->hasAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand'),
90
            'Missing env:mustUnderstand attribute in <ecp:SubjectConfirmation>.',
91
            MissingAttributeException::class,
92
        );
93
94
        $mustUnderstand = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'mustUnderstand');
95
        Assert::same(
96
            $mustUnderstand,
97
            '1',
98
            'Invalid value of env:mustUnderstand attribute in <ecp:SubjectConfirmation>.',
99
            ProtocolViolationException::class,
100
        );
101
102
        $actor = $xml->getAttributeNS(C::NS_SOAP_ENV_11, 'actor');
103
        Assert::same(
104
            $actor,
105
            C::SOAP_ACTOR_NEXT,
106
            'Invalid value of env:actor attribute in <ecp:SubjectConfirmation>.',
107
            ProtocolViolationException::class,
108
        );
109
110
        $subjectConfirmationData = SubjectConfirmationData::getChildrenOfClass($xml);
111
        Assert::maxCount(
112
            $subjectConfirmationData,
113
            1,
114
            'More than one <saml:SubjectConfirmationData> in <saml:SubjectConfirmation>.',
115
            TooManyElementsException::class,
116
        );
117
118
        return new static(
119
            self::getAttribute($xml, 'Method'),
120
            array_pop($subjectConfirmationData),
121
        );
122
    }
123
124
125
    /**
126
     * Convert this ECP SubjectConfirmation to XML.
127
     *
128
     * @param \DOMElement|null $parent The element we should append this element to.
129
     * @return \DOMElement
130
     */
131
    public function toXML(DOMElement $parent = null): DOMElement
132
    {
133
        $e = $this->instantiateParentElement($parent);
134
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:mustUnderstand', '1');
135
        $e->setAttributeNS(C::NS_SOAP_ENV_11, 'env:actor', C::SOAP_ACTOR_NEXT);
136
        $e->setAttribute('Method', $this->getMethod());
137
138
        $this->getSubjectConfirmationData()?->toXML($e);
139
140
        return $e;
141
    }
142
}
143