JsonRpcRequestHandler   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 110
ccs 30
cts 30
cp 1
rs 10
wmc 11

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createResponse() 0 9 2
A setMethodParamsValidator() 0 3 1
A __construct() 0 4 1
A resolveMethod() 0 9 2
A processJsonRpcRequest() 0 17 2
A validateParamList() 0 7 3
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\JsonRpcMethodParamsValidatorInterface;
11
use Yoanm\JsonRpcServer\Domain\JsonRpcMethodResolverInterface;
12
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest;
13
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
14
15
/**
16
 * Class JsonRpcRequestHandler
17
 */
18
class JsonRpcRequestHandler
19
{
20
    use JsonRpcServerDispatcherAwareTrait;
21
22
    /** @var JsonRpcMethodResolverInterface */
23
    private $methodResolver;
24
    /** @var ResponseCreator */
25
    private $responseCreator;
26
    /** @var JsonRpcMethodParamsValidatorInterface|null */
27
    private $methodParamsValidator = null;
28
29
    /**
30
     * @param JsonRpcMethodResolverInterface $methodResolver
31
     * @param ResponseCreator                $responseCreator
32
     */
33 47
    public function __construct(JsonRpcMethodResolverInterface $methodResolver, ResponseCreator $responseCreator)
34
    {
35 47
        $this->methodResolver = $methodResolver;
36 47
        $this->responseCreator = $responseCreator;
37
    }
38
39
    /**
40
     * @param JsonRpcMethodParamsValidatorInterface $methodParamsValidator
41
     *
42
     * @return void
43
     */
44 44
    public function setMethodParamsValidator(JsonRpcMethodParamsValidatorInterface $methodParamsValidator) : void
45
    {
46 44
        $this->methodParamsValidator = $methodParamsValidator;
47
    }
48
49
    /**
50
     * @param JsonRpcRequest $jsonRpcRequest
51
     *
52
     * @return JsonRpcResponse
53
     *
54
     * @throws JsonRpcInvalidParamsException
55
     * @throws JsonRpcMethodNotFoundException
56
     */
57 25
    public function processJsonRpcRequest(JsonRpcRequest $jsonRpcRequest) : JsonRpcResponse
58
    {
59 25
        $method = $this->resolveMethod($jsonRpcRequest);
60
61 21
        $this->validateParamList($jsonRpcRequest, $method);
62
63
        try {
64 19
            $result = $method->apply($jsonRpcRequest->getParamList());
65
66 11
            $event = new ActionEvent\OnMethodSuccessEvent($result, $method, $jsonRpcRequest);
67 8
        } catch (\Exception $exception) {
68 8
            $event = new ActionEvent\OnMethodFailureEvent($exception, $method, $jsonRpcRequest);
69
        }
70
71 19
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
72
73 19
        return $this->createResponse($event);
74
    }
75
76
    /**
77
     * @param JsonRpcRequest $jsonRpcRequest
78
     *
79
     * @return JsonRpcMethodInterface
80
     *
81
     * @throws JsonRpcMethodNotFoundException
82
     */
83 25
    private function resolveMethod(JsonRpcRequest $jsonRpcRequest) : JsonRpcMethodInterface
84
    {
85 25
        $method = $this->methodResolver->resolve($jsonRpcRequest->getMethod());
86
87 25
        if (!$method instanceof JsonRpcMethodInterface) {
88 7
            throw new JsonRpcMethodNotFoundException($jsonRpcRequest->getMethod());
89
        }
90
91 21
        return $method;
92
    }
93
94
    /**
95
     *
96
     * @param JsonRpcRequest $jsonRpcRequest
97
     * @param JsonRpcMethodInterface $method
98
     *
99
     * @return void
100
     *
101
     * @throws JsonRpcInvalidParamsException
102
     */
103 21
    private function validateParamList(JsonRpcRequest $jsonRpcRequest, JsonRpcMethodInterface $method) : void
104
    {
105 21
        if (null !== $this->methodParamsValidator) {
106 19
            $violationList = $this->methodParamsValidator->validate($jsonRpcRequest, $method);
107
108 19
            if (count($violationList)) {
109 2
                throw new JsonRpcInvalidParamsException($violationList);
110
            }
111
        }
112
    }
113
114
    /**
115
     * @param ActionEvent\AbstractOnMethodEvent $event
116
     *
117
     * @return JsonRpcResponse
118
     */
119 19
    protected function createResponse(ActionEvent\AbstractOnMethodEvent $event) : JsonRpcResponse
120
    {
121 19
        if ($event instanceof ActionEvent\OnMethodSuccessEvent) {
122 11
            return $this->responseCreator->createResultResponse($event->getResult(), $event->getJsonRpcRequest());
123
        } else {
124
            /** @var $event ActionEvent\OnMethodFailureEvent */
125 8
            return $this->responseCreator->createErrorResponse(
126 8
                $event->getException(),
127 8
                $event->getJsonRpcRequest()
128 8
            );
129
        }
130
    }
131
}
132