|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcServer\Infra\Endpoint; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServer\App\Creator\ResponseCreator; |
|
5
|
|
|
use Yoanm\JsonRpcServer\App\RequestHandler; |
|
6
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcException; |
|
7
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface; |
|
8
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInternalErrorException; |
|
9
|
|
|
use Yoanm\JsonRpcServer\Infra\RawObject\JsonRpcRawRequest; |
|
10
|
|
|
use Yoanm\JsonRpcServer\Infra\RawObject\JsonRpcRawResponse; |
|
11
|
|
|
use Yoanm\JsonRpcServer\Infra\Serialization\RawRequestSerializer; |
|
12
|
|
|
use Yoanm\JsonRpcServer\Infra\Serialization\RawResponseSerializer; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class JsonRpcEndpoint |
|
16
|
|
|
*/ |
|
17
|
|
|
class JsonRpcEndpoint |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var RawRequestSerializer */ |
|
20
|
|
|
private $rawRequestSerializer; |
|
21
|
|
|
/** @var RequestHandler */ |
|
22
|
|
|
private $requestHandler; |
|
23
|
|
|
/** @var ResponseCreator */ |
|
24
|
|
|
private $responseCreator; |
|
25
|
|
|
/** @var RawResponseSerializer */ |
|
26
|
|
|
private $rawResponseNormalizer; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param RawRequestSerializer $rawRequestSerializer |
|
30
|
|
|
* @param RequestHandler $requestHandler |
|
31
|
|
|
* @param RawResponseSerializer $rawResponseNormalizer |
|
32
|
|
|
* @param ResponseCreator $responseCreator |
|
33
|
|
|
*/ |
|
34
|
7 |
|
public function __construct( |
|
35
|
|
|
RawRequestSerializer $rawRequestSerializer, |
|
36
|
|
|
RequestHandler $requestHandler, |
|
37
|
|
|
RawResponseSerializer $rawResponseNormalizer, |
|
38
|
|
|
ResponseCreator $responseCreator |
|
39
|
|
|
) { |
|
40
|
7 |
|
$this->rawRequestSerializer = $rawRequestSerializer; |
|
41
|
7 |
|
$this->requestHandler = $requestHandler; |
|
42
|
7 |
|
$this->rawResponseNormalizer = $rawResponseNormalizer; |
|
43
|
7 |
|
$this->responseCreator = $responseCreator; |
|
44
|
7 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $request |
|
48
|
|
|
* |
|
49
|
|
|
* @return string The response |
|
50
|
|
|
*/ |
|
51
|
7 |
|
public function index(string $request) : string |
|
52
|
|
|
{ |
|
53
|
|
|
try { |
|
54
|
7 |
|
$rawResponse = $this->handleRawRequest( |
|
55
|
7 |
|
$this->rawRequestSerializer->deserialize($request) |
|
56
|
|
|
); |
|
57
|
2 |
|
} catch (JsonRpcExceptionInterface $jsonRpcException) { |
|
58
|
|
|
// Try to create a valid json-rpc error |
|
59
|
1 |
|
$rawResponse = $this->createRawResponseFromException($jsonRpcException); |
|
60
|
1 |
|
} catch (\Exception $exception) { |
|
61
|
|
|
// Try to create a valid json-rpc error anyway |
|
62
|
1 |
|
$rawResponse = $this->createRawResponseFromException( |
|
63
|
1 |
|
new JsonRpcInternalErrorException($exception) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
7 |
|
return $this->rawResponseNormalizer->serialize($rawResponse); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param JsonRpcRawRequest $rawRequest |
|
72
|
|
|
* |
|
73
|
|
|
* @return JsonRpcRawResponse |
|
74
|
|
|
*/ |
|
75
|
7 |
|
private function handleRawRequest(JsonRpcRawRequest $rawRequest) : JsonRpcRawResponse |
|
76
|
|
|
{ |
|
77
|
7 |
|
$rawResponse = new JsonRpcRawResponse($rawRequest->isBatch()); |
|
78
|
|
|
|
|
79
|
7 |
|
foreach ($rawRequest->getItemtList() as $item) { |
|
80
|
7 |
|
if ($item instanceof \Exception) { |
|
81
|
3 |
|
$response = $this->responseCreator->createErrorResponse($item); |
|
82
|
|
|
} else { |
|
83
|
5 |
|
$response = $this->requestHandler->handle($item); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
5 |
|
$rawResponse->addResponse($response); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
return $rawResponse; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param JsonRpcException $exception |
|
94
|
|
|
* |
|
95
|
|
|
* @return JsonRpcRawResponse |
|
96
|
|
|
*/ |
|
97
|
2 |
|
private function createRawResponseFromException(JsonRpcException $exception) : JsonRpcRawResponse |
|
98
|
|
|
{ |
|
99
|
2 |
|
return (new JsonRpcRawResponse())->addResponse( |
|
100
|
2 |
|
$this->responseCreator->createErrorResponse($exception) |
|
101
|
|
|
); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|