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\JsonRpcMethodParamsValidatorInterface; |
11
|
|
|
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodResolverInterface; |
12
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; |
13
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class JsonRpcRequestHandler |
17
|
|
|
*/ |
18
|
|
|
class JsonRpcRequestHandler |
19
|
|
|
{ |
20
|
|
|
use JsonRpcServerDispatcherAwareTrait; |
21
|
|
|
|
22
|
|
|
/** @var JsonRpcMethodResolverInterface */ |
23
|
|
|
private $methodResolver; |
24
|
|
|
/** @var ResponseCreator */ |
25
|
|
|
private $responseCreator; |
26
|
|
|
/** @var JsonRpcMethodParamsValidatorInterface|null */ |
27
|
|
|
private $methodParamsValidator = null; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param JsonRpcMethodResolverInterface $methodResolver |
31
|
|
|
* @param ResponseCreator $responseCreator |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct(JsonRpcMethodResolverInterface $methodResolver, ResponseCreator $responseCreator) |
34
|
|
|
{ |
35
|
4 |
|
$this->methodResolver = $methodResolver; |
36
|
4 |
|
$this->responseCreator = $responseCreator; |
37
|
4 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param JsonRpcMethodParamsValidatorInterface $methodParamsValidator |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
1 |
|
public function setMethodParamsValidator(JsonRpcMethodParamsValidatorInterface $methodParamsValidator) : void |
45
|
|
|
{ |
46
|
1 |
|
$this->methodParamsValidator = $methodParamsValidator; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
51
|
|
|
* |
52
|
|
|
* @return JsonRpcResponse |
53
|
|
|
* |
54
|
|
|
* @throws JsonRpcInvalidParamsException |
55
|
|
|
* @throws JsonRpcMethodNotFoundException |
56
|
|
|
*/ |
57
|
4 |
|
public function processJsonRpcRequest(JsonRpcRequest $jsonRpcRequest) : JsonRpcResponse |
58
|
|
|
{ |
59
|
4 |
|
$method = $this->resolveMethod($jsonRpcRequest); |
60
|
|
|
|
61
|
3 |
|
$this->validateParamList($jsonRpcRequest, $method); |
62
|
|
|
|
63
|
|
|
try { |
64
|
2 |
|
$result = $method->apply($jsonRpcRequest->getParamList()); |
65
|
|
|
|
66
|
1 |
|
$event = new ActionEvent\OnMethodSuccessEvent($result, $method, $jsonRpcRequest); |
67
|
1 |
|
} catch (\Exception $exception) { |
68
|
1 |
|
$event = new ActionEvent\OnMethodFailureEvent($exception, $method, $jsonRpcRequest); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$this->dispatchJsonRpcEvent($event::EVENT_NAME, $event); |
72
|
|
|
|
73
|
2 |
|
return $this->createResponse($event); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
78
|
|
|
* |
79
|
|
|
* @return JsonRpcMethodInterface |
80
|
|
|
* |
81
|
|
|
* @throws JsonRpcMethodNotFoundException |
82
|
|
|
*/ |
83
|
4 |
|
private function resolveMethod(JsonRpcRequest $jsonRpcRequest) : JsonRpcMethodInterface |
84
|
|
|
{ |
85
|
4 |
|
$method = $this->methodResolver->resolve($jsonRpcRequest->getMethod()); |
86
|
|
|
|
87
|
4 |
|
if (!$method instanceof JsonRpcMethodInterface) { |
88
|
1 |
|
throw new JsonRpcMethodNotFoundException($jsonRpcRequest->getMethod()); |
89
|
|
|
} |
90
|
|
|
|
91
|
3 |
|
return $method; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
97
|
|
|
* @param JsonRpcMethodInterface $method |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
* |
101
|
|
|
* @throws JsonRpcInvalidParamsException |
102
|
|
|
*/ |
103
|
3 |
|
private function validateParamList(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method) : void |
104
|
|
|
{ |
105
|
3 |
|
if (null !== $this->methodParamsValidator) { |
106
|
1 |
|
$violationList = $this->methodParamsValidator->validate($jsonRpcRequest, $method); |
107
|
|
|
|
108
|
1 |
|
if (count($violationList)) { |
109
|
1 |
|
throw new JsonRpcInvalidParamsException($violationList); |
110
|
|
|
} |
111
|
|
|
} |
112
|
2 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ActionEvent\AbstractOnMethodEvent $event |
116
|
|
|
* |
117
|
|
|
* @return JsonRpcResponse |
118
|
|
|
*/ |
119
|
2 |
|
protected function createResponse(ActionEvent\AbstractOnMethodEvent $event) : JsonRpcResponse |
120
|
|
|
{ |
121
|
2 |
|
if ($event instanceof ActionEvent\OnMethodSuccessEvent) { |
122
|
1 |
|
return $this->responseCreator->createResultResponse($event->getResult(), $event->getJsonRpcRequest()); |
123
|
|
|
} else { |
124
|
|
|
/** @var $event ActionEvent\OnMethodFailureEvent */ |
125
|
1 |
|
return $this->responseCreator->createErrorResponse( |
126
|
1 |
|
$event->getException(), |
127
|
1 |
|
$event->getJsonRpcRequest() |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|