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
|
1 |
|
public function setMethodParamsValidator(JsonRpcMethodParamsValidatorInterface $methodParamsValidator) |
43
|
|
|
{ |
44
|
1 |
|
$this->methodParamsValidator = $methodParamsValidator; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
49
|
|
|
* |
50
|
|
|
* @return JsonRpcResponse |
51
|
|
|
* |
52
|
|
|
* @throws JsonRpcInvalidParamsException |
53
|
|
|
* @throws JsonRpcMethodNotFoundException |
54
|
|
|
*/ |
55
|
4 |
|
public function processJsonRpcRequest(JsonRpcRequest $jsonRpcRequest) : JsonRpcResponse |
56
|
|
|
{ |
57
|
4 |
|
$method = $this->resolveMethod($jsonRpcRequest); |
58
|
|
|
|
59
|
3 |
|
$this->validateParamList($jsonRpcRequest, $method); |
60
|
|
|
|
61
|
|
|
try { |
62
|
2 |
|
$result = $method->apply($jsonRpcRequest->getParamList()); |
63
|
|
|
|
64
|
1 |
|
$event = new ActionEvent\OnMethodSuccessEvent($result, $method, $jsonRpcRequest); |
65
|
1 |
|
} catch (\Exception $exception) { |
66
|
1 |
|
$event = new ActionEvent\OnMethodFailureEvent($exception, $method, $jsonRpcRequest); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$this->dispatchJsonRpcEvent($event::EVENT_NAME, $event); |
70
|
|
|
|
71
|
2 |
|
return $this->createResponse($event); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
76
|
|
|
* |
77
|
|
|
* @return JsonRpcMethodInterface |
78
|
|
|
* |
79
|
|
|
* @throws JsonRpcMethodNotFoundException |
80
|
|
|
*/ |
81
|
4 |
|
private function resolveMethod(JsonRpcRequest $jsonRpcRequest) : JsonRpcMethodInterface |
82
|
|
|
{ |
83
|
4 |
|
$method = $this->methodResolver->resolve($jsonRpcRequest->getMethod()); |
84
|
|
|
|
85
|
4 |
|
if (!$method instanceof JsonRpcMethodInterface) { |
86
|
1 |
|
throw new JsonRpcMethodNotFoundException($jsonRpcRequest->getMethod()); |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return $method; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @param JsonRpcRequest $jsonRpcRequest |
95
|
|
|
* @param JsonRpcMethodInterface $method |
96
|
|
|
* |
97
|
|
|
* @throws JsonRpcInvalidParamsException |
98
|
|
|
*/ |
99
|
3 |
|
private function validateParamList(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method) |
100
|
|
|
{ |
101
|
3 |
|
if (null !== $this->methodParamsValidator) { |
102
|
1 |
|
$violationList = $this->methodParamsValidator->validate($jsonRpcRequest, $method); |
103
|
|
|
|
104
|
1 |
|
if (count($violationList)) { |
105
|
1 |
|
throw new JsonRpcInvalidParamsException($violationList); |
106
|
|
|
} |
107
|
|
|
} |
108
|
2 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ActionEvent\AbstractOnMethodEvent $event |
112
|
|
|
* |
113
|
|
|
* @return JsonRpcResponse |
114
|
|
|
*/ |
115
|
2 |
|
protected function createResponse(ActionEvent\AbstractOnMethodEvent $event) : JsonRpcResponse |
116
|
|
|
{ |
117
|
2 |
|
if ($event instanceof ActionEvent\OnMethodSuccessEvent) { |
118
|
1 |
|
return $this->responseCreator->createResultResponse($event->getResult(), $event->getJsonRpcRequest()); |
119
|
|
|
} else { |
120
|
|
|
/** @var $event ActionEvent\OnMethodFailureEvent */ |
121
|
1 |
|
return $this->responseCreator->createErrorResponse( |
122
|
1 |
|
$event->getException(), |
123
|
1 |
|
$event->getJsonRpcRequest() |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|