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

Request::fromXML()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 61
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 61
rs 8.3146
cc 7
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\AssertionIDReference;
14
use SimpleSAML\XML\Exception\InvalidDOMElementException;
15
use SimpleSAML\XML\Exception\MissingElementException;
16
use SimpleSAML\XML\Exception\TooManyElementsException;
17
18
use function array_merge;
19
use function array_pop;
20
use function preg_replace;
21
22
/**
23
 * Class representing a samlp:Request element.
24
 *
25
 * @package simplesaml/xml-saml11
26
 */
27
final class Request extends AbstractRequestType
28
{
29
    /**
30
     * Convert XML into Request
31
     *
32
     * @param \DOMElement $xml The XML element we should load
33
     * @return static
34
     *
35
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
36
     *   if the qualified name of the supplied element is wrong
37
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
38
     *   if too many child-elements of a type are specified
39
     * @throws \SimpleSAML\XML\Exception\MissingElementException
40
     *   if one of the mandatory child-elements is missing
41
     */
42
    public static function fromXML(DOMElement $xml): static
43
    {
44
        Assert::same($xml->localName, 'Request', InvalidDOMElementException::class);
45
        Assert::same($xml->namespaceURI, Request::NS, InvalidDOMElementException::class);
46
47
        $query = AbstractQuery::getChildrenOfClass($xml);
48
        Assert::maxCount($query, 1, TooManyElementsException::class);
49
50
        $subjectQuery = AbstractSubjectQuery::getChildrenOfClass($xml);
51
        Assert::maxCount($subjectQuery, 1, TooManyElementsException::class);
52
53
        $authenticationQuery = AuthenticationQuery::getChildrenOfClass($xml);
54
        Assert::maxCount($authenticationQuery, 1, TooManyElementsException::class);
55
56
        $attributeQuery = AttributeQuery::getChildrenOfClass($xml);
57
        Assert::maxCount($attributeQuery, 1, TooManyElementsException::class);
58
59
        $authorizationDecisionQuery = AuthorizationDecisionQuery::getChildrenOfClass($xml);
60
        Assert::maxCount($authorizationDecisionQuery, 1, TooManyElementsException::class);
61
62
        $query = array_merge(
63
            $query,
64
            $subjectQuery,
65
            $authenticationQuery,
66
            $attributeQuery,
67
            $authorizationDecisionQuery,
68
        );
69
        Assert::maxCount($query, 1, TooManyElementsException::class);
70
71
        $assertionIdReference = AssertionIDReference::getChildrenOfClass($xml);
72
        $assertionArtifact = AssertionArtifact::getChildrenOfClass($xml);
73
74
        Assert::true(
75
            !empty($query) || !empty($assertionIdReference) || !empty($assertionArtifact),
76
            TooManyElementsException::class,
77
        );
78
        Assert::false(
79
            empty($query) && empty($assertionIdReference) && empty($assertionArtifact),
80
            MissingElementException::class,
81
        );
82
83
        $majorVersion = self::getIntegerAttribute($xml, 'MajorVersion');
84
        Assert::same($majorVersion, 1, VersionMismatchException::class);
85
86
        $minorVersion = self::getIntegerAttribute($xml, 'MinorVersion');
87
        Assert::same($minorVersion, 1, VersionMismatchException::class);
88
89
        $issueInstant = self::getAttribute($xml, 'IssueInstant');
90
        // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
91
        $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1);
92
93
        SAMLAssert::validDateTime($issueInstant, ProtocolViolationException::class);
94
        $issueInstant = new DateTimeImmutable($issueInstant);
95
96
        return new static(
97
            $assertionIdReference ?: $assertionArtifact ?: array_pop($query),
98
            self::getAttribute($xml, 'RequestID'),
99
            $majorVersion,
100
            $minorVersion,
101
            $issueInstant,
102
            RespondWith::getChildrenOfClass($xml),
103
        );
104
    }
105
}
106