XmlProcessor::encodeSingleCall()   A
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.0067

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 10
cts 11
cp 0.9091
rs 9.472
c 0
b 0
f 0
cc 3
nc 7
nop 1
crap 3.0067
1
<?php namespace Comodojo\RpcClient\Processor;
2
3
use \Psr\Log\LoggerInterface;
4
use \Comodojo\RpcClient\RpcRequest;
5
use \Comodojo\Xmlrpc\XmlrpcEncoder;
6
use \Comodojo\Xmlrpc\XmlrpcDecoder;
7
use \Comodojo\Exception\RpcException;
8
use \Comodojo\Exception\XmlrpcException;
9
use \Exception;
10
11
/**
12
 * @package     Comodojo Spare Parts
13
 * @author      Marco Giovinazzi <[email protected]>
14
 * @license     MIT
15
 *
16
 * LICENSE:
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
class XmlProcessor extends AbstractProcessor {
28
29
    private $encoder;
30
31
    private $decoder;
32
33
    private $isMulticall = false;
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 63
    public function __construct($encoding, LoggerInterface $logger) {
39
40 63
        parent::__construct($encoding, $logger);
41
42 63
        $this->encoder = new XmlrpcEncoder();
43
44 63
        $this->decoder = new XmlrpcDecoder();
45
46 63
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 39
    public function encode(array $requests) {
52
53 39
        $requests = array_values($requests);
54
55 39
        $this->isMulticall = sizeof($requests) > 1 ? true : false;
56
57
        try {
58
59 39
            $payload = $this->isMulticall ? $this->encodeMulticall($requests) : $this->encodeSingleCall($requests[0]);
60
61 13
        } catch (XmlrpcException $xe) {
62
63
            throw $xe;
64
65
        }
66
67 39
        return $payload;
68
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 39
    public function decode($response) {
75
76
        try {
77
78 39
            $content = $this->decoder->decodeResponse($response);
79
80 39
            if ( $this->decoder->isFault() ) throw new RpcException($content['faultString'], $content['faultCode']);
81
82 13
        } catch (XmlrpcException $xe) {
83
84
            throw $xe;
85
86
        }
87
88 39
        return $this->isMulticall ? self::normalizeContent($content) : $content;
89
90
    }
91
92 36
    private function encodeSingleCall(RpcRequest $request) {
93
94 36
        $this->logger->debug("Performing a single XML call");
95
96 36
    	$this->logger->debug("Data dump before encoding", $request->toArray());
97
98
        try {
99
100
        	// encoding
101 36
            foreach ( $request->getSpecialTypes() as $key => $value ) {
102
103 6
                $this->encoder->setValueType($key, $value);
104
105 12
            }
106
107 36
            $encoded_request = $this->encoder->encodeCall($request->getMethod(), $request->getParameters());
108
109 12
        } catch (XmlrpcException $xe) {
110
111
            throw $xe;
112
113
        }
114
115 36
        $this->logger->debug("Data dump after encoding: ".$encoded_request);
116
117 36
        return $encoded_request;
118
119
    }
120
121
    /**
122
     * Perform an xml multicall
123
     *
124
     * @param array $requests
125
     * @return array
126
     * @throws XmlrpcException
127
     */
128 3
    private function encodeMulticall(array $requests) {
129
130 3
        $composed_requests = [];
131
132 3
    	$this->logger->debug("Performing an XML multicall");
133
134 3
    	$this->logger->debug("Data dump before encoding", $requests);
135
136 3
        foreach ($requests as $request) {
137
138 3
            $composed_requests[] = [
139 3
                $request->getMethod(),
140 3
                $request->getParameters()
141 1
            ];
142
143 3
            foreach ( $request->getSpecialTypes() as $key => $value ) {
144 3
                $this->encoder->setValueType($key, $value);
145 1
            }
146
147 1
        }
148
149
        try {
150
151 3
            $encoded_requests = $this->encoder
152 3
                ->setEncoding($this->getEncoding())
153 3
                ->encodeMulticall($composed_requests);
154
155 1
        } catch (XmlrpcException $xe) {
156
157
            throw $xe;
158
159
        }
160
161 3
        $this->logger->debug("Data dump after encoding: ".$encoded_requests);
162
163 3
        return $encoded_requests;
164
165
    }
166
167
    private static function normalizeContent($content) {
168
169 3
        return array_map(function($value) {
170
171
            if (
172 3
                is_array($value) &&
173 3
                sizeof($value) == 1 &&
174 3
                isset($value[0])
175 1
            ) {
176 3
                return $value[0];
177
            }
178
179 3
            return $value;
180
181 3
        }, $content);
182
183
    }
184
185
}
186