Passed
Push — master ( c93035...6d9219 )
by Tim
12:59 queued 11:11
created

Body   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFault() 0 3 1
A toXML() 0 18 4
A isEmptyElement() 0 3 3
A __construct() 0 14 1
A setFault() 0 3 1
A fromXML() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP\XML\env_200106;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SOAP\Exception\ProtocolViolationException;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_diff;
16
use function array_filter;
17
use function array_pop;
18
use function array_values;
19
20
/**
21
 * Class representing a env:Body element.
22
 *
23
 * @package simplesaml/xml-soap
24
 */
25
final class Body extends AbstractSoapElement
26
{
27
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SOAP\XML\env_200106\Body: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
28
    use ExtendableElementTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\SOAP\XML\env_200106\Body: $namespaceURI, $localName, $childNodes
Loading history...
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const XS_ANY_ATTR_NAMESPACE = NS::ANY;
35
36
    /**
37
     * @var \SimpleSAML\SOAP\XML\env_200106\Fault|null
38
     */
39
    protected ?Fault $fault;
40
41
42
    /**
43
     * Initialize a soap:Body
44
     *
45
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $children
46
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP\XML\env_200106\list 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...
47
     */
48
    public function __construct(array $children = [], array $namespacedAttributes = [])
49
    {
50
        /**
51
         * 4.4: If present, the SOAP Fault element MUST appear as a body entry and MUST NOT
52
         * appear more than once within a Body element.
53
         */
54
        $fault =  array_values(array_filter($children, function ($elt) {
55
            return $elt instanceof Fault;
56
        }));
57
        Assert::maxCount($fault, 1, ProtocolViolationException::class);
58
59
        $this->setFault(array_pop($fault));
60
        $this->setElements(array_diff($children, $fault));
61
        $this->setAttributesNS($namespacedAttributes);
62
    }
63
64
65
    /**
66
     * @param \SimpleSAML\SOAP\XML\env_200106\Fault|null $fault
67
     */
68
    public function setFault(?Fault $fault): void
69
    {
70
        $this->fault = $fault;
71
    }
72
73
74
    /**
75
     * @return \SimpleSAML\SOAP\XML\env_200106\Fault|null
76
     */
77
    public function getFault(): ?Fault
78
    {
79
        return $this->fault;
80
    }
81
82
83
    /**
84
     * Test if an object, at the state it's in, would produce an empty XML-element
85
     *
86
     * @return bool
87
     */
88
    public function isEmptyElement(): bool
89
    {
90
        return empty($this->fault) && empty($this->elements) && empty($this->namespacedAttributes);
91
    }
92
93
94
    /*
95
     * Convert XML into an Body element
96
     *
97
     * @param \DOMElement $xml The XML element we should load
98
     * @return static
99
     *
100
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
101
     *   If the qualified name of the supplied element is wrong
102
     */
103
    public static function fromXML(DOMElement $xml): static
104
    {
105
        Assert::same($xml->localName, 'Body', InvalidDOMElementException::class);
106
        Assert::same($xml->namespaceURI, Body::NS, InvalidDOMElementException::class);
107
108
        return new static(
109
            self::getChildElementsFromXML($xml),
110
            self::getAttributesNSFromXML($xml),
111
        );
112
    }
113
114
115
    /**
116
     * Convert this Body to XML.
117
     *
118
     * @param \DOMElement|null $parent The element we should add this Body to.
119
     * @return \DOMElement This Body-element.
120
     */
121
    public function toXML(DOMElement $parent = null): DOMElement
122
    {
123
        $e = $this->instantiateParentElement($parent);
124
125
        foreach ($this->getAttributesNS() as $attr) {
126
            $attr->toXML($e);
127
        }
128
129
        $this->getFault()?->toXML($e);
130
131
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
132
        foreach ($this->getElements() as $child) {
133
            if (!$child->isEmptyElement()) {
134
                $child->toXML($e);
135
            }
136
        }
137
138
        return $e;
139
    }
140
}
141