Completed
Branch release/0.1.0 (dcda87)
by Yo
03:15 queued 01:20
created

JsonRpcEndpoint::createRawResponseFromException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 6
    public function __construct(
35
        RawRequestSerializer $rawRequestSerializer,
36
        RequestHandler $requestHandler,
37
        RawResponseSerializer $rawResponseNormalizer,
38
        ResponseCreator $responseCreator
39
    ) {
40 6
        $this->rawRequestSerializer = $rawRequestSerializer;
41 6
        $this->requestHandler = $requestHandler;
42 6
        $this->rawResponseNormalizer = $rawResponseNormalizer;
43 6
        $this->responseCreator = $responseCreator;
44 6
    }
45
46
    /**
47
     * @param string $request
48
     *
49
     * @return string The response
50
     */
51 6
    public function index(string $request) : string
52
    {
53
        try {
54 6
            $rawResponse = $this->handleRawRequest(
55 6
                $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 6
        return $this->rawResponseNormalizer->serialize($rawResponse);
68
    }
69
70
    /**
71
     * @param JsonRpcRawRequest $rawRequest
72
     *
73
     * @return JsonRpcRawResponse
74
     */
75 6
    private function handleRawRequest(JsonRpcRawRequest $rawRequest) : JsonRpcRawResponse
76
    {
77 6
        $rawResponse = new JsonRpcRawResponse($rawRequest->isBatch());
78
79 6
        foreach ($rawRequest->getItemtList() as $item) {
80 6
            if ($item instanceof \Exception) {
81 2
                $response = $this->responseCreator->createErrorResponse($item);
82
            } else {
83 5
                $response = $this->requestHandler->handle($item);
84
            }
85
86 4
            $rawResponse->addResponse($response);
87
        }
88
89 4
        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