Completed
Branch 2.0 (7b6aa1)
by Marco
14:49
created

RpcClient::getTransport()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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