SubjectConfirmation::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 1
dl 0
loc 42
rs 9.472
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\SAMLAnyURIValue;
11
use SimpleSAML\SAML2\XML\saml\SubjectConfirmationData;
12
use SimpleSAML\SOAP11\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP11\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SimpleSAML\SOAP11\Type\MustUnderstandValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP11\Type\MustUnderstandValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use SimpleSAML\XML\SchemaValidatableElementInterface;
15
use SimpleSAML\XML\SchemaValidatableElementTrait;
16
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
17
use SimpleSAML\XMLSchema\Exception\MissingAttributeException;
18
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
19
20
/**
21
 * Class representing the ECP SubjectConfirmation element.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
final class SubjectConfirmation extends AbstractEcpElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\ecp\AbstractEcpElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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