Test Failed
Pull Request — master (#2)
by Sergey
03:54
created

RpcService::setBatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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
    public function __construct(
43
        JsonSchemaValidator $validator,
44
        JsonRpcSchema $rpcSchema,
45
        JsonMapper $mapper
46
    ) {
47
        $this->validator = $validator;
48
        $this->rpcSchema = $rpcSchema;
49
        $this->mapper = $mapper;
50
    }
51
52
    /**
53
     * @param string $json
54
     *
55
     * @return stdClass[]
56
     * @throws ParseErrorException
57
     */
58
    public function jsonParse(string $json): array
59
    {
60
        /**
61
         * @param string $json
62
         *
63
         * @return array
64
         */
65
        try {
66
            $data = json_decode(
67
                $json,
68
                false,
69
                512,
70
                JSON_THROW_ON_ERROR
71
            );
72
73
            if ($data instanceof stdClass) {
74
                $data = [$data];
75
                $this->setBatch(false);
76
            }
77
        } catch (Exception $e) {
78
            throw new ParseErrorException(
79
                $e->getMessage(),
80
                $e->getCode(),
81
                $e->getPrevious()
82
            );
83
        }
84
85
        return $data;
86
    }
87
88
    /**
89
     * Проверим RPC
90
     *
91
     * @param stdClass $data
92
     */
93
    private function validateJsonRpc(stdClass $data): void
94
    {
95
        $this
96
            ->getValidator()
97
            ->validate(
98
                $this->getRpcSchema()->get(),
99
                $data,
100
                'jsonRpc'
101
            );
102
    }
103
104
    /**
105
     * @param stdClass $data
106
     * @return RpcModel
107
     */
108
    public function getRpc(stdClass $data): RpcModel
109
    {
110
        $this->validateJsonRpc($data);
111
112
        try {
113
            return $this->getMapper()->map($data, new RpcModel());
114
        } catch (JsonMapper_Exception $e) {
115
            throw new ParseErrorException(
116
                $e->getMessage(),
117
                $e->getCode(),
118
                $e->getPrevious()
119
            );
120
        }
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function isBatch(): bool
127
    {
128
        return $this->batch;
129
    }
130
131
    /**
132
     * @param bool $batch
133
     */
134
    public function setBatch(bool $batch): void
135
    {
136
        $this->batch = $batch;
137
    }
138
139
    /**
140
     * @return JsonSchemaValidator
141
     */
142
    public function getValidator(): JsonSchemaValidator
143
    {
144
        return $this->validator;
145
    }
146
147
    /**
148
     * @return JsonRpcSchema
149
     */
150
    public function getRpcSchema(): JsonRpcSchema
151
    {
152
        return $this->rpcSchema;
153
    }
154
155
    /**
156
     * @return JsonMapper
157
     */
158
    public function getMapper(): JsonMapper
159
    {
160
        return $this->mapper;
161
    }
162
}
163