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

Body   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

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