Passed
Push — master ( b1bcd1...5ac656 )
by Tim
12:37
created

Body   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 125
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 20 2
A getFault() 0 3 1
A toXML() 0 16 3
A isEmptyElement() 0 3 3
A fromXML() 0 28 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SOAP12\XML\env;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SOAP\Constants as C;
10
use SimpleSAML\SOAP\Exception\ProtocolViolationException;
11
use SimpleSAML\XML\Chunk;
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\ExtendableAttributesTrait;
14
use SimpleSAML\XML\ExtendableElementTrait;
15
use SimpleSAML\XML\XsNamespace as NS;
16
17
use function array_diff;
18
use function array_filter;
19
use function array_pop;
20
use function array_values;
21
22
/**
23
 * Class representing a env:Body element.
24
 *
25
 * @package simplesaml/xml-soap
26
 */
27
final class Body extends AbstractSoapElement
28
{
29
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SOAP12\XML\env\Body: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
30
    use ExtendableElementTrait;
31
32
    /** The namespace-attribute for the xs:any element */
33
    public const XS_ANY_ELT_NAMESPACE = NS::ANY;
34
35
    /** The namespace-attribute for the xs:anyAttribute element */
36
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
37
38
39
    /**
40
     * Initialize a soap:Body
41
     *
42
     * @param \SimpleSAML\SOAP12\XML\Fault|null $fault
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP12\XML\Fault 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...
43
     * @param \SimpleSAML\XML\ElementInterface[] $children
44
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SOAP12\XML\env\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...
45
     */
46
    public function __construct(
47
        protected ?Fault $fault = null,
48
        array $children = [],
49
        array $namespacedAttributes = []
50
    ) {
51
        if ($fault !== null) {
52
            /**
53
             * 5.4: When generating a fault, SOAP senders MUST NOT include additional element
54
             *      information items in the SOAP Body .
55
             */
56
            Assert::isEmpty(
57
                $children,
58
                "When generating a fault, SOAP senders MUST NOT include additional elements in the SOAP Body.",
59
                ProtocolViolationException::class,
60
            );
61
        }
62
        Assert::allNotInstanceOf($children, Fault::class, ProtocolViolationException::class);
63
64
        $this->setElements($children);
65
        $this->setAttributesNS($namespacedAttributes);
66
    }
67
68
69
    /**
70
     * @return \SimpleSAML\SOAP12\XML\env\Fault|null
71
     */
72
    public function getFault(): ?Fault
73
    {
74
        return $this->fault;
75
    }
76
77
78
    /**
79
     * Test if an object, at the state it's in, would produce an empty XML-element
80
     *
81
     * @return bool
82
     */
83
    public function isEmptyElement(): bool
84
    {
85
        return empty($this->fault) && empty($this->elements) && empty($this->namespacedAttributes);
86
    }
87
88
89
    /*
90
     * Convert XML into an Body element
91
     *
92
     * @param \DOMElement $xml The XML element we should load
93
     * @return static
94
     *
95
     * @throws \SimpleSAML\XML\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, 'Body', InvalidDOMElementException::class);
101
        Assert::same($xml->namespaceURI, Body::NS, InvalidDOMElementException::class);
102
103
        $children = $fault = [];
104
        foreach ($xml->childNodes as $child) {
105
            if (!($child instanceof DOMElement)) {
106
                continue;
107
            } elseif ($child->namespaceURI === C::NS_SOAP_ENV_12) {
108
                if ($child->localName === 'Fault') {
109
                    $fault = Fault::fromXML($child);
110
                    continue;
111
                }
112
            }
113
            $children[] = new Chunk($child);
114
        }
115
116
        /**
117
         * 5.4: To be recognized as carrying SOAP error information, a SOAP message MUST contain a single SOAP Fault
118
         *      element information item as the only child element information item of the SOAP Body .
119
         */
120
        Assert::maxCount($fault, 1, ProtocolViolationException::class);
121
122
        return new static(
123
            array_pop($fault),
0 ignored issues
show
Bug introduced by
It seems like $fault can also be of type SimpleSAML\SOAP12\XML\env\Fault; however, parameter $array of array_pop() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

123
            array_pop(/** @scrutinizer ignore-type */ $fault),
Loading history...
124
            $children,
125
            self::getAttributesNSFromXML($xml)
126
        );
127
    }
128
129
130
    /**
131
     * Convert this Body to XML.
132
     *
133
     * @param \DOMElement|null $parent The element we should add this Body to.
134
     * @return \DOMElement This Body-element.
135
     */
136
    public function toXML(DOMElement $parent = null): DOMElement
137
    {
138
        $e = $this->instantiateParentElement($parent);
139
140
        foreach ($this->getAttributesNS() as $attr) {
141
            $attr->toXML($e);
142
        }
143
144
        $this->getFault()?->toXML($e);
145
146
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $child */
147
        foreach ($this->getElements() as $child) {
148
            $child->toXML($e);
149
        }
150
151
        return $e;
152
    }
153
}
154