Test Failed
Pull Request — master (#2)
by Sergey
04:44 queued 12s
created

RpcService::isBatch()   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 0
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('', 0, $e->getPrevious());
79
        }
80
81
        return $data;
82
    }
83
84
    /**
85
     * Проверим RPC
86
     *
87
     * @param stdClass $data
88
     */
89
    private function validateJsonRpc(stdClass $data): void
90
    {
91
        $this
92
            ->getValidator()
93
            ->validate(
94
                $this->getRpcSchema()->get(),
95
                $data,
96
                'jsonRpc'
97
            );
98
    }
99
100
    /**
101
     * @param stdClass $data
102
     * @return RpcModel
103
     */
104
    public function getRpc(stdClass $data): RpcModel
105
    {
106
        $this->validateJsonRpc($data);
107
108
        try {
109
            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
    public function isBatch(): bool
119
    {
120
        return $this->batch;
121
    }
122
123
    /**
124
     * @param bool $batch
125
     */
126
    public function setBatch(bool $batch): void
127
    {
128
        $this->batch = $batch;
129
    }
130
131
    /**
132
     * @return JsonSchemaValidator
133
     */
134
    public function getValidator(): JsonSchemaValidator
135
    {
136
        return $this->validator;
137
    }
138
139
    /**
140
     * @return JsonRpcSchema
141
     */
142
    public function getRpcSchema(): JsonRpcSchema
143
    {
144
        return $this->rpcSchema;
145
    }
146
147
    /**
148
     * @return JsonMapper
149
     */
150
    public function getMapper(): JsonMapper
151
    {
152
        return $this->mapper;
153
    }
154
}
155