IDPList   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 177
rs 10
c 0
b 0
f 0
wmc 13

8 Methods

Rating   Name   Duplication   Size   Complexity  
A fromArray() 0 7 1
A getGetComplete() 0 3 1
A getIdpEntry() 0 3 1
A __construct() 0 7 1
A toXML() 0 11 2
A fromXML() 0 24 2
A processArrayContents() 0 28 3
A toArray() 0 12 2
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\Exception\ArrayValidationException;
10
use SimpleSAML\XML\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XML\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...
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
14
use SimpleSAML\XMLSchema\Exception\MissingElementException;
15
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
16
17
use function array_change_key_case;
18
use function array_filter;
19
use function array_key_exists;
20
use function array_keys;
21
use function array_pop;
22
23
/**
24
 * Class for handling SAML2 IDPList.
25
 *
26
 * @package simplesamlphp/saml2
27
 */
28
final class IDPList extends AbstractSamlpElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\AbstractSamlpElement 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...
29
{
30
    use SchemaValidatableElementTrait;
31
32
33
    /**
34
     * Initialize an IDPList element.
35
     *
36
     * @param \SimpleSAML\SAML2\XML\samlp\IDPEntry[] $IDPEntry
37
     * @param \SimpleSAML\SAML2\XML\samlp\GetComplete|null $getComplete
38
     */
39
    public function __construct(
40
        protected array $IDPEntry,
41
        protected ?GetComplete $getComplete = null,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\samlp\GetComplete 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...
42
    ) {
43
        Assert::maxCount($IDPEntry, C::UNBOUNDED_LIMIT);
44
        Assert::minCount($IDPEntry, 1, 'At least one samlp:IDPEntry must be specified.');
45
        Assert::allIsInstanceOf($IDPEntry, IDPEntry::class);
46
    }
47
48
49
    /**
50
     * @return \SimpleSAML\SAML2\XML\samlp\IDPEntry[]
51
     */
52
    public function getIdpEntry(): array
53
    {
54
        return $this->IDPEntry;
55
    }
56
57
58
    /**
59
     * @return \SimpleSAML\SAML2\XML\samlp\GetComplete|null
60
     */
61
    public function getGetComplete(): ?GetComplete
62
    {
63
        return $this->getComplete;
64
    }
65
66
67
    /**
68
     * Convert XML into a IDPList-element
69
     *
70
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
71
     *   if the qualified name of the supplied element is wrong
72
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
73
     *   if one of the mandatory child-elements is missing
74
     * @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException
75
     *   if too many child-elements of a type are specified
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'IDPList', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, IDPList::NS, InvalidDOMElementException::class);
81
82
        $idpEntry = IDPEntry::getChildrenOfClass($xml);
83
        Assert::minCount(
84
            $idpEntry,
85
            1,
86
            'At least one <samlp:IDPEntry> must be specified.',
87
            MissingElementException::class,
88
        );
89
90
        $getComplete = GetComplete::getChildrenOfClass($xml);
91
        Assert::maxCount(
92
            $getComplete,
93
            1,
94
            'Only one <samlp:GetComplete> element is allowed.',
95
            TooManyElementsException::class,
96
        );
97
98
        return new static(
99
            $idpEntry,
100
            empty($getComplete) ? null : array_pop($getComplete),
101
        );
102
    }
103
104
105
    /**
106
     * Convert this IDPList to XML.
107
     */
108
    public function toXML(?DOMElement $parent = null): DOMElement
109
    {
110
        $e = $this->instantiateParentElement($parent);
111
112
        foreach ($this->getIDPEntry() as $idpEntry) {
113
            $idpEntry->toXML($e);
114
        }
115
116
        $this->getGetComplete()?->toXML($e);
117
118
        return $e;
119
    }
120
121
122
    /**
123
     * Create a class from an array
124
     *
125
     * @param array{
126
     *   'IDPEntry': string,
127
     *   'GetComplete'?: string,
128
     * } $data
129
     */
130
    public static function fromArray(array $data): static
131
    {
132
        $data = self::processArrayContents($data);
133
134
        return new static(
135
            $data['IDPEntry'],
136
            $data['GetComplete'] ?? null,
137
        );
138
    }
139
140
141
    /**
142
     * Validates an array representation of this object and returns the same array with
143
     * rationalized keys (casing) and parsed sub-elements.
144
     *
145
     * @param array{
146
     *   'IDPEntry': string,
147
     *   'GetComplete'?: string,
148
     * } $data
149
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
150
     *   'IDPEntry': string,
151
     *   'GetComplete'?: string,
152
     * }
153
     */
154
    private static function processArrayContents(array $data): array
155
    {
156
        $data = array_change_key_case($data, CASE_LOWER);
157
158
        // Make sure the array keys are known for this kind of object
159
        Assert::allOneOf(
160
            array_keys($data),
161
            [
162
                'idpentry',
163
                'getcomplete',
164
            ],
165
            ArrayValidationException::class,
166
        );
167
168
        Assert::keyExists($data, 'idpentry', ArrayValidationException::class);
169
        Assert::isArray($data['idpentry'], ArrayValidationException::class);
170
171
        $retval = ['IDPEntry' => [], 'GetComplete' => null];
172
173
        foreach ($data['idpentry'] as $entry) {
174
            $retval['IDPEntry'][] = IDPEntry::fromArray($entry);
175
        }
176
177
        if (array_key_exists('getcomplete', $data)) {
178
            $retval['GetComplete'] = GetComplete::fromArray($data['getcomplete']);
179
        }
180
181
        return $retval;
182
    }
183
184
185
    /**
186
     * Create an array from this class
187
     *
188
     * @return array{
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{ at position 2 could not be parsed: the token is null at position 2.
Loading history...
189
     *   'IDPEntry': string,
190
     *   'GetComplete'?: string,
191
     * }
192
     */
193
    public function toArray(): array
194
    {
195
        $data = [
196
            'IDPEntry' => [],
197
            'GetComplete' => $this->getGetComplete()?->toArray(),
198
        ];
199
200
        foreach ($this->getIDPEntry() as $entry) {
201
            $data['IDPEntry'][] = $entry->toArray();
202
        }
203
204
        return array_filter($data);
205
    }
206
}
207