Completed
Push — master ( 40086d...cb5320 )
by Piotr
03:21
created

XMLRpcStyle::generateBinding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 7
loc 7
rs 9.4285
ccs 5
cts 5
cp 1
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2013-2016
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
 * XMLRpcStyle
33
 *
34
 * @author Piotr Olaszewski <[email protected]>
35
 */
36 View Code Duplication
class XMLRpcStyle implements XMLStyle
0 ignored issues
show
Duplication introduced by
This class 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...
37
{
38
    /**
39
     * @inheritdoc
40
     */
41 1
    public function generateBinding(DOMDocument $DOMDocument, $soapVersion)
42
    {
43 1
        return XMLAttributeHelper::forDOM($DOMDocument)
44 1
            ->createElementWithAttributes($soapVersion . ':binding', [
45 1
                'style' => 'rpc', 'transport' => 'http://schemas.xmlsoap.org/soap/http'
46 1
            ]);
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 1
    public function generateMessagePart(DOMDocument $DOMDocument, $nodes)
53
    {
54 1
        $parts = [];
55 1
        foreach ($nodes as $node) {
56 1
            $attributes = $this->attributes($node);
57 1
            $parts[] = XMLAttributeHelper::forDOM($DOMDocument)->createElementWithAttributes('part', $attributes);
58 1
        }
59 1
        return $parts;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65 5
    public function generateTypes(DOMDocument $DOMDocument, $parameters, $soapVersion)
66
    {
67 5
        $types = [];
68 5
        foreach ($parameters as $parameter) {
69 5
            $node = $parameter->getNode();
70 5
            $nodeGen = $this->generateParameters($DOMDocument, $node, null, $soapVersion);
71 5
            $types = array_merge($types, $nodeGen);
72 5
        }
73 5
        return $types;
74
    }
75
76
    /**
77
     * @param DOMDocument $DOMDocument
78
     * @param Node $node
79
     * @param DOMElement|null $sequenceElement
80
     * @param string $soapVersion
81
     * @return DOMElement[]
82
     */
83 5
    private function generateParameters(DOMDocument $DOMDocument, Node $node, DOMElement $sequenceElement = null, $soapVersion)
84
    {
85 5
        $result = [];
86 5
        if ($sequenceElement) {
87 4
            $attributes = $this->attributes($node);
88 4
            $elementPartElement = XMLAttributeHelper::forDOM($DOMDocument)->createElementWithAttributes('xsd:element', $attributes);
89 4
            $sequenceElement->appendChild($elementPartElement);
90 4
        }
91 5
        if ($node->isArray()) {
92 3
            $complexTypeElement = XMLAttributeHelper::forDOM($DOMDocument)
93 3
                ->createElementWithAttributes('xsd:complexType', ['name' => $node->getNameForArray()]);
94 3
            $complexContentElement = XMLAttributeHelper::forDOM($DOMDocument)->createElement('xsd:complexContent');
95 3
            $restrictionElement = XMLAttributeHelper::forDOM($DOMDocument)
96 3
                ->createElementWithAttributes('xsd:restriction', ['base' => 'soapenc:Array']);
97 3
            $type = $node->isObject() ? 'ns:' . $node->getNameForObject() . '[]' : 'xsd:' . $node->getType() . '[]';
98 3
            $attributeElement = XMLAttributeHelper::forDOM($DOMDocument)
99 3
                ->createElementWithAttributes('xsd:attribute', [
100 3
                    'ref' => 'soapenc:arrayType',
101 3
                    $soapVersion . ':arrayType' => $type
102 3
                ]);
103 3
            $restrictionElement->appendChild($attributeElement);
104 3
            $complexContentElement->appendChild($restrictionElement);
105 3
            $complexTypeElement->appendChild($complexContentElement);
106 3
            $result[] = $complexTypeElement;
107 3
        }
108 5
        if ($node->isObject()) {
109 4
            $name = $node->getNameForObject();
110 4
            $element = XMLAttributeHelper::forDOM($DOMDocument)->createElementWithAttributes('xsd:element', [
111 4
                'name' => $name, 'nillable' => 'true', 'type' => 'ns:' . $name
112 4
            ]);
113 4
            $result[] = $element;
114
115 4
            $complexTypeElement = XMLAttributeHelper::forDOM($DOMDocument)
116 4
                ->createElementWithAttributes('xsd:complexType', ['name' => $node->getNameForObject()]);
117 4
            $sequenceElement = XMLAttributeHelper::forDOM($DOMDocument)->createElement('xsd:sequence');
118 4
            $complexTypeElement->appendChild($sequenceElement);
119
120 4
            $result[] = $complexTypeElement;
121 4
            foreach ($node->getElements() as $nodeElement) {
122 4
                $tmp = $this->generateParameters($DOMDocument, $nodeElement, $sequenceElement, $soapVersion);
123 4
                $result = array_merge($result, $tmp);
124 4
            }
125 4
        }
126 5
        return $result;
127
    }
128
129
    /**
130
     * @param Node $node
131
     * @return array
132
     */
133 5
    private function attributes(Node $node)
134
    {
135 5
        if ($node->isArray()) {
136 2
            $attributes = ['name' => $node->getSanitizedName(), 'type' => 'ns:' . $node->getNameForArray()];
137 5
        } elseif ($node->isObject()) {
138 2
            $attributes = ['name' => $node->getSanitizedName(), 'element' => 'ns:' . $node->getNameForObject()];
139 2
        } else {
140 4
            $attributes = ['name' => $node->getSanitizedName(), 'type' => 'xsd:' . $node->getType()];
141
        }
142 5
        return $attributes;
143
    }
144
}
145