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

JsonProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 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 extends AbstractProcessor {
9
10
    private $ids = array();
11
12
    private $isMulticall = false;
13
14 12
    public function encode($requests) {
15
16 12
        $requests = array_values($requests);
17
18 12
        $this->isMulticall = sizeof($requests) > 1 ? true : false;
19
20 12
        $payload = array();
21
22 12
        foreach ($requests as $request) {
23
24 12
            list($composed, $rid) = self::composeJsonRequest($request);
25
26 12
            $payload[] = $composed;
27
28 12
            if ( $rid !== null ) $this->ids[] = $rid;
29
30 12
        }
31
32 12
        return ( sizeof($payload > 1) ) ? json_encode($payload) : json_encode($payload[0]);
33
34
    }
35
36 12
    public function decode($response) {
37
38
        try {
39
40 12
            if ( sizeof($this->ids) == 0 ) {
41
42 3
                return true;
43
44
            }
45
46 9
            $content = json_decode($response, true);
47
48 9
            if ( is_null($content) ) throw new Exception("Incomprehensible or empty response");
49
50 9
            if ( $this->isMulticall === false ) {
51
52 6
                $content = $content[0];
53
54 6
                if ( $content["id"] != $this->ids[0] ) throw new Exception("Invalid response ID received");
55
56 6
                $return = $content["result"];
57
58 6
            } else {
59
60 3
                $batch_content = array();
61
62 3
                foreach ( $this->ids as $key => $id ) {
63
64 3
                    if ( !isset($content[$key]) ) $batch_content[$key] = array("error" => array("code" => null, "message" => "Empty response"));
65
66 3
                    else if ( isset($content[$key]["error"]) ) $batch_content[$key] = array("error" => $content["error"]);
67
68 3
                    else if ( !isset($content[$key]["id"]) ) $batch_content[$key] = array("error" => array("code" => null, "message" => "Malformed response received"));
69
70 3
                    else if ( $content[$key]["id"] != $id ) $batch_content[$key] = array("error" => array("code" => null, "message" => "Invalid response ID received"));
71
72 3
                    else $batch_content[$key] = array("result" => $content[$key]["result"]);
73
74 3
                }
75
76 3
                $return = $batch_content;
77
78
            }
79
80 9
        } catch (Exception $xe) {
81
82
            throw $xe;
83
84
        }
85
86 9
        return $return;
87
88
    }
89
90 12
    private static function composeJsonRequest(RpcRequest $request) {
91
92
        $return = array(
93 12
            "jsonrpc"   =>  "2.0",
94 12
            "method"    =>  $request->getMethod(),
95 12
            "params"    =>  $request->getParameters()
96 12
        );
97
98 12
        $rid = $request->getId();
99
100 12
        if ( $rid === true ) {
101
102 9
            $id = $return["id"] = $request->getUniqueId();
103
104 12
        } else if ( is_scalar($rid) ) {
105
106
            $id = $return["id"] = $rid;
107
108
        } else {
109
110 3
            $id = null;
111
112
        }
113
114 12
        return array($return, $id);
115
116
    }
117
118
}
119