Passed
Pull Request — master (#226)
by Jaime Pérez
02:38
created

ArtifactResponse::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 10
dl 0
loc 25
rs 9.9
c 0
b 0
f 0

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 SAML2\XML\samlp;
6
7
use DOMElement;
8
use DOMNode;
9
use SAML2\Utils;
10
use SAML2\XML\ExtendableElementTrait;
11
use SAML2\XML\ds\Signature;
12
use SAML2\XML\saml\Issuer;
13
use Webmozart\Assert\Assert;
14
15
/**
16
 * The \SAML2\ArtifactResponse, is the response to the \SAML2\ArtifactResolve.
17
 *
18
 * @author Danny Bollaert, UGent AS. <[email protected]>
19
 * @package SimpleSAMLphp
20
 */
21
class ArtifactResponse extends AbstractStatusResponse
22
{
23
    /** @var \DOMElement */
24
    protected $any;
25
26
27
    /**
28
     * Constructor for SAML 2 ArtifactResponse.
29
     *
30
     * @param \SAML2\XML\samlp\Status $status
31
     * @param \SAML2\XML\saml\Issuer|null $issuer
32
     * @param string|null $id
33
     * @param string $version
34
     * @param int|null $issueInstant
35
     * @param string|null $inResponseTo
36
     * @param string|null $destination
37
     * @param string|null $consent
38
     * @param \SAML2\XML\samlp\Extensions|null $extensions
39
     * @param \DOMElement $any
40
     */
41
    public function __construct(
42
        Status $status,
43
        ?Issuer $issuer = null,
44
        ?string $id = null,
45
        ?string $version = '2.0',
46
        ?int $issueInstant = null,
47
        ?string $inResponseTo = null,
48
        ?string $destination = null,
49
        ?string $consent = null,
50
        ?Extensions $extensions = null,
51
        ?DOMElement $any = null
52
    ) {
53
        parent::__construct(
54
            $status,
55
            $issuer,
56
            $id,
57
            $version,
58
            $issueInstant,
59
            $inResponseTo,
60
            $destination,
61
            $consent,
62
            $extensions
63
        );
64
65
        $this->setAny($any);
66
    }
67
68
69
    /**
70
     * Collect the value of the any-property
71
     *
72
     * @return \DOMElement|null
73
     */
74
    public function getAny(): ?DOMElement
75
    {
76
        return $this->any;
77
    }
78
79
80
    /**
81
     * Set the value of the any-property
82
     *
83
     * @param \DOMElement|null $any
84
     * @return void
85
     */
86
    private function setAny(?DOMElement $any): void
87
    {
88
        $this->any = $any;
89
    }
90
91
92
    /**
93
     * Convert XML into an ArtifactResponse
94
     *
95
     * @param \DOMElement $xml
96
     * @return self
97
     * @throws \Exception
98
     */
99
    public static function fromXML(DOMElement $xml): object
100
    {
101
        Assert::same($xml->localName, 'ArtifactResponse');
102
        Assert::same($xml->namespaceURI, ArtifactResponse::NS);
103
104
        $id = self::getAttribute($xml, 'ID');
105
        $version = self::getAttribute($xml, 'Version');
106
        $issueInstant = Utils::xsDateTimeToTimestamp(self::getAttribute($xml, 'IssueInstant'));
107
        $inResponseTo = self::getAttribute($xml, 'InResponseTo', null);
108
        $destination = self::getAttribute($xml, 'Destination', null);
109
        $consent = self::getAttribute($xml, 'Consent', null);
110
111
        $issuer = Issuer::getChildrenOfClass($xml);
112
        Assert::countBetween($issuer, 0, 1);
113
114
        // Find children; they should come last, after the Status-element
115
        $status = Utils::xpQuery($xml, './saml_protocol:Status');
116
        $status = $status[0];
117
118
        /** @psalm-suppress RedundantCondition */
119
        for ($any = $status->nextSibling; $any instanceof DOMNode; $any = $any->nextSibling) {
120
            if ($any instanceof DOMElement) {
121
                break;
122
            }
123
            /* Ignore comments and text nodes. */
124
        }
125
126
        $status = Status::getChildrenOfClass($xml);
127
        Assert::count($status, 1);
128
129
        $extensions = Extensions::getChildrenOfClass($xml);
130
        Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.');
131
132
        $signature = Signature::getChildrenOfClass($xml);
133
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.');
134
135
        $response = new self(
136
            array_pop($status),
137
            empty($issuer) ? null : array_pop($issuer),
138
            $id,
139
            $version,
140
            $issueInstant,
141
            $inResponseTo,
142
            $destination,
143
            $consent,
144
            empty($extensions) ? null : array_pop($extensions),
145
            $any
146
        );
147
148
        if (!empty($signature)) {
149
            $response->setSignature($signature[0]);
150
        }
151
152
        return $response;
153
    }
154
155
156
    /**
157
     * Convert the ArtifactResponse message to an XML element.
158
     *
159
     * @return \DOMElement This response.
160
     */
161
    public function toXML(?DOMElement $parent = null): DOMElement
162
    {
163
        $e = parent::toXML($parent);
164
165
        if ($this->any !== null) {
166
            $node = $e->ownerDocument->importNode($this->any, true);
167
            $e->appendChild($node);
168
        }
169
170
        return $this->signElement($e);
171
    }
172
}
173