Passed
Push — master ( 9afd28...a5eb5d )
by Tim
01:39
created

Code   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 3 1
A toXML() 0 8 1
A getSubcode() 0 3 1
A __construct() 0 15 2
A fromXML() 0 19 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP12\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SOAP\Constants as C;
10
use SimpleSAML\SOAP\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\TooManyElementsException;
14
15
/**
16
 * Class representing a env:Code element.
17
 *
18
 * @package simplesaml/xml-soap
19
 */
20
final class Code extends AbstractSoapElement
21
{
22
    /**
23
     * Initialize a soap:Code
24
     *
25
     * @param \SimpleSAML\SOAP12\XML\env\Value $value
26
     * @param \SimpleSAML\SOAP12\XML\env\Code|null $code
27
     */
28
    public function __construct(
29
        protected Value $value,
30
        protected ?Subcode $subcode = null
31
    ) {
32
        @list($prefix, $localName) = preg_split('/:/', $value->getContent(), 2);
33
        if ($localName === null) {
34
            // We don't have a prefixed value here
35
            $localName = $prefix;
36
        }
37
38
        Assert::oneOf(
39
            $localName,
40
            C::FAULT_CODES,
41
            'Invalid top-level Value',
42
            ProtocolViolationException::class
43
        );
44
    }
45
46
47
    /**
48
     * @return \SimpleSAML\SOAP12\XML\env\Value
49
     */
50
    public function getValue(): Value
51
    {
52
        return $this->value;
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\SOAP12\XML\env\Subcode|null
58
     */
59
    public function getSubcode(): ?Subcode
60
    {
61
        return $this->subcode;
62
    }
63
64
65
    /**
66
     * Convert XML into an Code element
67
     *
68
     * @param \DOMElement $xml The XML element we should load
69
     * @return static
70
     *
71
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
72
     *   If the qualified name of the supplied element is wrong
73
     */
74
    public static function fromXML(DOMElement $xml): static
75
    {
76
        Assert::same($xml->localName, 'Code', InvalidDOMElementException::class);
77
        Assert::same($xml->namespaceURI, Code::NS, InvalidDOMElementException::class);
78
79
        $value = Value::getChildrenOfClass($xml);
80
        Assert::count($value, 1, 'Must contain exactly one Value', MissingElementException::class);
81
82
        // Assert that the namespace of the value matches the SOAP-ENV namespace
83
        @list($prefix, $localName) = preg_split('/:/', $value[0]->getContent(), 2);
84
        $namespace = $xml->lookupNamespaceUri($prefix);
85
        Assert::same($namespace, C::NS_SOAP_ENV_12);
86
87
        $subcode = Subcode::getChildrenOfClass($xml);
88
        Assert::maxCount($subcode, 1, 'Cannot process more than one Subcode element.', TooManyElementsException::class);
89
90
        return new static(
91
            array_pop($value),
92
            empty($subcode) ? null : array_pop($subcode)
93
        );
94
    }
95
96
97
    /**
98
     * Convert this Code to XML.
99
     *
100
     * @param \DOMElement|null $parent The element we should add this code to.
101
     * @return \DOMElement This Code-element.
102
     */
103
    public function toXML(DOMElement $parent = null): DOMElement
104
    {
105
        $e = $this->instantiateParentElement($parent);
106
107
        $this->value->toXML($e);
108
        $this->subcode?->toXML($e);
109
110
        return $e;
111
    }
112
}
113