Response::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 1
dl 0
loc 32
rs 9.584
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\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...
12
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...
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
16
use SimpleSAML\XMLSchema\Exception\MissingAttributeException;
17
18
/**
19
 * Class representing the ECP Response element.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class Response 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...
24
{
25
    use SchemaValidatableElementTrait;
26
27
28
    /**
29
     * Create a ECP Response element.
30
     *
31
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue $assertionConsumerServiceURL
32
     */
33
    public function __construct(
34
        protected SAMLAnyURIValue $assertionConsumerServiceURL,
35
    ) {
36
    }
37
38
39
    /**
40
     * Collect the value of the AssertionConsumerServiceURL-property
41
     *
42
     * @return \SimpleSAML\SAML2\Type\SAMLAnyURIValue
43
     */
44
    public function getAssertionConsumerServiceURL(): SAMLAnyURIValue
45
    {
46
        return $this->assertionConsumerServiceURL;
47
    }
48
49
50
    /**
51
     * Convert XML into a Response
52
     *
53
     * @param \DOMElement $xml The XML element we should load
54
     *
55
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
56
     *   if the qualified name of the supplied element is wrong
57
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
58
     *   if the supplied element is missing any of the mandatory attributes
59
     */
60
    public static function fromXML(DOMElement $xml): static
61
    {
62
        Assert::same($xml->localName, 'Response', InvalidDOMElementException::class);
63
        Assert::same($xml->namespaceURI, Response::NS, InvalidDOMElementException::class);
64
65
        // Assert required attributes
66
        Assert::true(
67
            $xml->hasAttributeNS(C::NS_SOAP_ENV, 'mustUnderstand'),
68
            'Missing env:mustUnderstand attribute in <ecp:Response>.',
69
            MissingAttributeException::class,
70
        );
71
        Assert::true(
72
            $xml->hasAttributeNS(C::NS_SOAP_ENV, 'actor'),
73
            'Missing env:actor attribute in <ecp:Response>.',
74
            MissingAttributeException::class,
75
        );
76
        Assert::true(
77
            $xml->hasAttribute('AssertionConsumerServiceURL'),
78
            'Missing AssertionConsumerServiceURL attribute in <ecp:Response>.',
79
            MissingAttributeException::class,
80
        );
81
82
        MustUnderstandValue::fromString($xml->getAttributeNS(C::NS_SOAP_ENV, 'mustUnderstand'));
83
84
        Assert::same(
85
            $xml->getAttributeNS(C::NS_SOAP_ENV, 'actor'),
86
            C::SOAP_ACTOR_NEXT,
87
            'Invalid value of env:actor attribute in <ecp:Response>.',
88
            ProtocolViolationException::class,
89
        );
90
91
        return new static(self::getAttribute($xml, 'AssertionConsumerServiceURL', SAMLAnyURIValue::class));
92
    }
93
94
95
    /**
96
     * Convert this ECP Response to XML.
97
     *
98
     * @param \DOMElement|null $parent The element we should append this element to.
99
     */
100
    public function toXML(?DOMElement $parent = null): DOMElement
101
    {
102
        $response = $this->instantiateParentElement($parent);
103
104
        $response->setAttributeNS(C::NS_SOAP_ENV, 'SOAP-ENV:mustUnderstand', '1');
105
        $response->setAttributeNS(C::NS_SOAP_ENV, 'SOAP-ENV:actor', C::SOAP_ACTOR_NEXT);
106
        $response->setAttribute('AssertionConsumerServiceURL', $this->getAssertionConsumerServiceURL()->getValue());
107
108
        return $response;
109
    }
110
}
111