Response::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 24
rs 9.6666
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\SAML11\Assert\Assert;
9
use SimpleSAML\SAML11\Exception\{ProtocolViolationException, VersionMismatchException};
10
use SimpleSAML\SAML11\Type\{SAMLAnyURIValue, SAMLDateTimeValue};
11
use SimpleSAML\SAML11\XML\saml\Assertion;
12
use SimpleSAML\SAML11\XML\samlp\Status;
13
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
14
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
15
use SimpleSAML\XMLSchema\Type\{IDValue, NCNameValue, NonNegativeIntegerValue};
16
17
use function array_pop;
18
19
/**
20
 * Class representing a samlp:Response element.
21
 *
22
 * @package simplesaml/saml11
23
 */
24
final class Response extends AbstractResponseType implements SchemaValidatableElementInterface
25
{
26
    use SchemaValidatableElementTrait;
27
28
    /**
29
     * Convert XML into Response
30
     *
31
     * @param \DOMElement $xml The XML element we should load
32
     * @return static
33
     *
34
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
35
     *   if the qualified name of the supplied element is wrong
36
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
37
     *   if too many child-elements of a type are specified
38
     * @throws \SimpleSAML\XML\Exception\MissingElementException
39
     *   if one of the mandatory child-elements is missing
40
     */
41
    public static function fromXML(DOMElement $xml): static
42
    {
43
        Assert::same($xml->localName, 'Response', InvalidDOMElementException::class);
44
        Assert::same($xml->namespaceURI, Response::NS, InvalidDOMElementException::class);
45
46
        $majorVersion = self::getAttribute($xml, 'MajorVersion', NonNegativeIntegerValue::class);
47
        Assert::same($majorVersion->getValue(), '1', VersionMismatchException::class);
48
49
        $minorVersion = self::getAttribute($xml, 'MinorVersion', NonNegativeIntegerValue::class);
50
        Assert::same($minorVersion->getValue(), '1', VersionMismatchException::class);
51
52
        $status = Status::getChildrenOfClass($xml);
53
        Assert::minCount($status, 1, MissingElementException::class);
54
        Assert::maxCount($status, 1, TooManyElementsException::class);
55
56
        return new static(
57
            $majorVersion,
58
            $minorVersion,
59
            self::getAttribute($xml, 'ResponseID', IDValue::class),
60
            array_pop($status),
61
            self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class),
62
            Assertion::getChildrenOfClass($xml),
63
            self::getOptionalAttribute($xml, 'InResponseTo', NCNameValue::class, null),
64
            self::getOptionalAttribute($xml, 'Recipient', SAMLAnyURIValue::class, null),
65
        );
66
    }
67
}
68