Passed
Pull Request — master (#323)
by Tim
12:30
created

AssertionIDRequest::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 20
rs 9.9666

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooHighException;
10
use SimpleSAML\SAML2\Exception\Protocol\RequestVersionTooLowException;
11
use SimpleSAML\SAML2\XML\saml\AssertionIDRef;
12
use SimpleSAML\SAML2\XML\saml\Issuer;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
use SimpleSAML\XML\Utils as XMLUtils;
16
use SimpleSAML\XMLSecurity\XML\ds\Signature;
17
18
use function array_pop;
19
use function preg_replace;
20
use function version_compare;
21
22
/**
23
 * @package simplesamlphp/saml2
24
 */
25
final class AssertionIDRequest extends AbstractRequest
26
{
27
    /**
28
     * Initialize an AssertionIDRequest.
29
     *
30
     * @param \SimpleSAML\SAML2\XML\saml\AssertionIDRef $assertionIDRef
31
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
32
     * @param string|null $id
33
     * @param string $version
34
     * @param int|null $issueInstant
35
     * @param string|null $destination
36
     * @param string|null $consent
37
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
38
     *
39
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
40
     */
41
    public function __construct(
42
        protected array $assertionIDRef,
43
        ?Issuer $issuer = null,
44
        ?string $id = null,
45
        string $version = '2.0',
46
        ?int $issueInstant = null,
47
        ?string $destination = null,
48
        ?string $consent = null,
49
        ?Extensions $extensions = null,
50
    ) {
51
        Assert::allIsInstanceOf($assertionIDRef, AssertionIDRef::class, InvalidDOMElementException::class);
52
53
        parent::__construct(
54
            $issuer,
55
            $id,
56
            $version,
57
            $issueInstant,
58
            $destination,
59
            $consent,
60
            $extensions,
61
        );
62
    }
63
64
65
66
    /**
67
     * @return \SimpleSAML\SAML2\XML\saml\AssertionIDRef[]
68
     */
69
    public function getAssertionIDRef(): array
70
    {
71
        return $this->assertionIDRef;
72
    }
73
74
75
    /**
76
     * Convert XML into a AssertionIDRequest element
77
     *
78
     * @param \DOMElement $xml The XML element we should load
79
     * @return static
80
     *
81
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
82
     *   If the qualified name of the supplied element is wrong
83
     */
84
    public static function fromXML(DOMElement $xml): static
85
    {
86
        Assert::same($xml->localName, 'AssertionIDRequest', InvalidDOMElementException::class);
87
        Assert::same($xml->namespaceURI, AssertionIDRequest::NS, InvalidDOMElementException::class);
88
89
        $assertionIDRef = AssertionIDRef::getChildrenOfClass($xml);
90
        Assert::minCount(
91
            $assertionIDRef,
92
            1,
93
            'At least one <samlp:AssertionIDRef> element is required.',
94
            TooManyElementsException::class,
95
        );
96
97
        $issuer = Issuer::getChildrenOfClass($xml);
98
        Assert::maxCount($issuer, 1, 'Only one <saml:Issuer> element is allowed.', TooManyElementsException::class);
99
100
        $version = self::getAttribute($xml, 'Version');
101
        Assert::true(version_compare('2.0', $version, '<='), RequestVersionTooLowException::class);
102
        Assert::true(version_compare('2.0', $version, '>='), RequestVersionTooHighException::class);
103
104
        $issueInstant = self::getAttribute($xml, 'IssueInstant');
105
        // Strip sub-seconds - See paragraph 1.3.3 of SAML core specifications
106
        $issueInstant = preg_replace('/([.][0-9]+Z)$/', 'Z', $issueInstant, 1);
107
108
        Assert::validDateTimeZulu($issueInstant, ProtocolViolationException::class);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\sam...tocolViolationException 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...
109
        $issueInstant = XMLUtils::xsDateTimeToTimestamp($issueInstant);
110
111
        $extensions = Extensions::getChildrenOfClass($xml);
112
        Assert::maxCount(
113
            $extensions,
114
            1,
115
            'Only one <samlp:Extensions> element is allowed.',
116
            TooManyElementsException::class,
117
        );
118
119
        $signature = Signature::getChildrenOfClass($xml);
120
        Assert::maxCount(
121
            $signature,
122
            1,
123
            'Only one <ds:Signature> element is allowed.',
124
            TooManyElementsException::class,
125
        );
126
127
        $request = new static(
128
            $assertionIDRef,
129
            array_pop($issuer),
130
            self::getAttribute($xml, 'ID'),
131
            $version,
132
            $issueInstant,
133
            self::getAttribute($xml, 'Destination', null),
134
            self::getAttribute($xml, 'Consent', null),
135
            array_pop($extensions),
136
        );
137
138
        if (!empty($signature)) {
139
            $request->setSignature($signature[0]);
140
            $request->messageContainedSignatureUponConstruction = true;
141
            $request->setXML($xml);
142
        }
143
144
        return $request;
145
    }
146
147
148
    /**
149
     * Convert this AssertionIDRequest element to XML.
150
     *
151
     * @param \DOMElement|null $parent The element we should append this AssertionIDRequest element to.
152
     * @return \DOMElement
153
     */
154
    public function toUnsignedXML(DOMElement $parent = null): DOMElement
155
    {
156
        $e = parent::toUnsignedXML($parent);
157
158
        foreach ($this->getAssertionIDRef() as $a) {
159
            $a->toXML($e);
160
        }
161
162
        return $e;
163
    }
164
}
165