Completed
Pull Request — release/3.0.0 (#58)
by Yo
02:11
created

JsonRpcRequestHandler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 106
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

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