RpcService::getRpcSchema()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Onnov\JsonRpcServer\Service;
6
7
use JsonMapper;
8
use JsonMapper_Exception;
9
use Onnov\JsonRpcServer\Exception\ParseErrorException;
10
use Exception;
11
use Onnov\JsonRpcServer\Model\RpcModel;
12
use Onnov\JsonRpcServer\Validator\JsonRpcSchema;
13
use Onnov\JsonRpcServer\Validator\JsonSchemaValidator;
14
use stdClass;
15
16
/**
17
 * Class Rpc
18
 *
19
 * @package Onnov\JsonRpcServer
20
 */
21
class RpcService
22
{
23
    /**
24
     * @var bool
25
     */
26
    private $batch = true;
27
28
    /**
29
     * @var JsonSchemaValidator
30
     */
31
    private $validator;
32
33
    /**
34
     * @var JsonRpcSchema
35
     */
36
    private $rpcSchema;
37
38
    /**
39
     * @var JsonMapper
40
     */
41
    private $mapper;
42
43
    /**
44
     * RpcService constructor.
45
     *
46
     * @param JsonSchemaValidator $validator
47
     * @param JsonRpcSchema       $rpcSchema
48
     * @param JsonMapper          $mapper
49
     */
50 9
    public function __construct(
51
        JsonSchemaValidator $validator,
52
        JsonRpcSchema $rpcSchema,
53
        JsonMapper $mapper
54
    ) {
55 9
        $this->validator = $validator;
56 9
        $this->rpcSchema = $rpcSchema;
57 9
        $this->mapper = $mapper;
58
    }
59
60
    /**
61
     * @param string $json
62
     *
63
     * @return mixed[]
64
     * @throws ParseErrorException
65
     */
66 9
    public function jsonParse(string $json): array
67
    {
68
        /**
69
         * @param string $json
70
         *
71
         * @return array
72
         */
73
        try {
74 9
            $data = json_decode(
75 9
                $json,
76 9
                false,
77 9
                512,
78 9
                JSON_THROW_ON_ERROR
79 9
            );
80
81 8
            if ($data instanceof stdClass) {
82 5
                $data = [$data];
83 8
                $this->setBatch(false);
84
            }
85 1
        } catch (Exception $e) {
86 1
            throw new ParseErrorException('', 0, $e);
87
        }
88
89 8
        return $data;
90
    }
91
92
    /**
93
     * Проверим RPC
94
     *
95
     * @param stdClass $data
96
     */
97 8
    private function validateJsonRpc(stdClass $data): void
98
    {
99 8
        $this
100 8
            ->getValidator()
101 8
            ->validate(
102 8
                $this->getRpcSchema()->get(),
103 8
                $data,
104 8
                'jsonRpc'
105 8
            );
106
    }
107
108
    /**
109
     * @param  stdClass $data
110
     * @return RpcModel
111
     */
112 8
    public function getRpc(stdClass $data): RpcModel
113
    {
114 8
        $this->validateJsonRpc($data);
115
116
        try {
117 7
            return $this->getMapper()->map($data, new RpcModel());
118
        } catch (JsonMapper_Exception $e) {
119
            throw new ParseErrorException('', 0, $e);
120
        }
121
    }
122
123
    /**
124
     * @return bool
125
     */
126 8
    public function isBatch(): bool
127
    {
128 8
        return $this->batch;
129
    }
130
131
    /**
132
     * @param bool $batch
133
     */
134 5
    public function setBatch(bool $batch): void
135
    {
136 5
        $this->batch = $batch;
137
    }
138
139
    /**
140
     * @return JsonSchemaValidator
141
     */
142 8
    public function getValidator(): JsonSchemaValidator
143
    {
144 8
        return $this->validator;
145
    }
146
147
    /**
148
     * @return JsonRpcSchema
149
     */
150 8
    public function getRpcSchema(): JsonRpcSchema
151
    {
152 8
        return $this->rpcSchema;
153
    }
154
155
    /**
156
     * @return JsonMapper
157
     */
158 7
    public function getMapper(): JsonMapper
159
    {
160 7
        return $this->mapper;
161
    }
162
}
163