Passed
Push — feature/app ( 806163...3205d0 )
by Yo
01:55
created

RequestHandler   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 12 2
1
<?php
2
namespace Yoanm\JsonRpcServer\App;
3
4
use Yoanm\JsonRpcServer\App\Creator\ResponseCreator;
5
use Yoanm\JsonRpcServer\App\Manager\MethodManager;
6
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest;
7
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
8
9
/**
10
 * Class RequestHandler
11
 */
12
class RequestHandler
13
{
14
    /** @var MethodManager */
15
    private $methodManager;
16
    /** @var ResponseCreator */
17
    private $responseCreator;
18
19
    /**
20
     * @param MethodManager   $methodManager
21
     * @param ResponseCreator $responseCreator
22
     */
23 2
    public function __construct(MethodManager $methodManager, ResponseCreator $responseCreator)
24
    {
25 2
        $this->methodManager = $methodManager;
26 2
        $this->responseCreator = $responseCreator;
27 2
    }
28
29
    /**
30
     * @param JsonRpcRequest $jsonRpcRequest
31
     *
32
     * @return JsonRpcResponse
33
     */
34 2
    public function handle(JsonRpcRequest $jsonRpcRequest) : JsonRpcResponse
35
    {
36
        try {
37 2
            return $this->responseCreator->createResultResponse(
38 2
                $this->methodManager->apply(
39 2
                    $jsonRpcRequest->getMethod(),
40 2
                    $jsonRpcRequest->getParamList()
41
                ),
42 1
                $jsonRpcRequest
43
            );
44 1
        } catch (\Exception $exception) {
45 1
            return $this->responseCreator->createErrorResponse($exception, $jsonRpcRequest);
46
        }
47
    }
48
}
49