Fault::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 27
rs 9.6666
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP11\XML;
6
7
use DOMElement;
8
use SimpleSAML\SOAP11\Assert\Assert;
9
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
10
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
11
12
/**
13
 * Class representing a SOAP-ENV:Fault element.
14
 *
15
 * @package simplesaml/xml-soap
16
 */
17
final class Fault extends AbstractSoapElement implements SchemaValidatableElementInterface
18
{
19
    use SchemaValidatableElementTrait;
20
21
    /**
22
     * Initialize a SOAP-ENV:Fault
23
     *
24
     * @param \SimpleSAML\SOAP11\XML\FaultCode $faultCode
25
     * @param \SimpleSAML\SOAP11\XML\FaultString $faultString
26
     * @param \SimpleSAML\SOAP11\XML\FaultActor|null $faultActor
27
     * @param \SimpleSAML\SOAP11\XML\Detail|null $detail
28
     */
29
    public function __construct(
30
        protected FaultCode $faultCode,
31
        protected FaultString $faultString,
32
        protected ?FaultActor $faultActor = null,
33
        protected ?Detail $detail = null,
34
    ) {
35
    }
36
37
38
    /**
39
     * @return \SimpleSAML\SOAP11\XML\FaultCode
40
     */
41
    public function getFaultCode(): FaultCode
42
    {
43
        return $this->faultCode;
44
    }
45
46
47
    /**
48
     * @return \SimpleSAML\SOAP11\XML\FaultString
49
     */
50
    public function getFaultString(): FaultString
51
    {
52
        return $this->faultString;
53
    }
54
55
56
    /**
57
     * @return \SimpleSAML\SOAP11\XML\FaultActor|null
58
     */
59
    public function getFaultActor(): ?FaultActor
60
    {
61
        return $this->faultActor;
62
    }
63
64
65
    /**
66
     * @return \SimpleSAML\SOAP11\XML\Detail|null
67
     */
68
    public function getDetail(): ?Detail
69
    {
70
        return $this->detail;
71
    }
72
73
74
    /**
75
     * Convert XML into an Fault element
76
     *
77
     * @param \DOMElement $xml The XML element we should load
78
     * @return static
79
     *
80
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
81
     *   If the qualified name of the supplied element is wrong
82
     */
83
    public static function fromXML(DOMElement $xml): static
84
    {
85
        Assert::same($xml->localName, 'Fault', InvalidDOMElementException::class);
86
        Assert::same($xml->namespaceURI, Fault::NS, InvalidDOMElementException::class);
87
88
        $faultCode = FaultCode::getChildrenOfClass($xml);
89
        Assert::count($faultCode, 1, 'Must contain exactly one faultcode', MissingElementException::class);
90
91
        $faultString = FaultString::getChildrenOfClass($xml);
92
        Assert::count($faultString, 1, 'Must contain exactly one faultstring', MissingElementException::class);
93
94
        $faultActor = FaultActor::getChildrenOfClass($xml);
95
        Assert::maxCount(
96
            $faultActor,
97
            1,
98
            'Cannot process more than one faultactor element.',
99
            TooManyElementsException::class,
100
        );
101
102
        $detail = Detail::getChildrenOfClass($xml);
103
        Assert::maxCount($detail, 1, 'Cannot process more than one detail element.', TooManyElementsException::class);
104
105
        return new self(
106
            array_pop($faultCode),
107
            array_pop($faultString),
108
            array_pop($faultActor),
109
            array_pop($detail),
110
        );
111
    }
112
113
114
    /**
115
     * Convert this Fault to XML.
116
     *
117
     * @param \DOMElement|null $parent The element we should add this fault to.
118
     * @return \DOMElement This Fault-element.
119
     */
120
    public function toXML(?DOMElement $parent = null): DOMElement
121
    {
122
        $e = $this->instantiateParentElement($parent);
123
124
        $this->getFaultCode()->toXML($e);
125
        $this->getFaultString()->toXML($e);
126
        $this->getFaultActor()?->toXML($e);
127
128
        if ($this->getDetail() !== null && !$this->getDetail()->isEmptyElement()) {
129
            $this->getDetail()->toXML($e);
130
        }
131
132
        return $e;
133
    }
134
}
135