Test Failed
Push — feature/init ( ecfce8...b38e47 )
by Yo
05:12
created

JsonRpcEndpoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 9.4285
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\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