Failed Conditions
Pull Request — release/3.0.0-dev (#32)
by Yo
01:57
created

JsonRpcEndpoint::getJsonRpcCallResponse()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 19
ccs 12
cts 12
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcServer\Infra\Endpoint;
3
4
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait;
5
use Yoanm\JsonRpcServer\App\Handler\ExceptionHandler;
6
use Yoanm\JsonRpcServer\App\Handler\JsonRpcRequestHandler;
7
use Yoanm\JsonRpcServer\App\Serialization\JsonRpcCallSerializer;
8
use Yoanm\JsonRpcServer\Domain\Event\Acknowledge as AcknowledgeEvent;
9
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCall;
10
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCallResponse;
11
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest;
12
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
13
14
/**
15
 * Class JsonRpcEndpoint
16
 */
17
class JsonRpcEndpoint
18
{
19
    use JsonRpcServerDispatcherAwareTrait;
20
21
    /** @var JsonRpcCallSerializer */
22
    private $jsonRpcCallSerializer;
23
    /** @var JsonRpcRequestHandler */
24
    private $jsonRpcRequestHandler;
25
    /** @var ExceptionHandler */
26
    private $exceptionHandler;
27
28
    /**
29
     * @param JsonRpcCallSerializer $jsonRpcCallSerializer
30
     * @param JsonRpcRequestHandler $jsonRpcRequestHandler
31
     */
32 12
    public function __construct(
33
        JsonRpcCallSerializer $jsonRpcCallSerializer,
34
        JsonRpcRequestHandler $jsonRpcRequestHandler,
35
        ExceptionHandler $exceptionHandler
36
    ) {
37 12
        $this->jsonRpcCallSerializer = $jsonRpcCallSerializer;
38 12
        $this->jsonRpcRequestHandler = $jsonRpcRequestHandler;
39 12
        $this->exceptionHandler = $exceptionHandler;
40 12
    }
41
42
    /**
43
     * @param string $request
44
     *
45
     * @return string The response
46
     */
47 12
    public function index(string $request) : string
48
    {
49 12
        $jsonRpcCall = null;
50
        try {
51 12
            $jsonRpcCall = $this->getJsonRpcCall($request);
52
53 10
            $jsonRpcCallResponse = $this->getJsonRpcCallResponse($jsonRpcCall);
54
55 10
            return $this->getResponseString($jsonRpcCallResponse, $jsonRpcCall);
56 4
        } catch (\Exception $exception) {
57
            // Try to create a valid json-rpc error
58 4
            $jsonRpcCallResponse = (new JsonRpcCallResponse())->addResponse(
59 4
                $this->exceptionHandler->getJsonRpcResponseFromException($exception)
60
            );
61
62 4
            return $this->getResponseString($jsonRpcCallResponse, $jsonRpcCall);
63
        }
64
    }
65
66
    /**
67
     * @param string $request
68
     *
69
     * @return JsonRpcCall
70
     */
71 12
    protected function getJsonRpcCall(string $request) : JsonRpcCall
72
    {
73 12
        $jsonRpcCall = $this->jsonRpcCallSerializer->deserialize($request);
74
75 10
        $event = new AcknowledgeEvent\OnRequestReceivedEvent($request, $jsonRpcCall);
76 10
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
77
78 10
        return $jsonRpcCall;
79
    }
80
81
    /**
82
     * @param JsonRpcCallResponse     $jsonRpcCallResponse
83
     * @param JsonRpcCall|null        $jsonRpcCall
84
     *
85
     * @return string
86
     */
87 12
    protected function getResponseString(JsonRpcCallResponse $jsonRpcCallResponse, JsonRpcCall $jsonRpcCall = null) : string
88
    {
89 12
        $response = $this->jsonRpcCallSerializer->serialize($jsonRpcCallResponse);
90
91 12
        $event = new AcknowledgeEvent\OnResponseSendingEvent($response, $jsonRpcCallResponse, $jsonRpcCall);
92 12
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
93
94 12
        return $response;
95
    }
96
97
    /**
98
     * @param JsonRpcCall $jsonRpcCall
99
     *
100
     * @return JsonRpcCallResponse
101
     */
102 10
    protected function getJsonRpcCallResponse(JsonRpcCall $jsonRpcCall) : JsonRpcCallResponse
103
    {
104 10
        $jsonRpcCallResponse = new JsonRpcCallResponse($jsonRpcCall->isBatch());
105
106 10
        foreach ($jsonRpcCall->getItemList() as $itemPosition => $item) {
107 10
            if ($jsonRpcCall->isBatch()) {
108 3
                $event = new AcknowledgeEvent\OnBatchSubRequestProcessingEvent($itemPosition);
109 3
                $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
110
            }
111 10
            $jsonRpcCallResponse->addResponse(
112 10
                $this->processItem($item)
113
            );
114 10
            if ($jsonRpcCall->isBatch()) {
115 3
                $event = new AcknowledgeEvent\OnBatchSubRequestProcessedEvent($itemPosition);
116 10
                $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
117
            }
118
        }
119
120 10
        return $jsonRpcCallResponse;
121
    }
122
123
    /**
124
     * @param JsonRpcRequest|\Exception $item
125
     *
126
     * @return JsonRpcResponse
127
     */
128 10
    private function processItem($item) : JsonRpcResponse
129
    {
130
        try {
131 10
            if ($item instanceof \Exception) {
132
                // Exception will be catched just below and converted to response
133 2
                throw $item;
134
            }
135
136 9
            return $this->jsonRpcRequestHandler->processJsonRpcRequest($item);
137 4
        } catch (\Exception $exception) {
138 4
            return $this->exceptionHandler->getJsonRpcResponseFromException(
139 4
                $exception,
140 4
                $item instanceof JsonRpcRequest ? $item : null
141
            );
142
        }
143
    }
144
}
145