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

ArtifactResolve::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 8
dl 0
loc 13
rs 10
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 SAML2\Constants;
9
use SAML2\Utils;
10
use SAML2\XML\ds\Signature;
11
use SAML2\XML\saml\Issuer;
12
use Webmozart\Assert\Assert;
13
14
/**
15
 * The Artifact is part of the SAML 2.0 IdP code, and it builds an artifact object.
16
 * I am using strings, because I find them easier to work with.
17
 * I want to use this, to be consistent with the other saml2_requests
18
 *
19
 * @author Danny Bollaert, UGent AS. <[email protected]>
20
 * @package SimpleSAMLphp
21
 */
22
class ArtifactResolve extends AbstractRequest
23
{
24
    /** @var string */
25
    protected $artifact;
26
27
28
    /**
29
     * Initialize an ArtifactResolve.
30
     *
31
     * @param string $artifact
32
     * @param \SAML2\XML\saml\Issuer|null $issuer
33
     * @param string|null $id
34
     * @param string|null $version
35
     * @param int|null $issueInstant
36
     * @param string|null $destination
37
     * @param string|null $consent
38
     * @param \SAML2\XML\samlp\Extensions $extensions
39
     *
40
     * @throws \Exception
41
     */
42
    public function __construct(
43
        string $artifact,
44
        ?Issuer $issuer = null,
45
        ?string $id = null,
46
        ?string $version = null,
47
        ?int $issueInstant = null,
48
        ?string $destination = null,
49
        ?string $consent = null,
50
        ?Extensions $extensions = null
51
    ) {
52
        parent::__construct($issuer, $id, $version, $issueInstant, $destination, $consent, $extensions);
53
54
        $this->setArtifact($artifact);
55
    }
56
57
58
    /**
59
     * Retrieve the Artifact in this response.
60
     *
61
     * @return string artifact.
62
     *
63
     * @throws \InvalidArgumentException if assertions are false
64
     */
65
    public function getArtifact(): string
66
    {
67
        Assert::notEmpty($this->artifact, 'Artifact not set.');
68
69
        return $this->artifact;
70
    }
71
72
73
    /**
74
     * Set the artifact that should be included in this response.
75
     *
76
     * @param string $artifact
77
     * @return void
78
     */
79
    public function setArtifact(string $artifact): void
80
    {
81
        $this->artifact = $artifact;
82
    }
83
84
85
    /**
86
     * Create a class from XML
87
     *
88
     * @param \DOMElement $xml
89
     * @return self
90
     */
91
    public static function fromXML(DOMElement $xml): object
92
    {
93
        Assert::same($xml->localName, 'ArtifactResolve');
94
        Assert::same($xml->namespaceURI, ArtifactResolve::NS);
95
96
        $id = self::getAttribute($xml, 'ID');
97
        $version = self::getAttribute($xml, 'Version');
98
        $issueInstant = Utils::xsDateTimeToTimestamp(self::getAttribute($xml, 'IssueInstant'));
99
        $destination = self::getAttribute($xml, 'Destination', null);
100
        $consent = self::getAttribute($xml, 'Consent', null);
101
102
        $issuer = Issuer::getChildrenOfClass($xml);
103
        Assert::maxCount($issuer, 1);
104
105
        $extensions = Extensions::getChildrenOfClass($xml);
106
        Assert::maxCount($extensions, 1, 'Only one saml:Extensions element is allowed.');
107
108
        $signature = Signature::getChildrenOfClass($xml);
109
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.');
110
111
        $results = Utils::xpQuery($xml, './saml_protocol:Artifact');
112
        $artifact = $results[0]->textContent;
113
114
        $resolve = new self(
115
            $artifact,
116
            empty($issuer) ? null : array_pop($issuer),
117
            $id,
118
            $version,
119
            $issueInstant,
120
            $destination,
121
            $consent,
122
            empty($extensions) ? null : array_pop($extensions)
123
        );
124
125
        if (!empty($signature)) {
126
            $resolve->setSignature($signature[0]);
127
        }
128
129
        return $resolve;
130
    }
131
132
133
    /**
134
     * Convert the response message to an XML element.
135
     *
136
     * @return \DOMElement This response.
137
     *
138
     * @throws \InvalidArgumentException if assertions are false
139
     */
140
    public function toXML(?DOMElement $parent = null): DOMElement
141
    {
142
        Assert::notEmpty($this->artifact, 'Cannot convert ArtifactResolve to XML without an Artifact set.');
143
144
        $e = parent::toXML($parent);
145
        $artifactelement = $e->ownerDocument->createElementNS(Constants::NS_SAMLP, 'Artifact', $this->artifact);
146
        $e->appendChild($artifactelement);
147
148
        return $this->signElement($e);
149
    }
150
}
151