Fault   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 31
dl 0
loc 117
rs 10
c 0
b 0
f 0

7 Methods

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