Request   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 39
c 1
b 0
f 0
dl 0
loc 71
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B fromXML() 0 56 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Exception\{ProtocolViolationException, VersionMismatchException};
10
use SimpleSAML\SAML11\Type\SAMLDateTimeValue;
11
use SimpleSAML\SAML11\XML\saml\AssertionIDReference;
12
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
13
use SimpleSAML\XMLSchema\Type\{IDValue, NonNegativeIntegerValue};
14
15
use function array_merge;
16
use function array_pop;
17
18
/**
19
 * Class representing a samlp:Request element.
20
 *
21
 * @package simplesaml/saml11
22
 */
23
final class Request extends AbstractRequestType
24
{
25
    /**
26
     * Convert XML into Request
27
     *
28
     * @param \DOMElement $xml The XML element we should load
29
     * @return static
30
     *
31
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
32
     *   if the qualified name of the supplied element is wrong
33
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
34
     *   if too many child-elements of a type are specified
35
     * @throws \SimpleSAML\XML\Exception\MissingElementException
36
     *   if one of the mandatory child-elements is missing
37
     */
38
    public static function fromXML(DOMElement $xml): static
39
    {
40
        Assert::same($xml->localName, 'Request', InvalidDOMElementException::class);
41
        Assert::same($xml->namespaceURI, Request::NS, InvalidDOMElementException::class);
42
43
        $query = AbstractQuery::getChildrenOfClass($xml);
44
        Assert::maxCount($query, 1, TooManyElementsException::class);
45
46
        $subjectQuery = AbstractSubjectQuery::getChildrenOfClass($xml);
47
        Assert::maxCount($subjectQuery, 1, TooManyElementsException::class);
48
49
        $authenticationQuery = AuthenticationQuery::getChildrenOfClass($xml);
50
        Assert::maxCount($authenticationQuery, 1, TooManyElementsException::class);
51
52
        $attributeQuery = AttributeQuery::getChildrenOfClass($xml);
53
        Assert::maxCount($attributeQuery, 1, TooManyElementsException::class);
54
55
        $authorizationDecisionQuery = AuthorizationDecisionQuery::getChildrenOfClass($xml);
56
        Assert::maxCount($authorizationDecisionQuery, 1, TooManyElementsException::class);
57
58
        $query = array_merge(
59
            $query,
60
            $subjectQuery,
61
            $authenticationQuery,
62
            $attributeQuery,
63
            $authorizationDecisionQuery,
64
        );
65
        Assert::maxCount($query, 1, TooManyElementsException::class);
66
67
        $assertionIdReference = AssertionIDReference::getChildrenOfClass($xml);
68
        $assertionArtifact = AssertionArtifact::getChildrenOfClass($xml);
69
70
        Assert::true(
71
            !empty($query) || !empty($assertionIdReference) || !empty($assertionArtifact),
72
            TooManyElementsException::class,
73
        );
74
        Assert::false(
75
            empty($query) && empty($assertionIdReference) && empty($assertionArtifact),
76
            MissingElementException::class,
77
        );
78
79
        $majorVersion = self::getAttribute($xml, 'MajorVersion', NonNegativeIntegerValue::class);
80
        Assert::same($majorVersion->getValue(), '1', VersionMismatchException::class);
81
82
        $minorVersion = self::getAttribute($xml, 'MinorVersion', NonNegativeIntegerValue::class);
83
        Assert::same($minorVersion->getValue(), '1', VersionMismatchException::class);
84
85
        $issueInstant = self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class);
86
87
        return new static(
88
            $assertionIdReference ?: $assertionArtifact ?: array_pop($query),
89
            self::getAttribute($xml, 'RequestID', IDValue::class),
90
            $majorVersion,
91
            $minorVersion,
92
            $issueInstant,
93
            RespondWith::getChildrenOfClass($xml),
94
        );
95
    }
96
}
97