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