Completed
Push — 2.0 ( 7b6aa1...d47467 )
by Marco
35:53
created

RpcClient   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 204
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 78.33%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 24
lcom 1
cbo 12
dl 0
loc 204
ccs 47
cts 60
cp 0.7833
rs 10
c 3
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 27 5
A getLogger() 0 5 1
A getTransport() 0 5 1
A getRequest() 0 5 1
A setAutoclean() 0 7 1
A getAutoclean() 0 5 1
A addRequest() 0 7 1
A getPayload() 0 19 4
C send() 0 45 7
A getProcessor() 0 15 2
1
<?php namespace Comodojo\RpcClient;
2
3
use \Comodojo\RpcClient\Interfaces\Transport as TransportInterface;
4
use \Comodojo\RpcClient\Interfaces\Processor as ProcessorInterface;
5
use \Comodojo\RpcClient\Processor\JsonProcessor;
6
use \Comodojo\RpcClient\Processor\XmlProcessor;
7
use \Comodojo\RpcClient\Components\HttpTransport;
8
use \Comodojo\RpcClient\Components\Protocol;
9
use \Comodojo\RpcClient\Components\Encryption;
10
use \Comodojo\RpcClient\Components\Encoding;
11
use \Comodojo\RpcClient\Components\RequestManager;
12
use \Comodojo\Foundation\Logging\Manager as LogManager;
13
use \Psr\Log\LoggerInterface;
14
use \Comodojo\Exception\RpcException;
15
use \Comodojo\Exception\HttpException;
16
use \Comodojo\Exception\XmlrpcException;
17
use \Exception;
18
19
/**
20
 * Comodojo RPC client. It's able to talk in XML and JSON (2.0).
21
 *
22
 * It optionally supports a not standard encrypted transport
23
 *
24
 * @package     Comodojo Spare Parts
25
 * @author      Marco Giovinazzi <[email protected]>
26
 * @license     MIT
27
 *
28
 * LICENSE:
29
 *
30
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36
 * THE SOFTWARE.
37
 */
38
39
class RpcClient {
40
41
    use Protocol;
42
    use Encryption;
43
    use Encoding;
44
45
    const JSONRPC = "JSON";
46
47
    const XMLRPC = "XML";
48
49
    // internals
50
51
    /**
52
     * Autoclean requests
53
     *
54
     * @var string
55
     */
56
    private $autoclean = true;
57
58
    private $transport;
59
60
    private $logger;
61
62
    private $request;
63
64
    private $json_processor;
65
66
    private $xml_processor;
67
68
    /**
69
     * Class constructor
70
     *
71
     * @param   string  $server  Remote RPC server address
72
     *
73
     * @throws \Comodojo\Exception\HttpException
74
     */
75 51
    public function __construct(
76
        $server,
77
        LoggerInterface $logger = null,
78
        TransportInterface $transport = null
79
    ) {
80
81 51
        if ( empty($server) ) throw new Exception("Invalid RPC server address");
82
83 51
        $this->logger = is_null($logger) ? LogManager::create('rpcclient', false)->getLogger() : $logger;
84
85 51
        $this->request = new RequestManager();
86
87 51
        $this->json_processor = new JsonProcessor($this->getEncoding(), $this->getLogger());
88
89 51
        $this->xml_processor = new XmlProcessor($this->getEncoding(), $this->getLogger());
90
91
        try {
92
93 51
            $this->transport = empty($transport) ? new HttpTransport($server) : $transport;
94
95 51
        } catch (Exception $he) {
96
97
            throw $he;
98
99
        }
100
101 51
    }
102
103 51
    public function getLogger() {
104
105 51
        return $this->logger;
106
107
    }
108
109 51
    public function getTransport() {
110
111 51
        return $this->transport;
112
113
    }
114
115 51
    public function getRequest() {
116
117 51
        return $this->request;
118
119
    }
120
121
    /**
122
     * Set autoclean on/off
123
     *
124
     * @param   bool   $mode  If true, requests will be removed from queue at each send()
125
     *
126
     * @return  \Comodojo\RpcClient\RpcClient
127
     */
128
    public function setAutoclean($mode = true) {
129
130
        $this->autoclean = filter_var($mode, FILTER_VALIDATE_BOOLEAN);
131
132
        return $this;
133
134
    }
135
136 51
    public function getAutoclean() {
137
138 51
        return $this->autoclean;
139
140
    }
141
142 51
    public function addRequest(RpcRequest $request) {
143
144 51
        $this->request->add($request);
145
146 51
        return $this;
147
148
    }
149
150 51
    public function getPayload(ProcessorInterface $processor = null) {
151
152 51
        $requests = $this->getRequest()->get();
153
154 51
        if ( empty($requests) ) throw new Exception("No request to send");
155
156 51
        $processor = is_null($processor) ? $this->getProcessor() : $processor;
157
158
        try {
159
160 51
            return $processor->encode($requests);
161
162
        } catch (Exception $e) {
163
164
            throw $e;
165
166
        }
167
168
    }
169
170
    /**
171
     * Send request(s) to server
172
     *
173
     * @return mixed
174
     *
175
     * @throws \Comodojo\Exception\RpcException
176
     * @throws \Comodojo\Exception\HttpException
177
     * @throws \Comodojo\Exception\XmlrpcException
178
     * @throws \Exception
179
     */
180 51
    public function send() {
181
182 51
        $protocol = $this->getProtocol();
183
184 51
        $content_type = $protocol == self::XMLRPC ? "text/xml" : "application/json";
185
186 51
        $processor = $this->getProcessor();
187
188
        try {
189
190 51
            $payload = $this->getPayload($processor);
191
192 51
            $response = $this->getTransport()
193 51
                ->performCall(
194 51
                    $this->logger,
195 51
                    $payload,
196 51
                    $content_type,
197 51
                    $this->getEncryption()
198 51
                );
199
200 51
            $result = $processor->decode($response);
201
202 51
        } catch (HttpException $he) {
203
204
            throw $he;
205
206
        } catch (RpcException $re) {
207
208
            throw $re;
209
210
        } catch (XmlrpcException $xe) {
211
212
            throw $xe;
213
214
        } catch (Exception $e) {
215
216
            throw $e;
217
218
        }
219
220 51
        if ( $this->getAutoclean() ) $this->getRequest()->clean();
221
222 51
        return $result;
223
224
    }
225
226 51
    private function getProcessor() {
227
228 51
        if ( $this->getProtocol() == self::XMLRPC ) {
229
230 39
            $processor = $this->xml_processor;
231
232 39
        } else {
233
234 12
            $processor = $this->json_processor;
235
236
        }
237
238 51
        return $processor->setEncoding($this->getEncoding());
239
240
    }
241
242
}
243