Request::fromXML()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 56
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 56
rs 8.3626
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 DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Exception\VersionMismatchException;
10
use SimpleSAML\SAML11\Type\SAMLDateTimeValue;
11
use SimpleSAML\SAML11\XML\saml\AssertionIDReference;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\MissingElementException;
14
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
15
use SimpleSAML\XMLSchema\Type\IDValue;
16
use SimpleSAML\XMLSchema\Type\NonNegativeIntegerValue;
17
18
use function array_merge;
19
use function array_pop;
20
21
/**
22
 * Class representing a samlp:Request element.
23
 *
24
 * @package simplesaml/saml11
25
 */
26
final class Request extends AbstractRequestType
27
{
28
    /**
29
     * Convert XML into Request
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, 'Request', InvalidDOMElementException::class);
44
        Assert::same($xml->namespaceURI, Request::NS, InvalidDOMElementException::class);
45
46
        $query = AbstractQuery::getChildrenOfClass($xml);
47
        Assert::maxCount($query, 1, TooManyElementsException::class);
48
49
        $subjectQuery = AbstractSubjectQuery::getChildrenOfClass($xml);
50
        Assert::maxCount($subjectQuery, 1, TooManyElementsException::class);
51
52
        $authenticationQuery = AuthenticationQuery::getChildrenOfClass($xml);
53
        Assert::maxCount($authenticationQuery, 1, TooManyElementsException::class);
54
55
        $attributeQuery = AttributeQuery::getChildrenOfClass($xml);
56
        Assert::maxCount($attributeQuery, 1, TooManyElementsException::class);
57
58
        $authorizationDecisionQuery = AuthorizationDecisionQuery::getChildrenOfClass($xml);
59
        Assert::maxCount($authorizationDecisionQuery, 1, TooManyElementsException::class);
60
61
        $query = array_merge(
62
            $query,
63
            $subjectQuery,
64
            $authenticationQuery,
65
            $attributeQuery,
66
            $authorizationDecisionQuery,
67
        );
68
        Assert::maxCount($query, 1, TooManyElementsException::class);
69
70
        $assertionIdReference = AssertionIDReference::getChildrenOfClass($xml);
71
        $assertionArtifact = AssertionArtifact::getChildrenOfClass($xml);
72
73
        Assert::true(
74
            !empty($query) || !empty($assertionIdReference) || !empty($assertionArtifact),
75
            TooManyElementsException::class,
76
        );
77
        Assert::false(
78
            empty($query) && empty($assertionIdReference) && empty($assertionArtifact),
79
            MissingElementException::class,
80
        );
81
82
        $majorVersion = self::getAttribute($xml, 'MajorVersion', NonNegativeIntegerValue::class);
83
        Assert::same($majorVersion->getValue(), '1', VersionMismatchException::class);
84
85
        $minorVersion = self::getAttribute($xml, 'MinorVersion', NonNegativeIntegerValue::class);
86
        Assert::same($minorVersion->getValue(), '1', VersionMismatchException::class);
87
88
        $issueInstant = self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class);
89
90
        return new static(
91
            $assertionIdReference ?: $assertionArtifact ?: array_pop($query),
92
            self::getAttribute($xml, 'RequestID', IDValue::class),
93
            $majorVersion,
94
            $minorVersion,
95
            $issueInstant,
96
            RespondWith::getChildrenOfClass($xml),
97
        );
98
    }
99
}
100