Response::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 9
dl 0
loc 23
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 SimpleSAML\SAML2\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\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...
10
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...
11
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...
12
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
13
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...
14
use SimpleSAML\SAML2\Type\SAMLStringValue;
15
use SimpleSAML\SAML2\XML\saml\Assertion;
16
use SimpleSAML\SAML2\XML\saml\EncryptedAssertion;
17
use SimpleSAML\SAML2\XML\saml\Issuer;
18
use SimpleSAML\XML\SchemaValidatableElementInterface;
19
use SimpleSAML\XML\SchemaValidatableElementTrait;
20
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
21
use SimpleSAML\XMLSchema\Exception\MissingElementException;
22
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
23
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...
24
use SimpleSAML\XMLSchema\Type\NCNameValue;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSchema\Type\NCNameValue 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...
25
use SimpleSAML\XMLSecurity\XML\ds\Signature;
26
27
use function array_merge;
28
use function array_pop;
29
use function strval;
30
31
/**
32
 * Class for SAML 2 Response messages.
33
 *
34
 * @package simplesamlphp/saml2
35
 */
36
class Response extends AbstractStatusResponse implements SchemaValidatableElementInterface
37
{
38
    use SchemaValidatableElementTrait;
39
40
41
    /**
42
     * Constructor for SAML 2 response messages.
43
     *
44
     * @param \SimpleSAML\XMLSchema\Type\IDValue $id
45
     * @param \SimpleSAML\SAML2\XML\samlp\Status $status
46
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue $issueInstant
47
     * @param \SimpleSAML\SAML2\XML\saml\Issuer|null $issuer
48
     * @param \SimpleSAML\XMLSchema\Type\NCNameValue|null $inResponseTo
49
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $destination
50
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $consent
51
     * @param \SimpleSAML\SAML2\XML\samlp\Extensions $extensions
52
     * @param (\SimpleSAML\SAML2\XML\saml\Assertion|\SimpleSAML\SAML2\XML\saml\EncryptedAssertion)[] $assertions
53
     */
54
    final public function __construct(
55
        IDValue $id,
56
        Status $status,
57
        SAMLDateTimeValue $issueInstant,
58
        ?Issuer $issuer = null,
59
        ?NCNameValue $inResponseTo = null,
60
        ?SAMLAnyURIValue $destination = null,
61
        ?SAMLAnyURIValue $consent = null,
62
        ?Extensions $extensions = null,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\Extensions 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...
63
        protected array $assertions = [],
64
    ) {
65
        Assert::maxCount($assertions, C::UNBOUNDED_LIMIT);
66
        Assert::allIsInstanceOfAny($assertions, [Assertion::class, EncryptedAssertion::class]);
67
68
        parent::__construct(
69
            $id,
70
            $status,
71
            $issueInstant,
72
            $issuer,
73
            $inResponseTo,
74
            $destination,
75
            $consent,
76
            $extensions,
77
        );
78
    }
79
80
81
    /**
82
     * Retrieve the assertions in this response.
83
     *
84
     * @return \SimpleSAML\SAML2\XML\saml\Assertion[]|\SimpleSAML\SAML2\XML\saml\EncryptedAssertion[]
85
     */
86
    public function getAssertions(): array
87
    {
88
        return $this->assertions;
89
    }
90
91
92
    /**
93
     * Convert XML into a Response element.
94
     *
95
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
96
     *   if the qualified name of the supplied element is wrong
97
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
98
     *   if the supplied element is missing one of the mandatory attributes
99
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
100
     *   if one of the mandatory child-elements is missing
101
     */
102
    public static function fromXML(DOMElement $xml): static
103
    {
104
        Assert::same($xml->localName, 'Response', InvalidDOMElementException::class);
105
        Assert::same($xml->namespaceURI, Response::NS, InvalidDOMElementException::class);
106
107
        $version = self::getAttribute($xml, 'Version', SAMLStringValue::class);
108
        Assert::true(version_compare('2.0', strval($version), '<='), RequestVersionTooLowException::class);
109
        Assert::true(version_compare('2.0', strval($version), '>='), RequestVersionTooHighException::class);
110
111
        $signature = Signature::getChildrenOfClass($xml);
112
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
113
114
        $issuer = Issuer::getChildrenOfClass($xml);
115
        Assert::countBetween($issuer, 0, 1);
116
117
        $status = Status::getChildrenOfClass($xml);
118
        Assert::minCount($status, 1, MissingElementException::class);
119
        Assert::maxCount($status, 1, TooManyElementsException::class);
120
121
        $extensions = Extensions::getChildrenOfClass($xml);
122
        Assert::maxCount(
123
            $extensions,
124
            1,
125
            'Only one saml:Extensions element is allowed.',
126
            TooManyElementsException::class,
127
        );
128
129
        $response = new static(
130
            self::getAttribute($xml, 'ID', IDValue::class),
131
            array_pop($status),
132
            self::getAttribute($xml, 'IssueInstant', SAMLDateTimeValue::class),
133
            empty($issuer) ? null : array_pop($issuer),
134
            self::getOptionalAttribute($xml, 'InResponseTo', NCNameValue::class, null),
135
            self::getOptionalAttribute($xml, 'Destination', SAMLAnyURIValue::class, null),
136
            self::getOptionalAttribute($xml, 'Consent', SAMLAnyURIValue::class, null),
137
            empty($extensions) ? null : array_pop($extensions),
138
            array_merge(Assertion::getChildrenOfClass($xml), EncryptedAssertion::getChildrenOfClass($xml)),
139
        );
140
141
        if (!empty($signature)) {
142
            $response->setSignature($signature[0]);
143
            $response->messageContainedSignatureUponConstruction = true;
144
            $response->setXML($xml);
145
        }
146
147
        return $response;
148
    }
149
150
151
    /**
152
     * Convert the response message to an XML element.
153
     */
154
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
155
    {
156
        $e = parent::toUnsignedXML($parent);
157
158
        foreach ($this->getAssertions() as $assertion) {
159
            $assertion->toXML($e);
160
        }
161
162
        return $e;
163
    }
164
}
165