Completed
Pull Request — master (#2)
by Sergey
03:47
created

RpcService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 94.87%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
eloc 36
c 2
b 0
f 0
dl 0
loc 132
ccs 37
cts 39
cp 0.9487
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A isBatch() 0 3 1
A getRpc() 0 8 2
A jsonParse() 0 24 3
A setBatch() 0 3 1
A getMapper() 0 3 1
A validateJsonRpc() 0 8 1
A getRpcSchema() 0 3 1
A getValidator() 0 3 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
    /** @var bool */
24
    private $batch = true;
25
26
    /** @var JsonSchemaValidator */
27
    private $validator;
28
29
    /** @var JsonRpcSchema */
30
    private $rpcSchema;
31
32
    /** @var JsonMapper */
33
    private $mapper;
34
35
    /**
36
     * RpcService constructor.
37
     *
38
     * @param JsonSchemaValidator $validator
39
     * @param JsonRpcSchema $rpcSchema
40
     * @param JsonMapper $mapper
41
     */
42 6
    public function __construct(
43
        JsonSchemaValidator $validator,
44
        JsonRpcSchema $rpcSchema,
45
        JsonMapper $mapper
46
    ) {
47 6
        $this->validator = $validator;
48 6
        $this->rpcSchema = $rpcSchema;
49 6
        $this->mapper = $mapper;
50 6
    }
51
52
    /**
53
     * @param string $json
54
     *
55
     * @return stdClass[]
56
     * @throws ParseErrorException
57
     */
58 6
    public function jsonParse(string $json): array
59
    {
60
        /**
61
         * @param string $json
62
         *
63
         * @return array
64
         */
65
        try {
66 6
            $data = json_decode(
67 6
                $json,
68 6
                false,
69 6
                512,
70 6
                JSON_THROW_ON_ERROR
71
            );
72
73 5
            if ($data instanceof stdClass) {
74 5
                $data = [$data];
75 5
                $this->setBatch(false);
76
            }
77 1
        } catch (Exception $e) {
78 1
            throw new ParseErrorException('', 0, $e->getPrevious());
79
        }
80
81 5
        return $data;
82
    }
83
84
    /**
85
     * Проверим RPC
86
     *
87
     * @param stdClass $data
88
     */
89 5
    private function validateJsonRpc(stdClass $data): void
90
    {
91
        $this
92 5
            ->getValidator()
93 5
            ->validate(
94 5
                $this->getRpcSchema()->get(),
95
                $data,
96 5
                'jsonRpc'
97
            );
98 4
    }
99
100
    /**
101
     * @param stdClass $data
102
     * @return RpcModel
103
     */
104 5
    public function getRpc(stdClass $data): RpcModel
105
    {
106 5
        $this->validateJsonRpc($data);
107
108
        try {
109 4
            return $this->getMapper()->map($data, new RpcModel());
110
        } catch (JsonMapper_Exception $e) {
111
            throw new ParseErrorException('', 0, $e->getPrevious());
112
        }
113
    }
114
115
    /**
116
     * @return bool
117
     */
118 5
    public function isBatch(): bool
119
    {
120 5
        return $this->batch;
121
    }
122
123
    /**
124
     * @param bool $batch
125
     */
126 5
    public function setBatch(bool $batch): void
127
    {
128 5
        $this->batch = $batch;
129 5
    }
130
131
    /**
132
     * @return JsonSchemaValidator
133
     */
134 5
    public function getValidator(): JsonSchemaValidator
135
    {
136 5
        return $this->validator;
137
    }
138
139
    /**
140
     * @return JsonRpcSchema
141
     */
142 5
    public function getRpcSchema(): JsonRpcSchema
143
    {
144 5
        return $this->rpcSchema;
145
    }
146
147
    /**
148
     * @return JsonMapper
149
     */
150 4
    public function getMapper(): JsonMapper
151
    {
152 4
        return $this->mapper;
153
    }
154
}
155