Passed
Push — master ( 9afd28...a5eb5d )
by Tim
01:39
created

Fault::setFaultCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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