Completed
Branch 2.0 (7b6aa1)
by Marco
09:58
created

XmlProcessor   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.16%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 7
dl 0
loc 151
ccs 47
cts 51
cp 0.9216
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A encode() 0 19 4
A decode() 0 17 4
B encodeSingleCall() 0 29 3
B encodeMulticall() 0 33 4
A normalizeContent() 0 17 4
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
class XmlProcessor extends AbstractProcessor {
12
13
    private $encoder;
14
15
    private $decoder;
16
17
    private $isMulticall = false;
18
19 51
    public function __construct($encoding, LoggerInterface $logger) {
20
21 51
        parent::__construct($encoding, $logger);
22
23 51
        $this->encoder = new XmlrpcEncoder();
24
25 51
        $this->decoder = new XmlrpcDecoder();
26
27 51
    }
28
29 39
    public function encode($requests) {
30
31 39
        $requests = array_values($requests);
32
33 39
        $this->isMulticall = sizeof($requests) > 1 ? true : false;
34
35
        try {
36
37 39
            $payload = $this->isMulticall ? $this->encodeMulticall($requests) : $this->encodeSingleCall($requests[0]);
38
39 39
        } catch (XmlrpcException $xe) {
40
41
            throw $xe;
42
43
        }
44
45 39
        return $payload;
46
47
    }
48
49 39
    public function decode($response) {
50
51
        try {
52
53 39
            $content = $this->decoder->decodeResponse($response);
54
55 39
            if ( $this->decoder->isFault() ) throw new RpcException($content['faultString'], $content['faultCode']);
56
57 39
        } catch (XmlrpcException $xe) {
58
59
            throw $xe;
60
61
        }
62
63 39
        return $this->isMulticall ? self::normalizeContent($content) : $content;
64
65
    }
66
67 36
    private function encodeSingleCall(RpcRequest $request) {
68
69 36
        $this->logger->notice("Performing a single XML call");
70
71 36
    	$this->logger->debug("Data dump before encoding", $request->toArray());
72
73
        try {
74
75
        	// encoding
76
77 36
        	foreach ( $request->getSpecialTypes() as $key => $value ) {
78
79 6
                $this->encoder->setValueType($key, $value);
80
81 36
            }
82
83 36
            $encoded_request = $this->encoder->encodeCall($request->getMethod(), $request->getParameters());
84
85 36
        } catch (XmlrpcException $xe) {
86
87
            throw $xe;
88
89
        }
90
91 36
        $this->logger->debug("Data dump after encoding: ".$encoded_request);
92
93 36
        return $encoded_request;
94
95
    }
96
97
    /**
98
     * Perform an xml multicall
99
     *
100
     * @param   array   $requests
101
     *
102
     * @return  array
103
     *
104
     * @throws \Comodojo\Exception\RpcException
105
     * @throws \Comodojo\Exception\HttpException
106
     * @throws \Comodojo\Exception\XmlrpcException
107
     * @throws \Exception
108
     */
109 3
    private function encodeMulticall($requests) {
110
111 3
        $composed_requests = array();
112
113 3
    	$this->logger->notice("Performing an XML multicall");
114
115 3
    	$this->logger->debug("Data dump before encoding", $requests);
116
117 3
        foreach ($requests as $request) {
118
119 3
            $composed_requests[] = array($request->getMethod(), $request->getParameters());
120
121 3
            foreach ( $request->getSpecialTypes() as $key => $value ) {
122
123 3
                $this->encoder->setValueType($key, $value);
124
125 3
            }
126
127 3
        }
128
129
        try {
130
131 3
            $encoded_requests = $this->encoder->setEncoding($this->getEncoding())->encodeMulticall($composed_requests);
132
133 3
        } catch (XmlrpcException $xe) {
134
135
            throw $xe;
136
137
        }
138
139 3
        return $encoded_requests;
140
141
    }
142
143
    private static function normalizeContent($content) {
144
145 3
        return array_map(function($value) {
146
147
            if (
148 3
                is_array($value) &&
149 3
                sizeof($value) == 1 &&
150 3
                isset($value[0])
151 3
            ) {
152 3
                return $value[0];
153
            }
154
155 3
            return $value;
156
157 3
        }, $content);
158
159
    }
160
161
}
162