Failed Conditions
Pull Request — release/3.0.0-dev (#32)
by Yo
01:59
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 8
    public function __construct(
33
        JsonRpcCallSerializer $jsonRpcCallSerializer,
34
        JsonRpcRequestHandler $jsonRpcRequestHandler,
35
        ExceptionHandler $exceptionHandler
36
    ) {
37 8
        $this->jsonRpcCallSerializer = $jsonRpcCallSerializer;
38 8
        $this->jsonRpcRequestHandler = $jsonRpcRequestHandler;
39 8
        $this->exceptionHandler = $exceptionHandler;
40 8
    }
41
42
    /**
43
     * @param string $request
44
     *
45
     * @return string The response
46
     */
47 8
    public function index(string $request) : string
48
    {
49 8
        $jsonRpcCall = null;
1 ignored issue
show
Unused Code introduced by
The assignment to $jsonRpcCall is dead and can be removed.
Loading history...
50
        try {
51 8
            $jsonRpcCall = $this->getJsonRpcCall($request);
52
53 8
            $jsonRpcCallResponse = $this->getJsonRpcCallResponse($jsonRpcCall);
54
55 8
            return $this->getResponseString($jsonRpcCallResponse, $jsonRpcCall);
56
        } catch (\Exception $exception) {
57
            // Try to create a valid json-rpc error
58
            $jsonRpcCallResponse = (new JsonRpcCallResponse())->addResponse(
59
                $this->exceptionHandler->getJsonRpcResponseFromException($exception)
60
            );
61
62
            return $this->getResponseString($jsonRpcCallResponse, $jsonRpcCall);
63
        }
64
    }
65
66
    /**
67
     * @param string $request
68
     *
69
     * @return JsonRpcCall
70
     */
71 8
    protected function getJsonRpcCall(string $request) : JsonRpcCall
72
    {
73 8
        $jsonRpcCall = $this->jsonRpcCallSerializer->deserialize($request);
74
75 8
        $event = new AcknowledgeEvent\OnRequestReceivedEvent($request, $jsonRpcCall);
76 8
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
77
78 8
        return $jsonRpcCall;
79
    }
80
81
    /**
82
     * @param JsonRpcCallResponse     $jsonRpcCallResponse
83
     * @param JsonRpcCall|null        $jsonRpcCall
84
     *
85
     * @return string
86
     */
87 8
    protected function getResponseString(JsonRpcCallResponse $jsonRpcCallResponse, JsonRpcCall $jsonRpcCall = null) : string
88
    {
89 8
        $response = $this->jsonRpcCallSerializer->serialize($jsonRpcCallResponse);
90
91 8
        $event = new AcknowledgeEvent\OnResponseSendingEvent($response, $jsonRpcCallResponse, $jsonRpcCall);
92 8
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
93
94 8
        return $response;
95
    }
96
97
    /**
98
     * @param JsonRpcCall $jsonRpcCall
99
     *
100
     * @return JsonRpcCallResponse
101
     */
102 8
    protected function getJsonRpcCallResponse(JsonRpcCall $jsonRpcCall) : JsonRpcCallResponse
103
    {
104 8
        $jsonRpcCallResponse = new JsonRpcCallResponse($jsonRpcCall->isBatch());
105
106 8
        foreach ($jsonRpcCall->getItemList() as $itemPosition => $item) {
107 8
            if ($jsonRpcCall->isBatch()) {
108 3
                $event = new AcknowledgeEvent\OnBatchSubRequestProcessingEvent($itemPosition);
109 3
                $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
110
            }
111 8
            $jsonRpcCallResponse->addResponse(
112 8
                $this->processItem($item)
113
            );
114 8
            if ($jsonRpcCall->isBatch()) {
115 3
                $event = new AcknowledgeEvent\OnBatchSubRequestProcessedEvent($itemPosition);
116 8
                $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
117
            }
118
        }
119
120 8
        return $jsonRpcCallResponse;
121
    }
122
123
    /**
124
     * @param JsonRpcRequest|\Exception $item
125
     *
126
     * @return JsonRpcResponse
127
     */
128 8
    private function processItem($item) : JsonRpcResponse
129
    {
130
        try {
131 8
            if ($item instanceof \Exception) {
132
                // Exception will be catched just below and converted to response
133 2
                throw $item;
134
            }
135
136 7
            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