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