Completed
Push — master ( 2b2467...86fe2f )
by Marco
12:06
created

RpcClient   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 12
Bugs 4 Features 1
Metric Value
wmc 15
c 12
b 4
f 1
lcom 1
cbo 10
dl 0
loc 131
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 4
A logger() 0 5 1
A sender() 0 5 1
A getTransport() 0 5 1
C send() 0 51 8
1
<?php namespace Comodojo\RpcClient;
2
3
use \Comodojo\RpcClient\Processor\JsonProcessor;
4
use \Comodojo\RpcClient\Processor\XmlProcessor;
5
use \Comodojo\RpcClient\Transport\Sender;
6
use \Comodojo\RpcClient\Components\Protocol;
7
use \Comodojo\RpcClient\Components\Encryption;
8
use \Comodojo\RpcClient\Components\Encoding;
9
use \Comodojo\RpcClient\Components\Request;
10
use \Comodojo\RpcClient\Utils\NullLogger;
11
use \Comodojo\Exception\RpcException;
12
use \Comodojo\Exception\HttpException;
13
use \Comodojo\Exception\XmlrpcException;
14
use \Exception;
15
16
/**
17
 * Comodojo RPC client. It's able to talk in XML and JSON (2.0).
18
 *
19
 * It optionally supports a not standard encrypted transport
20
 *
21
 * @package     Comodojo Spare Parts
22
 * @author      Marco Giovinazzi <[email protected]>
23
 * @license     MIT
24
 *
25
 * LICENSE:
26
 *
27
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
 * THE SOFTWARE.
34
 */
35
36
class RpcClient {
37
38
    use Protocol;
39
    use Encryption;
40
    use Encoding;
41
    use Request;
42
43
    const JSONRPC = "JSON";
44
45
    const XMLRPC = "XML";
46
47
    // internals
48
49
    private $sender;
50
51
    private $logger;
52
53
    /**
54
     * Class constructor
55
     *
56
     * @param   string  $server  Remote RPC server address
57
     *
58
     * @throws \Comodojo\Exception\HttpException
59
     */
60
    public function __construct($server, Logger $logger = null) {
61
62
        if ( empty($server) ) throw new Exception("Invalid RPC server address");
63
64
        $this->logger = is_null($logger) ? new NullLogger() : $logger;
65
66
        try {
67
68
            $this->sender = new Sender($server, $this->logger);
69
70
        } catch (HttpException $he) {
71
72
            throw $he;
73
74
        }
75
76
    }
77
78
    public function logger() {
79
80
        return $this->logger;
81
82
    }
83
84
    public function sender() {
85
86
        return $this->sender;
87
88
    }
89
90
    /**
91
     * Get the transport layer
92
     *
93
     * This method will return the Httprequest object in order to customize transport
94
     * options before sending request(s)
95
     *
96
     * @return  \Comodojo\Httprequest\Httprequest
97
     */
98
    final public function getTransport() {
99
100
        return $this->sender->transport();
101
102
    }
103
104
    /**
105
     * Send request(s) to server
106
     *
107
     * @return mixed
108
     *
109
     * @throws \Comodojo\Exception\RpcException
110
     * @throws \Comodojo\Exception\HttpException
111
     * @throws \Comodojo\Exception\XmlrpcException
112
     * @throws \Exception
113
     */
114
    public function send() {
115
116
        $requests = $this->getRequest();
117
118
        if ( empty($requests) ) throw new Exception("No request to send");
119
120
        if ( $this->getProtocol() == self::XMLRPC ) {
121
122
            $processor = new XmlProcessor($this->getEncoding(), $this->logger());
123
124
            $content_type = "text/xml";
125
126
        } else {
127
128
            $processor = new JsonProcessor($this->getEncoding(), $this->logger());
129
130
            $content_type = "application/json";
131
132
        }
133
134
        try {
135
136
            $payload = $processor->encode($requests);
137
138
            $response = $this->sender()->setContentType($content_type)->performCall($payload, $this->getEncryption());
0 ignored issues
show
Bug introduced by
The method setContentType() does not seem to exist on object<Comodojo\RpcClient\Transport\Sender>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
139
140
            $result = $processor->decode($response);
141
142
        } catch (HttpException $he) {
143
144
            throw $he;
145
146
        } catch (RpcException $re) {
147
148
            throw $re;
149
150
        } catch (XmlrpcException $xe) {
151
152
            throw $xe;
153
154
        } catch (Exception $e) {
155
156
            throw $e;
157
158
        }
159
160
        if ( $this->autoclean ) $this->clean();
161
162
        return $result;
163
164
    }
165
166
}
167