|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcServer\App\Handler; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServer\App\Creator\ResponseCreator; |
|
5
|
|
|
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait; |
|
6
|
|
|
use Yoanm\JsonRpcServer\Domain\Event\Action as ActionEvent; |
|
7
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException; |
|
8
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException; |
|
9
|
|
|
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface; |
|
10
|
|
|
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodResolverInterface; |
|
11
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; |
|
12
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class JsonRpcRequestHandler |
|
16
|
|
|
*/ |
|
17
|
|
|
class JsonRpcRequestHandler |
|
18
|
|
|
{ |
|
19
|
|
|
use JsonRpcServerDispatcherAwareTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** @var JsonRpcMethodResolverInterface */ |
|
22
|
|
|
private $methodResolver; |
|
23
|
|
|
/** @var ResponseCreator */ |
|
24
|
|
|
private $responseCreator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param JsonRpcMethodResolverInterface $methodResolver |
|
28
|
|
|
* @param ResponseCreator $responseCreator |
|
29
|
|
|
*/ |
|
30
|
4 |
|
public function __construct(JsonRpcMethodResolverInterface $methodResolver, ResponseCreator $responseCreator) |
|
31
|
|
|
{ |
|
32
|
4 |
|
$this->methodResolver = $methodResolver; |
|
33
|
4 |
|
$this->responseCreator = $responseCreator; |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param JsonRpcRequest $item |
|
38
|
|
|
* |
|
39
|
|
|
* @return JsonRpcResponse |
|
40
|
|
|
* |
|
41
|
|
|
* @throws JsonRpcInvalidParamsException |
|
42
|
|
|
* @throws JsonRpcMethodNotFoundException |
|
43
|
|
|
*/ |
|
44
|
4 |
|
public function processJsonRpcRequest(JsonRpcRequest $jsonRpcRequest) : JsonRpcResponse |
|
45
|
|
|
{ |
|
46
|
4 |
|
$method = $this->resolveMethod($jsonRpcRequest); |
|
47
|
|
|
|
|
48
|
3 |
|
$this->validateParamList($jsonRpcRequest, $method); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
2 |
|
$result = $method->apply($jsonRpcRequest->getParamList()); |
|
52
|
1 |
|
$event = new ActionEvent\OnMethodSuccessEvent($result, $method, $jsonRpcRequest); |
|
53
|
1 |
|
} catch (\Exception $exception) { |
|
54
|
1 |
|
$event = new ActionEvent\OnMethodFailureEvent($exception, $method, $jsonRpcRequest); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
$this->dispatchJsonRpcEvent($event::EVENT_NAME, $event); |
|
58
|
|
|
|
|
59
|
2 |
|
if ($event instanceof ActionEvent\OnMethodSuccessEvent) { |
|
60
|
1 |
|
$response = $this->responseCreator->createResultResponse($event->getResult(), $event->getJsonRpcRequest()); |
|
61
|
|
|
} else { |
|
62
|
1 |
|
$response = $this->responseCreator->createErrorResponse( |
|
63
|
1 |
|
$event->getException(), |
|
64
|
1 |
|
$event->getJsonRpcRequest() |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $response; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
|
73
|
|
|
* |
|
74
|
|
|
* @return JsonRpcMethodInterface |
|
75
|
|
|
* |
|
76
|
|
|
* @throws JsonRpcMethodNotFoundException |
|
77
|
|
|
*/ |
|
78
|
4 |
|
private function resolveMethod(JsonRpcRequest $jsonRpcRequest) : JsonRpcMethodInterface |
|
79
|
|
|
{ |
|
80
|
4 |
|
$method = $this->methodResolver->resolve($jsonRpcRequest->getMethod()); |
|
81
|
|
|
|
|
82
|
4 |
|
if (!$method instanceof JsonRpcMethodInterface) { |
|
83
|
1 |
|
throw new JsonRpcMethodNotFoundException($jsonRpcRequest->getMethod()); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
return $method; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
|
91
|
|
|
* @param JsonRpcMethodInterface $method |
|
92
|
|
|
* |
|
93
|
|
|
* @throws JsonRpcInvalidParamsException |
|
94
|
|
|
*/ |
|
95
|
3 |
|
private function validateParamList(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method) |
|
96
|
|
|
{ |
|
97
|
3 |
|
$event = new ActionEvent\ValidateParamsEvent($method, $jsonRpcRequest->getParamList() ?? []); |
|
98
|
|
|
|
|
99
|
3 |
|
$this->dispatchJsonRpcEvent($event::EVENT_NAME, $event); |
|
100
|
|
|
|
|
101
|
3 |
|
if (count($event->getViolationList())) { |
|
102
|
1 |
|
throw new JsonRpcInvalidParamsException($event->getViolationList()); |
|
103
|
|
|
} |
|
104
|
2 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|