Fault::fromXML()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 1
nop 1
dl 0
loc 26
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP\XML\env_200305;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\DOMDocumentFactory;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
16
/**
17
 * Class representing a env:Fault element.
18
 *
19
 * @package simplesaml/xml-soap
20
 */
21
final class Fault extends AbstractSoapElement implements SchemaValidatableElementInterface
22
{
23
    use SchemaValidatableElementTrait;
24
25
    /**
26
     * Initialize a env:Fault
27
     *
28
     * @param \SimpleSAML\SOAP\XML\env_200305\Code $code
29
     * @param \SimpleSAML\SOAP\XML\env_200305\Reason $reason
30
     * @param \SimpleSAML\SOAP\XML\env_200305\Node|null $node
31
     * @param \SimpleSAML\SOAP\XML\env_200305\Role|null $role
32
     * @param \SimpleSAML\SOAP\XML\env_200305\Detail|null $detail
33
     */
34
    public function __construct(
35
        protected Code $code,
36
        protected Reason $reason,
37
        protected ?Node $node = null,
38
        protected ?Role $role = null,
39
        protected ?Detail $detail = null,
40
    ) {
41
    }
42
43
44
    /**
45
     * @return \SimpleSAML\SOAP\XML\env_200305\Code
46
     */
47
    public function getCode(): Code
48
    {
49
        return $this->code;
50
    }
51
52
53
    /**
54
     * @return \SimpleSAML\SOAP\XML\env_200305\Reason
55
     */
56
    public function getReason(): Reason
57
    {
58
        return $this->reason;
59
    }
60
61
62
    /**
63
     * @return \SimpleSAML\SOAP\XML\env_200305\Node|null
64
     */
65
    public function getNode(): ?Node
66
    {
67
        return $this->node;
68
    }
69
70
71
    /**
72
     * @return \SimpleSAML\SOAP\XML\env_200305\Role|null
73
     */
74
    public function getRole(): ?Role
75
    {
76
        return $this->role;
77
    }
78
79
80
    /**
81
     * @return \SimpleSAML\SOAP\XML\env_200305\Detail|null
82
     */
83
    public function getDetail(): ?Detail
84
    {
85
        return $this->detail;
86
    }
87
88
89
    /**
90
     * Convert XML into an Fault element
91
     *
92
     * @param \DOMElement $xml The XML element we should load
93
     * @return static
94
     *
95
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
96
     *   If the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        Assert::same($xml->localName, 'Fault', InvalidDOMElementException::class);
101
        Assert::same($xml->namespaceURI, Fault::NS, InvalidDOMElementException::class);
102
103
        $code = Code::getChildrenOfClass($xml);
104
        Assert::count($code, 1, 'Must contain exactly one Code', MissingElementException::class);
105
106
        $reason = Reason::getChildrenOfClass($xml);
107
        Assert::count($reason, 1, 'Must contain exactly one Reason', MissingElementException::class);
108
109
        $node = Node::getChildrenOfClass($xml);
110
        Assert::maxCount($node, 1, 'Cannot process more than one Node element.', TooManyElementsException::class);
111
112
        $role = Role::getChildrenOfClass($xml);
113
        Assert::maxCount($role, 1, 'Cannot process more than one Role element.', TooManyElementsException::class);
114
115
        $detail = Detail::getChildrenOfClass($xml);
116
        Assert::maxCount($detail, 1, 'Cannot process more than one Detail element.', TooManyElementsException::class);
117
118
        return new self(
119
            array_pop($code),
120
            array_pop($reason),
121
            empty($node) ? null : array_pop($node),
122
            empty($role) ? null : array_pop($role),
123
            empty($detail) ? null : array_pop($detail),
124
        );
125
    }
126
127
128
    /**
129
     * Convert this Fault to XML.
130
     *
131
     * @param \DOMElement|null $parent The element we should add this fault to.
132
     * @return \DOMElement This Fault-element.
133
     */
134
    public function toXML(?DOMElement $parent = null): DOMElement
135
    {
136
        $e = $this->instantiateParentElement($parent);
137
138
        $this->getCode()->toXML($e);
139
        $this->getReason()->toXML($e);
140
141
        $this->getNode()?->toXML($e);
142
        $this->getRole()?->toXML($e);
143
144
        if ($this->getDetail() !== null && !$this->getDetail()->isEmptyElement()) {
145
            $this->getDetail()->toXML($e);
146
        }
147
148
        // Dirty hack to get the namespaces in the right place. They cannot be in the env:Value element
149
        $doc = DOMDocumentFactory::create();
150
        $doc->appendChild($doc->importNode($e, true));
151
152
        return $doc->documentElement;
153
    }
154
}
155