XMLDocumentStyle::generateMessagePart()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Copyright (C) 2013-2020
4
 * Piotr Olaszewski <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
namespace WSDL\XML\XMLStyle;
25
26
use DOMDocument;
27
use DOMElement;
28
use WSDL\Parser\Node;
29
use WSDL\XML\XMLAttributeHelper;
30
31
/**
32
 * XMLDocumentStyle
33
 *
34
 * @author Piotr Olaszewski <[email protected]>
35
 */
36
class XMLDocumentStyle implements XMLStyle
37
{
38 View Code Duplication
    public function generateBinding(DOMDocument $DOMDocument, string $soapVersion): DOMElement
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        return XMLAttributeHelper::forDOM($DOMDocument)
41 1
            ->createElementWithAttributes($soapVersion . ':binding', [
42
                'style' => 'document', 'transport' => 'http://schemas.xmlsoap.org/soap/http'
43 1
            ]);
44 1
    }
45 1
46 1
    /**
47
     * @inheritdoc
48
     */
49
    public function generateMessagePart(DOMDocument $DOMDocument, array $nodes): array
50
    {
51
        $parts = [];
52 1
        foreach ($nodes as $node) {
53
            $parts[] = XMLAttributeHelper::forDOM($DOMDocument)->createElementWithAttributes('part', [
54 1
                'name' => $node->getSanitizedName(), 'element' => 'ns:' . $node->getSanitizedName()
55 1
            ]);
56 1
        }
57 1
58 1
        return $parts;
59 1
    }
60 1
61
    /**
62
     * @inheritdoc
63
     */
64
    public function generateTypes(DOMDocument $DOMDocument, array $parameters, string $soapVersion): array
65
    {
66
        $types = [];
67
        foreach ($parameters as $parameter) {
68
            $node = $parameter->getNode();
69
            $attributes = $this->attributes($node);
70
            $element = XMLAttributeHelper::forDOM($DOMDocument)->createElementWithAttributes('xsd:element', $attributes);
71
            $types[] = $element;
72
            $complexIfNeeded = $this->generateParameters($DOMDocument, $node, null, $soapVersion);
73
            if (!empty($complexIfNeeded)) {
74
                $types = array_merge($types, $complexIfNeeded);
75
            }
76
        }
77
78
        return $types;
79
    }
80
81
    /**
82
     * @param DOMDocument $DOMDocument
83
     * @param Node $node
84
     * @param DOMElement|null $sequenceElement
85
     * @param string $soapVersion
86
     * @return DOMElement[]
87
     */
88
    private function generateParameters(DOMDocument $DOMDocument, Node $node, DOMElement $sequenceElement = null, string $soapVersion): array
89
    {
90
        $result = [];
91 View Code Duplication
        if ($sequenceElement) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
92
            $attributes = $this->attributes($node);
93
            $elementPartElement = XMLAttributeHelper::forDOM($DOMDocument)->createElementWithAttributes('xsd:element', $attributes);
94
            $sequenceElement->appendChild($elementPartElement);
95
        }
96 View Code Duplication
        if ($node->isArray()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
            $complexTypeElement = XMLAttributeHelper::forDOM($DOMDocument)
98
                ->createElementWithAttributes('xsd:complexType', ['name' => $node->getNameForArray()]);
99
            $complexContentElement = XMLAttributeHelper::forDOM($DOMDocument)->createElement('xsd:complexContent');
100
            $restrictionElement = XMLAttributeHelper::forDOM($DOMDocument)
101
                ->createElementWithAttributes('xsd:restriction', ['base' => 'soapenc:Array']);
102
            $type = $node->isObject() ? 'ns:' . $node->getNameForObject() . '[]' : 'xsd:' . $node->getType() . '[]';
103
            $attributeElement = XMLAttributeHelper::forDOM($DOMDocument)
104
                ->createElementWithAttributes('xsd:attribute', [
105
                    'ref' => 'soapenc:arrayType',
106
                    $soapVersion . ':arrayType' => $type
107
                ]);
108
            $restrictionElement->appendChild($attributeElement);
109
            $complexContentElement->appendChild($restrictionElement);
110
            $complexTypeElement->appendChild($complexContentElement);
111
            $result[] = $complexTypeElement;
112
        }
113
        if ($node->isObject()) {
114
            $complexTypeElement = XMLAttributeHelper::forDOM($DOMDocument)
115
                ->createElementWithAttributes('xsd:complexType', ['name' => $node->getNameForObject()]);
116
            $sequenceElement = XMLAttributeHelper::forDOM($DOMDocument)->createElement('xsd:sequence');
117
            $complexTypeElement->appendChild($sequenceElement);
118
119
            $result[] = $complexTypeElement;
120 View Code Duplication
            foreach ($node->getElements() as $nodeElement) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
                $tmp = $this->generateParameters($DOMDocument, $nodeElement, $sequenceElement, $soapVersion);
122
                $result = array_merge($result, $tmp);
123
            }
124
        }
125
126
        return $result;
127
    }
128
129 View Code Duplication
    private function attributes(Node $node): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        if ($node->isArray()) {
132
            $attributes = ['name' => $node->getSanitizedName(), 'type' => 'ns:' . $node->getNameForArray()];
133
        } elseif ($node->isObject()) {
134
            $attributes = ['name' => $node->getSanitizedName(), 'type' => 'ns:' . $node->getNameForObject()];
135
        } else {
136
            $attributes = ['name' => $node->getSanitizedName(), 'type' => 'xsd:' . $node->getType()];
137
        }
138
139
        return $attributes;
140
    }
141
}
142