Completed
Push — master ( da4b66...3ae217 )
by Tim
19s queued 15s
created

Response::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 31
rs 9.568
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML11\Assert\Assert as SAMLAssert;
11
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
12
use SimpleSAML\SAML11\Exception\VersionMismatchException;
13
use SimpleSAML\SAML11\XML\saml\Assertion;
14
use SimpleSAML\SAML11\XML\samlp\Status;
15
use SimpleSAML\XML\Exception\InvalidDOMElementException;
16
use SimpleSAML\XML\Exception\MissingElementException;
17
use SimpleSAML\XML\Exception\TooManyElementsException;
18
19
use function array_pop;
20
21
/**
22
 * Class representing a samlp:Response element.
23
 *
24
 * @package simplesaml/xml-saml11
25
 */
26
final class Response extends AbstractResponseType
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::getIntegerAttribute($xml, 'MajorVersion');
47
        Assert::same($majorVersion, 1, VersionMismatchException::class);
48
49
        $minorVersion = self::getIntegerAttribute($xml, 'MinorVersion');
50
        Assert::same($minorVersion, 1, VersionMismatchException::class);
51
52
        $issueInstant = self::getAttribute($xml, 'IssueInstant');
53
        // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
54
        $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1);
55
56
        SAMLAssert::validDateTime($issueInstant, ProtocolViolationException::class);
57
        $issueInstant = new DateTimeImmutable($issueInstant);
58
59
        $status = Status::getChildrenOfClass($xml);
60
        Assert::minCount($status, 1, MissingElementException::class);
61
        Assert::maxCount($status, 1, TooManyElementsException::class);
62
63
        return new static(
64
            self::getAttribute($xml, 'ResponseID'),
65
            array_pop($status),
66
            Assertion::getChildrenOfClass($xml),
67
            $majorVersion,
68
            $minorVersion,
69
            $issueInstant,
70
            $inResponseTo = self::getOptionalAttribute($xml, 'InResponseTo', null),
71
            $recipient = self::getOptionalAttribute($xml, 'Recipient', null),
72
        );
73
    }
74
}
75