Passed
Pull Request — master (#58)
by Tim
02:17
created

AbstractSPKIDataType::fromXML()   B

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\Exception\TooManyElementsException;
13
use SimpleSAML\XML\Registry\ElementRegistry;
14
use SimpleSAML\XML\SerializableElementInterface;
15
use SimpleSAML\XML\XsNamespace as NS;
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
24
{
25
    /**
26
     * Initialize a SPKIData element.
27
     *
28
     * @param array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null> $tuples
29
     */
30
    final public function __construct(
31
        protected array $tuples,
32
    ) {
33
        Assert::allIsArray($tuples, SchemaViolationException::class);
34
        Assert::allCount($tuples, 2);
35
36
        foreach ($tuples as $tuple) {
37
            list($spkisExp, $other) = $tuple;
38
            Assert::isInstanceOf($spkisExp, SPKISexp::class, SchemaViolationException::class);
39
            Assert::nullOrIsInstanceOf($other, SerializableElementInterface::class, SchemaViolationException::class);
40
        }
41
    }
42
43
44
    /**
45
     * Collect the value of the SPKISexp-property
46
     *
47
     * @return array<\SimpleSAML\XMLSecurity\XML\ds\SPKISexp, SimpleSAML\XML\SerializableElementInterface|null>
48
     */
49
    public function getTuples(): array
50
    {
51
        return $this->tuples;
52
    }
53
54
55
    /**
56
     * Convert XML into a SPKIData
57
     *
58
     * @param \DOMElement $xml The XML element we should load
59
     * @return static
60
     *
61
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
62
     *   If the qualified name of the supplied element is wrong
63
     */
64
    public static function fromXML(DOMElement $xml): static
65
    {
66
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
67
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
68
69
        $registry = ElementRegistry::getInstance();
70
        $tuples = [];
71
        $tuple = [null, null];
72
        foreach ($xml->childNodes as $node) {
73
            if ($node instanceof DOMElement) {
74
                if ($node->namespaceURI === static::NS && $node->localName === 'SPKISexp') {
75
                    if ($tuple[0] !== null) {
76
                        $tuples[] = $tuple;
77
                    }
78
                    $tuple = [SPKISexp::fromXML($node), null];
79
                } elseif ($node->namespaceURI !== static::NS && $tuple[0] !== null) {
80
                    $handler = $registry->getElementHandler($node->namespaceURI, $node->localName);
81
                    $tuple[1] = ($handler === null) ? Chunk::fromXML($node) : $handler::fromXML($node);
82
                    $tuples[] = $tuple;
83
                    $tuple = [null, null];
84
                }
85
            }
86
        }
87
88
        if ($tuple[0] !== null) {
89
            $tuples[] = $tuple;
90
        }
91
92
        return new static($tuples);
93
    }
94
95
96
    /**
97
     * Convert this SPKIData to XML.
98
     *
99
     * @param \DOMElement|null $parent The element we should append this SPKIData to.
100
     * @return \DOMElement
101
     */
102
    public function toXML(?DOMElement $parent = null): DOMElement
103
    {
104
        $e = $this->instantiateParentElement($parent);
105
106
        foreach ($this->getTuples() as $tuple) {
107
            list($spkisExp, $other) = $tuple;
108
109
            $spkisExp->toXML($e);
110
            $other?->toXML($e);
111
        }
112
113
        return $e;
114
    }
115
}
116