1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Created by PhpStorm. |
5
|
|
|
* Project: json_rpc_server |
6
|
|
|
* User: sv |
7
|
|
|
* Date: 25.06.19 |
8
|
|
|
* Time: 7:54 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Onnov\JsonRpcServer\Service; |
14
|
|
|
|
15
|
|
|
use JsonMapper; |
16
|
|
|
use JsonMapper_Exception; |
17
|
|
|
use Onnov\JsonRpcServer\ApiMethodAbstract; |
18
|
|
|
use Onnov\JsonRpcServer\ApiMethodInterface; |
19
|
|
|
use Onnov\JsonRpcServer\Exception\InternalErrorException; |
20
|
|
|
use Onnov\JsonRpcServer\Exception\MethodNotFoundException; |
21
|
|
|
use Onnov\JsonRpcServer\Exception\ParseErrorException; |
22
|
|
|
use Onnov\JsonRpcServer\Model\RpcModel; |
23
|
|
|
use Onnov\JsonRpcServer\Model\RpcRequest; |
24
|
|
|
use Onnov\JsonRpcServer\Model\RunModel; |
25
|
|
|
use Onnov\JsonRpcServer\Traits\JsonHelperTrait; |
26
|
|
|
use Onnov\JsonRpcServer\Validator\JsonRpcSchema; |
27
|
|
|
use Onnov\JsonRpcServer\Validator\JsonSchemaValidator; |
28
|
|
|
|
29
|
|
|
class ApiExecService |
30
|
|
|
{ |
31
|
|
|
use JsonHelperTrait; |
32
|
|
|
|
33
|
|
|
/** @var JsonSchemaValidator */ |
34
|
|
|
protected $validator; |
35
|
|
|
|
36
|
|
|
/** @var JsonRpcSchema */ |
37
|
|
|
protected $rpcSchema; |
38
|
|
|
|
39
|
|
|
/** @var JsonMapper */ |
40
|
|
|
private $mapper; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* ApiExecService constructor. |
44
|
|
|
* |
45
|
|
|
* @param JsonSchemaValidator $validator |
46
|
|
|
* @param JsonRpcSchema $rpcSchema |
47
|
|
|
* @param JsonMapper $mapper |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
JsonSchemaValidator $validator, |
51
|
|
|
JsonRpcSchema $rpcSchema, |
52
|
|
|
JsonMapper $mapper |
53
|
|
|
) { |
54
|
|
|
$this->validator = $validator; |
55
|
|
|
$this->rpcSchema = $rpcSchema; |
56
|
|
|
$this->mapper = $mapper; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param RunModel $model |
61
|
|
|
* @param RpcModel $rpc |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
|
|
public function exe( |
66
|
|
|
RunModel $model, |
67
|
|
|
RpcModel $rpc |
68
|
|
|
) { |
69
|
|
|
$factory = $model->getApiFactory(); |
70
|
|
|
$method = $rpc->getMethod(); |
71
|
|
|
/** Проверим существование метода */ |
72
|
|
|
if ($factory->has($method) === false) { |
73
|
|
|
throw new MethodNotFoundException( |
74
|
|
|
'Method "' . $method . '" not found' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** Создаем экземпляр класса |
79
|
|
|
* |
80
|
|
|
* @var ApiMethodAbstract $class |
81
|
|
|
*/ |
82
|
|
|
$class = $factory->get($method); |
83
|
|
|
|
84
|
|
|
/** Проверим соответствие интерфейсу */ |
85
|
|
|
$interfaces = (array)class_implements($class); |
86
|
|
|
if ( |
87
|
|
|
(bool)$interfaces === false |
88
|
|
|
|| in_array(ApiMethodInterface::class, $interfaces, true) === false |
89
|
|
|
) { |
90
|
|
|
throw new InternalErrorException( |
91
|
|
|
'Method "' . $method . '" does not match Interface' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** Валидируем парамертры ЗАПРОСА */ |
96
|
|
|
if ($class->requestSchema() !== null) { |
97
|
|
|
$this->getValidator()->validate( |
98
|
|
|
$class->requestSchema(), |
99
|
|
|
$rpc->getParams() |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$paramsObject = null; |
104
|
|
|
if ($class->customParamsObject() !== null) { |
105
|
|
|
try { |
106
|
|
|
$paramsObject = $this->getMapper()->map($rpc->getParams(), $class->customParamsObject()); |
107
|
|
|
} catch (JsonMapper_Exception $e) { |
108
|
|
|
throw new ParseErrorException( |
109
|
|
|
$e->getMessage(), |
110
|
|
|
$e->getCode(), |
111
|
|
|
$e->getPrevious() |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** засетим в метод RpcRequest */ |
117
|
|
|
$class->setRpcRequest(new RpcRequest($rpc, $paramsObject)); |
118
|
|
|
|
119
|
|
|
/** Выполним метод */ |
120
|
|
|
$res = $class->execute()->getResult(); |
121
|
|
|
|
122
|
|
|
if ($model->isResponseCheck() && $class->responseSchema() !== null) { |
123
|
|
|
/** Валидируем парамертры ОТВЕТА */ |
124
|
|
|
$this->getValidator()->validate( |
125
|
|
|
$class->responseSchema(), |
126
|
|
|
$res |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return $res; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return JsonSchemaValidator |
135
|
|
|
*/ |
136
|
|
|
public function getValidator(): JsonSchemaValidator |
137
|
|
|
{ |
138
|
|
|
return $this->validator; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return JsonRpcSchema |
143
|
|
|
*/ |
144
|
|
|
public function getRpcSchema(): JsonRpcSchema |
145
|
|
|
{ |
146
|
|
|
return $this->rpcSchema; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return JsonMapper |
151
|
|
|
*/ |
152
|
|
|
public function getMapper(): JsonMapper |
153
|
|
|
{ |
154
|
|
|
return $this->mapper; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|