Completed
Push — master ( 8e8a04...31fb68 )
by Marco
38:43 queued 23:45
created

XmlProcessor::encode()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 1
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
    public function __construct($encoding, LoggerInterface $logger) {
20
21
        parent::__construct($encoding, $logger);
22
23
        $this->encoder = new XmlrpcEncoder();
24
25
        $this->decoder = new XmlrpcDecoder();
26
27
    }
28
29
    public function encode($requests) {
30
31
        $requests = array_values($requests);
32
33
        $this->isMulticall = sizeof($requests) > 1 ? true : false;
34
35
        try {
36
37
            $payload = $this->isMulticall ? $this->encodeMulticall($requests) : $this->encodeSingleCall($requests[0]);
38
39
        } catch (XmlrpcException $xe) {
40
41
            throw $xe;
42
43
        }
44
45
        return $payload;
46
47
    }
48
49
    public function decode($response) {
50
51
        try {
52
53
            $content = $this->decoder->decodeResponse($response);
54
55
            if ( $this->decoder->isFault() ) throw new RpcException($content['faultString'], $content['faultCode']);
56
57
        } catch (XmlrpcException $xe) {
58
59
            throw $xe;
60
61
        }
62
63
        return $this->isMulticall ? self::normalizeContent($content) : $content;
64
65
    }
66
67
    private function encodeSingleCall(RpcRequest $request) {
68
69
        $this->logger->notice("Performing a single XML call");
70
71
    	$this->logger->debug("Data dump before encoding", $request->toArray());
72
73
        try {
74
75
        	// encoding
76
77
        	foreach ( $request->getSpecialTypes() as $key => $value ) {
78
79
                $this->encoder->setValueType($key, $value);
80
81
            }
82
83
            $encoded_request = $this->encoder->encodeCall($request->getMethod(), $request->getParameters());
84
85
        } catch (XmlrpcException $xe) {
86
87
            throw $xe;
88
89
        }
90
91
        $this->logger->debug("Data dump after encoding: ".$encoded_request);
92
93
        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
    private function encodeMulticall($requests) {
110
111
        $composed_requests = array();
112
113
    	$this->logger->notice("Performing an XML multicall");
114
115
    	$this->logger->debug("Data dump before encoding", $requests);
116
117
        foreach ($requests as $request) {
118
119
            $composed_requests[] = array($request->getMethod(), $request->getParameters());
120
121
            foreach ( $request->getSpecialTypes() as $key => $value ) {
122
123
                $this->encoder->setValueType($key, $value);
124
125
            }
126
127
        }
128
129
        try {
130
131
            $encoded_requests = $this->encoder->setEncoding($this->getEncoding())->encodeMulticall($composed_requests);
132
133
        } catch (XmlrpcException $xe) {
134
135
            throw $xe;
136
137
        }
138
139
        return $encoded_requests;
140
141
    }
142
143
    private static function normalizeContent($content) {
144
145
        return array_map(function($value) {
146
147
            if (
148
                is_array($value) &&
149
                sizeof($value) == 1 &&
150
                isset($value[0])
151
            ) {
152
                return $value[0];
153
            }
154
155
            return $value;
156
157
        }, $content);
158
159
    }
160
161
}
162