|
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 ApiMethodInterface $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
|
|
|
'requestParams' |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$paramsObject = null; |
|
105
|
|
|
if (method_exists($class, 'customParamsObject')) { |
|
106
|
|
|
try { |
|
107
|
|
|
$paramsObject = $this->getMapper()->map($rpc->getParams(), $class->customParamsObject()); |
|
108
|
|
|
} catch (JsonMapper_Exception $e) { |
|
109
|
|
|
throw new ParseErrorException('', 0, $e->getPrevious()); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* засетим в метод RpcRequest |
|
115
|
|
|
* @var ApiMethodAbstract $class |
|
116
|
|
|
*/ |
|
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
|
|
|
'responseParams' |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $res; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @return JsonSchemaValidator |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getValidator(): JsonSchemaValidator |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->validator; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return JsonRpcSchema |
|
144
|
|
|
*/ |
|
145
|
|
|
public function getRpcSchema(): JsonRpcSchema |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->rpcSchema; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return JsonMapper |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getMapper(): JsonMapper |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->mapper; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|