AbstractSPKIDataType::fromXML()   B
last analyzed

Complexity

Conditions 10
Paths 14

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 19
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 29
rs 7.6666

How to fix   Complexity   

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\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\Registry\ElementRegistry;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XML\SerializableElementInterface;
16
use SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement;
17
18
/**
19
 * Abstract class representing the SPKIDataType.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
abstract class AbstractSPKIDataType extends AbstractDsElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\AbstractSPKIDataType: $message, $line
Loading history...
26
27
    /**
28
     * Initialize a SPKIData element.
29
     *
30
     * @param array{
31
     *   array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}
32
     * } $tuples
33
     */
34
    final public function __construct(
35
        protected array $tuples,
36
    ) {
37
        Assert::allIsArray($tuples, SchemaViolationException::class);
38
        Assert::allCount($tuples, 2);
39
40
        foreach ($tuples as $tuple) {
41
            Assert::isInstanceOf($tuple[0], SPKISexp::class, SchemaViolationException::class);
42
            Assert::nullOrIsInstanceOf($tuple[1], SerializableElementInterface::class, SchemaViolationException::class);
43
        }
44
    }
45
46
47
    /**
48
     * Collect the value of the SPKISexp-property
49
     *
50
     * @return array{array{\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, \SimpleSAML\XML\SerializableElementInterface|null}}
0 ignored issues
show
Documentation Bug introduced by
The doc comment array{array{\SimpleSAML\...ElementInterface|null}} at position 2 could not be parsed: Expected ':' at position 2, but found 'array'.
Loading history...
51
     */
52
    public function getTuples(): array
53
    {
54
        return $this->tuples;
55
    }
56
57
58
    /**
59
     * Convert XML into a SPKIData
60
     *
61
     * @param \DOMElement $xml The XML element we should load
62
     * @return static
63
     *
64
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
65
     *   If the qualified name of the supplied element is wrong
66
     */
67
    public static function fromXML(DOMElement $xml): static
68
    {
69
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
70
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
71
72
        $registry = ElementRegistry::getInstance();
73
        $tuples = [];
74
        $tuple = [null, null];
75
        foreach ($xml->childNodes as $node) {
76
            if ($node instanceof DOMElement) {
77
                if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
78
                    if ($tuple[0] !== null) {
79
                        $tuples[] = $tuple;
80
                    }
81
                    $tuple = [SPKISexp::fromXML($node), null];
82
                } elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
83
                    $handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
84
                    $tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
85
                    $tuples[] = $tuple;
86
                    $tuple = [null, null];
87
                }
88
            }
89
        }
90
91
        if ($tuple[0] !== null) {
92
            $tuples[] = $tuple;
93
        }
94
95
        return new static($tuples);
96
    }
97
98
99
    /**
100
     * Convert this SPKIData to XML.
101
     *
102
     * @param \DOMElement|null $parent The element we should append this SPKIData to.
103
     * @return \DOMElement
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
109
        foreach ($this->getTuples() as $tuple) {
110
            $tuple[0]->toXML($e);
111
            $tuple[1]?->toXML($e);
112
        }
113
114
        return $e;
115
    }
116
}
117