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

JsonRpcRequestHandler::resolveMethod()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Handler;
3
4
use Yoanm\JsonRpcServer\App\Creator\ResponseCreator;
5
use Yoanm\JsonRpcServer\App\Dispatcher\JsonRpcServerDispatcherAwareTrait;
6
use Yoanm\JsonRpcServer\Domain\Event\Action as ActionEvent;
7
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidParamsException;
8
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcMethodNotFoundException;
9
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodInterface;
10
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodResolverInterface;
11
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest;
12
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
13
14
/**
15
 * Class JsonRpcRequestHandler
16
 */
17
class JsonRpcRequestHandler
18
{
19
    use JsonRpcServerDispatcherAwareTrait;
20
21
    /** @var JsonRpcMethodResolverInterface */
22
    private $methodResolver;
23
    /** @var ResponseCreator */
24
    private $responseCreator;
25
26
    /**
27
     * @param JsonRpcMethodResolverInterface $methodResolver
28
     * @param ResponseCreator                $responseCreator
29
     */
30
    public function __construct(JsonRpcMethodResolverInterface $methodResolver, ResponseCreator $responseCreator)
31
    {
32
        $this->methodResolver = $methodResolver;
33
        $this->responseCreator = $responseCreator;
34
    }
35
36
    /**
37
     * @param JsonRpcRequest $item
38
     *
39
     * @return JsonRpcResponse
40
     *
41
     * @throws JsonRpcInvalidParamsException
42
     * @throws JsonRpcMethodNotFoundException
43
     */
44
    public function processJsonRpcRequest(JsonRpcRequest $jsonRpcRequest) : JsonRpcResponse
45
    {
46
        $method = $this->resolveMethod($jsonRpcRequest);
47
48
        $this->validateParamList($jsonRpcRequest, $method);
49
50
        try {
51
            $result = $method->apply($jsonRpcRequest->getParamList());
52
            $event = new ActionEvent\OnMethodSuccessEvent($result, $method, $jsonRpcRequest);
53
        } catch (\Exception $exception) {
54
            $event = new ActionEvent\OnMethodFailureEvent($exception, $method, $jsonRpcRequest);
55
        }
56
57
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
58
59
        if ($event instanceof ActionEvent\OnMethodSuccessEvent) {
60
            $response = $this->responseCreator->createResultResponse($event->getResult(), $event->getJsonRpcRequest());
61
        } else {
62
            $response = $this->responseCreator->createErrorResponse(
63
                $event->getException(),
64
                $event->getJsonRpcRequest()
65
            );
66
        }
67
68
        return $response;
69
    }
70
71
    /**
72
     * @param JsonRpcRequest $jsonRpcRequest
73
     *
74
     * @return JsonRpcMethodInterface
75
     *
76
     * @throws JsonRpcMethodNotFoundException
77
     */
78
    private function resolveMethod(JsonRpcRequest $jsonRpcRequest) : JsonRpcMethodInterface
79
    {
80
        $method = $this->methodResolver->resolve($jsonRpcRequest->getMethod());
81
82
        if (!$method instanceof JsonRpcMethodInterface) {
83
            throw new JsonRpcMethodNotFoundException($jsonRpcRequest->getMethod());
84
        }
85
86
        return $method;
87
    }
88
89
    /**
90
     * @param JsonRpcRequest $jsonRpcRequest
91
     * @param JsonRpcMethodInterface $method
92
     *
93
     * @throws JsonRpcInvalidParamsException
94
     */
95
    private function validateParamList(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method)
96
    {
97
        $event = new ActionEvent\ValidateParamsEvent($method, $jsonRpcRequest->getParamList() ?? []);
98
        try {
99
            $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
100
        } catch (\Exception $validationException) {
101
            // Append exception to current violation list
102
            $event->addViolation([
103
                'message' => 'Internal error during validation',
104
                'exception' => $validationException->getMessage()
105
            ]);
106
        }
107
108
        if (count($event->getViolationList())) {
109
            throw new JsonRpcInvalidParamsException($event->getViolationList());
110
        }
111
    }
112
}
113