Organization::processArrayContents()   B
last analyzed

Complexity

Conditions 7
Paths 32

Size

Total Lines 58
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 34
nc 32
nop 1
dl 0
loc 58
rs 8.4426
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Exception\ArrayValidationException;
10
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
11
use SimpleSAML\SAML2\XML\ExtendableElementTrait;
12
use SimpleSAML\XML\ArrayizableElementInterface;
13
use SimpleSAML\XML\Attribute as XMLAttribute;
14
use SimpleSAML\XML\Constants as C;
15
use SimpleSAML\XML\Exception\InvalidDOMElementException;
16
use SimpleSAML\XML\Exception\MissingElementException;
17
use SimpleSAML\XML\Exception\TooManyElementsException;
18
use SimpleSAML\XML\ExtendableAttributesTrait;
19
use SimpleSAML\XML\SchemaValidatableElementInterface;
20
use SimpleSAML\XML\SchemaValidatableElementTrait;
21
use SimpleSAML\XML\XsNamespace as NS;
22
23
use function array_change_key_case;
24
use function array_filter;
25
use function array_key_exists;
26
use function array_keys;
27
use function array_merge;
28
29
/**
30
 * Class representing SAML 2 Organization element.
31
 *
32
 * @package simplesamlphp/saml2
33
 */
34
final class Organization extends AbstractMdElement implements
35
    ArrayizableElementInterface,
36
    SchemaValidatableElementInterface
37
{
38
    use ExtendableAttributesTrait;
39
    use ExtendableElementTrait;
40
    use SchemaValidatableElementTrait;
41
42
43
    /** The namespace-attribute for the xs:anyAttribute element */
44
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
45
46
47
    /**
48
     * Organization constructor.
49
     *
50
     * @param \SimpleSAML\SAML2\XML\md\OrganizationName[] $organizationName
51
     * @param \SimpleSAML\SAML2\XML\md\OrganizationDisplayName[] $organizationDisplayName
52
     * @param \SimpleSAML\SAML2\XML\md\OrganizationURL[] $organizationURL
53
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
54
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list 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...
55
     */
56
    public function __construct(
57
        protected array $organizationName,
58
        protected array $organizationDisplayName,
59
        protected array $organizationURL,
60
        ?Extensions $extensions = null,
61
        array $namespacedAttributes = [],
62
    ) {
63
        Assert::maxCount($organizationName, C::UNBOUNDED_LIMIT);
64
        Assert::maxCount($organizationDisplayName, C::UNBOUNDED_LIMIT);
65
        Assert::maxCount($organizationURL, C::UNBOUNDED_LIMIT);
66
67
        // [One or More]
68
        Assert::minCount($organizationName, 1, ProtocolViolationException::class);
69
        Assert::minCount($organizationDisplayName, 1, ProtocolViolationException::class);
70
        Assert::minCount($organizationURL, 1, ProtocolViolationException::class);
71
72
        Assert::allIsInstanceOf($organizationName, OrganizationName::class);
73
        Assert::allIsInstanceOf($organizationDisplayName, OrganizationDisplayName::class);
74
        Assert::allIsInstanceOf($organizationURL, OrganizationURL::class);
75
76
        $this->setExtensions($extensions);
77
        $this->setAttributesNS($namespacedAttributes);
78
    }
79
80
81
    /**
82
     * Collect the value of the OrganizationName property.
83
     *
84
     * @return \SimpleSAML\SAML2\XML\md\OrganizationName[]
85
     */
86
    public function getOrganizationName(): array
87
    {
88
        return $this->organizationName;
89
    }
90
91
92
    /**
93
     * Collect the value of the OrganizationDisplayName property.
94
     *
95
     * @return \SimpleSAML\SAML2\XML\md\OrganizationDisplayName[]
96
     */
97
    public function getOrganizationDisplayName(): array
98
    {
99
        return $this->organizationDisplayName;
100
    }
101
102
103
    /**
104
     * Collect the value of the OrganizationURL property.
105
     *
106
     * @return \SimpleSAML\SAML2\XML\md\OrganizationURL[]
107
     */
108
    public function getOrganizationURL(): array
109
    {
110
        return $this->organizationURL;
111
    }
112
113
114
    /**
115
     * Initialize an Organization element.
116
     *
117
     * @param \DOMElement $xml The XML element we should load.
118
     * @return static
119
     *
120
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
121
     *   if the qualified name of the supplied element is wrong
122
     * @throws \SimpleSAML\XML\Exception\MissingElementException
123
     *   if one of the mandatory child-elements is missing
124
     */
125
    public static function fromXML(DOMElement $xml): static
126
    {
127
        Assert::same($xml->localName, 'Organization', InvalidDOMElementException::class);
128
        Assert::same($xml->namespaceURI, Organization::NS, InvalidDOMElementException::class);
129
130
        $names = OrganizationName::getChildrenOfClass($xml);
131
        Assert::minCount($names, 1, 'Missing at least one OrganizationName.', MissingElementException::class);
132
133
        $displayNames = OrganizationDisplayName::getChildrenOfClass($xml);
134
        Assert::minCount(
135
            $displayNames,
136
            1,
137
            'Missing at least one OrganizationDisplayName',
138
            MissingElementException::class,
139
        );
140
141
        $urls = OrganizationURL::getChildrenOfClass($xml);
142
        Assert::minCount($urls, 1, 'Missing at least one OrganizationURL', MissingElementException::class);
143
144
        $extensions = Extensions::getChildrenOfClass($xml);
145
        Assert::maxCount(
146
            $extensions,
147
            1,
148
            'Cannot process more than one md:Extensions element.',
149
            TooManyElementsException::class,
150
        );
151
152
        return new static(
153
            $names,
154
            $displayNames,
155
            $urls,
156
            !empty($extensions) ? $extensions[0] : null,
157
            self::getAttributesNSFromXML($xml),
158
        );
159
    }
160
161
162
    /**
163
     * Convert this Organization to XML.
164
     *
165
     * @param \DOMElement|null $parent The element we should add this organization to.
166
     * @return \DOMElement This Organization-element.
167
     */
168
    public function toXML(?DOMElement $parent = null): DOMElement
169
    {
170
        $e = $this->instantiateParentElement($parent);
171
172
        foreach ($this->getAttributesNS() as $attr) {
173
            $attr->toXML($e);
174
        }
175
176
        $this->getExtensions()?->toXML($e);
177
178
        foreach ($this->getOrganizationName() as $name) {
179
            $name->toXML($e);
180
        }
181
182
        foreach ($this->getOrganizationDisplayName() as $displayName) {
183
            $displayName->toXML($e);
184
        }
185
186
        foreach ($this->getOrganizationURL() as $url) {
187
            $url->toXML($e);
188
        }
189
190
        return $e;
191
    }
192
193
194
    /**
195
     * Create a class from an array
196
     *
197
     * @param array $data
198
     * @return static
199
     */
200
    public static function fromArray(array $data): static
201
    {
202
        $data = self::processArrayContents($data);
203
204
        return new static(
205
            $data['OrganizationName'],
206
            $data['OrganizationDisplayName'],
207
            $data['OrganizationURL'],
208
            $data['Extensions'] ?? null,
209
            $data['attributes'] ?? [],
210
        );
211
    }
212
213
214
    /**
215
     * Validates an array representation of this object and returns the same array with
216
     * rationalized keys (casing) and parsed sub-elements.
217
     *
218
     * @param array $data
219
     * @return array $data
220
     */
221
    private static function processArrayContents(array $data): array
222
    {
223
        $data = array_change_key_case($data, CASE_LOWER);
224
225
        // Make sure the array keys are known for this kind of object
226
        Assert::allOneOf(
227
            array_keys($data),
228
            [
229
                'organizationname',
230
                'organizationdisplayname',
231
                'organizationurl',
232
                'extensions',
233
                'attributes',
234
            ],
235
            ArrayValidationException::class,
236
        );
237
238
        Assert::keyExists($data, 'organizationname', ArrayValidationException::class);
239
        Assert::keyExists($data, 'organizationdisplayname', ArrayValidationException::class);
240
        Assert::keyExists($data, 'organizationurl', ArrayValidationException::class);
241
242
        // The minimum count is validated by the constructor
243
        Assert::isArray($data['organizationname'], ArrayValidationException::class);
244
        Assert::isArray($data['organizationdisplayname'], ArrayValidationException::class);
245
        Assert::isArray($data['organizationurl'], ArrayValidationException::class);
246
247
        foreach ($data['organizationname'] as $lang => $orgName) {
248
            $data['organizationname'][$lang] = OrganizationName::fromArray([$lang => $orgName]);
249
        }
250
251
        foreach ($data['organizationdisplayname'] as $lang => $orgDisplayName) {
252
            $data['organizationdisplayname'][$lang] = OrganizationDisplayName::fromArray([$lang => $orgDisplayName]);
253
        }
254
255
        foreach ($data['organizationurl'] as $lang => $orgUrl) {
256
            $data['organizationurl'][$lang] = OrganizationURL::fromArray([$lang => $orgUrl]);
257
        }
258
259
        $retval = [
260
            'OrganizationName' => $data['organizationname'],
261
            'OrganizationDisplayName' => $data['organizationdisplayname'],
262
            'OrganizationURL' => $data['organizationurl'],
263
        ];
264
265
        if (array_key_exists('extensions', $data)) {
266
            Assert::isArray($data['extensions'], ArrayValidationException::class);
267
            $retval['Extensions'] = new Extensions($data['extensions']);
268
        }
269
270
        if (array_key_exists('attributes', $data)) {
271
            Assert::isArray($data['attributes'], ArrayValidationException::class);
272
            Assert::allIsArray($data['attributes'], ArrayValidationException::class);
273
            foreach ($data['attributes'] as $i => $attr) {
274
                $retval['attributes'][] = XMLAttribute::fromArray($attr);
275
            }
276
        }
277
278
        return $retval;
279
    }
280
281
282
    /**
283
     * Create an array from this class
284
     *
285
     * @return array
286
     */
287
    public function toArray(): array
288
    {
289
        $data = [
290
            'OrganizationName' => [],
291
            'OrganizationDisplayName' => [],
292
            'OrganizationURL' => [],
293
            'Extensions' => $this->getExtensions()?->getList(),
0 ignored issues
show
Bug introduced by
The method getList() does not exist on SimpleSAML\XML\AbstractElement. It seems like you code against a sub-type of SimpleSAML\XML\AbstractElement such as SimpleSAML\SAML2\XML\md\Extensions or SimpleSAML\SAML2\XML\samlp\Extensions. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

293
            'Extensions' => $this->getExtensions()?->/** @scrutinizer ignore-call */ getList(),
Loading history...
294
            'attributes' => [],
295
        ];
296
297
        foreach ($this->getOrganizationName() as $orgName) {
298
            $data['OrganizationName'] = array_merge($data['OrganizationName'], $orgName->toArray());
299
        }
300
301
        foreach ($this->getOrganizationDisplayName() as $orgDisplayName) {
302
            $data['OrganizationDisplayName'] = array_merge(
303
                $data['OrganizationDisplayName'],
304
                $orgDisplayName->toArray(),
305
            );
306
        }
307
308
        foreach ($this->getOrganizationURL() as $orgURL) {
309
            $data['OrganizationURL'] = array_merge($data['OrganizationURL'], $orgURL->toArray());
310
        }
311
312
        foreach ($this->getAttributesNS() as $attr) {
313
            $data['attributes'][] = $attr->toArray();
314
        }
315
316
        return array_filter($data);
317
    }
318
}
319