Fault::getCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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