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