Completed
Push — master ( 86fe2f...647445 )
by Marco
14:10
created

JsonProcessor::composeJsonRequest()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 13
nc 3
nop 1
1
<?php namespace Comodojo\RpcClient\Processor;
2
3
use \Psr\Log\LoggerInterface;
4
use \Comodojo\Exception\RpcException;
5
use \Comodojo\RpcClient\RpcRequest;
6
use \Exception;
7
8
class JsonProcessor implements ProcessorInterface {
9
10
    private $encoding;
11
12
    private $logger;
13
14
    private $encoder;
0 ignored issues
show
Unused Code introduced by
The property $encoder is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
    private $ids = array();
17
18
    private $isMulticall = false;
19
20
    public function __construct($encoding, LoggerInterface $logger) {
21
22
        $this->encoding = $encoding;
23
24
        $this->logger = $logger;
25
26
    }
27
28
    public function encode($requests) {
29
30
        $requests = array_values($requests);
31
32
        $this->isMulticall = sizeof($requests) > 1 ? true : false;
33
34
        $payload = array();
35
36
        foreach ($requests as $request) {
37
38
            list($composed, $rid) = self::composeJsonRequest($request);
39
40
            $payload[] = $composed;
41
42
            if ( $rid !== null ) $this->ids[] = $rid;
43
44
        }
45
46
        return ( sizeof($payload > 1) ) ? json_encode($payload) : json_encode($payload[0]);
47
48
    }
49
50
    public function decode($response) {
51
52
        try {
53
54
            if ( sizeof($this->ids) == 0 ) {
55
56
                return true;
57
58
            }
59
60
            $content = json_decode($response, true);
61
62
            if ( is_null($content) ) throw new Exception("Incomprehensible or empty response");
63
64
            if ( $this->isMulticall === false ) {
65
66
                $content = $content[0];
67
68
                if ( $content["id"] != $this->ids[0] ) throw new Exception("Invalid response ID received");
69
70
                $return = $content["result"];
71
72
            } else {
73
74
                $batch_content = array();
75
76
                foreach ( $this->ids as $key => $id ) {
77
78
                    if ( !isset($content[$key]) ) $batch_content[$key] = array("error" => array("code" => null, "message" => "Empty response"));
79
80
                    else if ( isset($content[$key]["error"]) ) $batch_content[$key] = array("error" => $content["error"]);
81
82
                    else if ( !isset($content[$key]["id"]) ) $batch_content[$key] = array("error" => array("code" => null, "message" => "Malformed response received"));
83
84
                    else if ( $content[$key]["id"] != $id ) $batch_content[$key] = array("error" => array("code" => null, "message" => "Invalid response ID received"));
85
86
                    else $batch_content[$key] = array("result" => $content[$key]["result"]);
87
88
                }
89
90
                $return = $batch_content;
91
92
            }
93
94
        } catch (Exception $xe) {
95
96
            throw $xe;
97
98
        }
99
100
        return $return;
101
102
    }
103
104
    private static function composeJsonRequest(RpcRequest $request) {
105
106
        $return = array(
107
            "jsonrpc"   =>  "2.0",
108
            "method"    =>  $request->getMethod(),
109
            "params"    =>  $request->getParameters()
110
        );
111
112
        $rid = $request->getId();
113
114
        if ( $rid === true ) {
115
116
            $id = $return["id"] = $request->getUniqueId();
117
118
        } else if ( is_scalar($rid) ) {
119
120
            $id = $return["id"] = $rid;
121
122
        } else {
123
124
            $id = null;
125
126
        }
127
128
        return array($return, $id);
129
130
    }
131
132
}
133