AssertionIDRequest::fromXML()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 53
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 35
nc 2
nop 1
dl 0
loc 53
rs 9.36
c 0
b 0
f 0

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\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooHighException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...VersionTooHighException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooLowException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...tVersionTooLowException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
12
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Type\SAMLDateTimeValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use SimpleSAML\SAML2\Type\SAMLStringValue;
14
use SimpleSAML\SAML2\XML\saml\AssertionIDRef;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\AssertionIDRef was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use SimpleSAML\SAML2\XML\saml\Issuer;
16
use SimpleSAML\XML\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XML\Constants was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use SimpleSAML\XML\SchemaValidatableElementInterface;
18
use SimpleSAML\XML\SchemaValidatableElementTrait;
19
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
20
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
21
use SimpleSAML\XMLSchema\Type\IDValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\IDValue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use SimpleSAML\XMLSecurity\XML\ds\Signature;
23
24
use function array_pop;
25
use function version_compare;
26
27
/**
28
 * @package simplesamlphp/saml2
29
 */
30
final class AssertionIDRequest extends AbstractRequest implements SchemaValidatableElementInterface
31
{
32
    use SchemaValidatableElementTrait;
33
34
35
    /**
36
     * Initialize an AssertionIDRequest.
37
     *
38
     * @param \SimpleSAML\XMLSchema\Type\IDValue $id
39
     * @param \SimpleSAML\SAML2\XML\saml\AssertionIDRef[] $assertionIDRef
40
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
41
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $issueInstant
42
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $destination
43
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $consent
44
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
45
     *
46
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
47
     */
48
    public function __construct(
49
        IDValue $id,
50
        protected array $assertionIDRef,
51
        ?Issuer $issuer = null,
52
        ?SAMLDateTimeValue $issueInstant = null,
53
        ?SAMLAnyURIValue $destination = null,
54
        ?SAMLAnyURIValue $consent = null,
55
        ?Extensions $extensions = null,
56
    ) {
57
        Assert::maxCount($assertionIDRef, C::UNBOUNDED_LIMIT);
58
        Assert::allIsInstanceOf($assertionIDRef, AssertionIDRef::class, InvalidDOMElementException::class);
59
60
        parent::__construct(
61
            $id,
62
            $issuer,
63
            $issueInstant,
64
            $destination,
65
            $consent,
66
            $extensions,
67
        );
68
    }
69
70
71
    /**
72
     * @return \SimpleSAML\SAML2\XML\saml\AssertionIDRef[]
73
     */
74
    public function getAssertionIDRef(): array
75
    {
76
        return $this->assertionIDRef;
77
    }
78
79
80
    /**
81
     * Convert XML into a AssertionIDRequest element
82
     *
83
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
84
     *   If the qualified name of the supplied element is wrong
85
     */
86
    public static function fromXML(DOMElement $xml): static
87
    {
88
        Assert::same($xml->localName, 'AssertionIDRequest', InvalidDOMElementException::class);
89
        Assert::same($xml->namespaceURI, AssertionIDRequest::NS, InvalidDOMElementException::class);
90
91
        $assertionIDRef = AssertionIDRef::getChildrenOfClass($xml);
92
        Assert::minCount(
93
            $assertionIDRef,
94
            1,
95
            'At least one <samlp:AssertionIDRef> element is required.',
96
            TooManyElementsException::class,
97
        );
98
99
        $issuer = Issuer::getChildrenOfClass($xml);
100
        Assert::maxCount($issuer, 1, 'Only one <saml:Issuer> element is allowed.', TooManyElementsException::class);
101
102
        $version = self::getAttribute($xml, 'Version', SAMLStringValue::class);
103
        Assert::true(version_compare('2.0', strval($version), '<='), RequestVersionTooLowException::class);
104
        Assert::true(version_compare('2.0', strval($version), '>='), RequestVersionTooHighException::class);
105
106
        $extensions = Extensions::getChildrenOfClass($xml);
107
        Assert::maxCount(
108
            $extensions,
109
            1,
110
            'Only one <samlp:Extensions> element is allowed.',
111
            TooManyElementsException::class,
112
        );
113
114
        $signature = Signature::getChildrenOfClass($xml);
115
        Assert::maxCount(
116
            $signature,
117
            1,
118
            'Only one <ds:Signature> element is allowed.',
119
            TooManyElementsException::class,
120
        );
121
122
        $request = new static(
123
            self::getAttribute($xml, 'ID', IDValue::class),
124
            $assertionIDRef,
125
            array_pop($issuer),
126
            self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class),
127
            self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null),
128
            self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null),
129
            array_pop($extensions),
130
        );
131
132
        if (!empty($signature)) {
133
            $request->setSignature($signature[0]);
134
            $request->messageContainedSignatureUponConstruction = true;
135
            $request->setXML($xml);
136
        }
137
138
        return $request;
139
    }
140
141
142
    /**
143
     * Convert this AssertionIDRequest element to XML.
144
     */
145
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
146
    {
147
        $e = parent::toUnsignedXML($parent);
148
149
        foreach ($this->getAssertionIDRef() as $a) {
150
            $a->toXML($e);
151
        }
152
153
        return $e;
154
    }
155
}
156