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