Test Failed
Pull Request — master (#2)
by Sergey
03:36
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
            );
101
    }
102
103
    /**
104
     * @param stdClass $data
105
     * @return RpcModel
106
     */
107
    public function getRpc(stdClass $data): RpcModel
108
    {
109
        $this->validateJsonRpc($data);
110
111
        try {
112
            return $this->getMapper()->map($data, new RpcModel());
113
        } catch (JsonMapper_Exception $e) {
114
            throw new ParseErrorException(
115
                $e->getMessage(),
116
                $e->getCode(),
117
                $e->getPrevious()
118
            );
119
        }
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function isBatch(): bool
126
    {
127
        return $this->batch;
128
    }
129
130
    /**
131
     * @param bool $batch
132
     */
133
    public function setBatch(bool $batch): void
134
    {
135
        $this->batch = $batch;
136
    }
137
138
    /**
139
     * @return JsonSchemaValidator
140
     */
141
    public function getValidator(): JsonSchemaValidator
142
    {
143
        return $this->validator;
144
    }
145
146
    /**
147
     * @return JsonRpcSchema
148
     */
149
    public function getRpcSchema(): JsonRpcSchema
150
    {
151
        return $this->rpcSchema;
152
    }
153
154
    /**
155
     * @return JsonMapper
156
     */
157
    public function getMapper(): JsonMapper
158
    {
159
        return $this->mapper;
160
    }
161
}
162