Fault::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 5
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
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP12\XML\AbstractSoapElement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP12\XML\Node was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
        protected ?Role $role = null,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP12\XML\Role was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
        protected ?Detail $detail = null,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP12\XML\Detail was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     *
95
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
96
     *   If the qualified name of the supplied element is wrong
97
     */
98
    public static function fromXML(DOMElement $xml): static
99
    {
100
        Assert::same($xml->localName, 'Fault', InvalidDOMElementException::class);
101
        Assert::same($xml->namespaceURI, Fault::NS, InvalidDOMElementException::class);
102
103
        $code = Code::getChildrenOfClass($xml);
104
        Assert::count($code, 1, 'Must contain exactly one Code', MissingElementException::class);
105
106
        $reason = Reason::getChildrenOfClass($xml);
107
        Assert::count($reason, 1, 'Must contain exactly one Reason', MissingElementException::class);
108
109
        $node = Node::getChildrenOfClass($xml);
110
        Assert::maxCount($node, 1, 'Cannot process more than one Node element.', TooManyElementsException::class);
111
112
        $role = Role::getChildrenOfClass($xml);
113
        Assert::maxCount($role, 1, 'Cannot process more than one Role element.', TooManyElementsException::class);
114
115
        $detail = Detail::getChildrenOfClass($xml);
116
        Assert::maxCount($detail, 1, 'Cannot process more than one Detail element.', TooManyElementsException::class);
117
118
        return new self(
119
            array_pop($code),
120
            array_pop($reason),
121
            empty($node) ? null : array_pop($node),
122
            empty($role) ? null : array_pop($role),
123
            empty($detail) ? null : array_pop($detail),
124
        );
125
    }
126
127
128
    /**
129
     * Convert this Fault to XML.
130
     *
131
     * @param \DOMElement|null $parent The element we should add this fault to.
132
     */
133
    public function toXML(?DOMElement $parent = null): DOMElement
134
    {
135
        $e = $this->instantiateParentElement($parent);
136
137
        $this->getCode()->toXML($e);
138
        $this->getReason()->toXML($e);
139
140
        $this->getNode()?->toXML($e);
141
        $this->getRole()?->toXML($e);
142
143
        if ($this->getDetail() !== null && !$this->getDetail()->isEmptyElement()) {
144
            $this->getDetail()->toXML($e);
145
        }
146
147
        // Dirty hack to get the namespaces in the right place. They cannot be in the env:Value element
148
        $doc = DOMDocumentFactory::create();
149
        $doc->appendChild($doc->importNode($e, true));
150
151
        return $doc->documentElement;
152
    }
153
}
154