Passed
Push — master ( 72ba5d...808b88 )
by Yo
01:00 queued 10s
created

ExceptionHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 7
dl 0
loc 29
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getJsonRpcResponseFromException() 0 8 1
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\Model\JsonRpcRequest;
8
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcResponse;
9
10
/**
11
 * Class ExceptionHandler
12
 */
13
class ExceptionHandler
14
{
15
    use JsonRpcServerDispatcherAwareTrait;
16
17
    /** @var ResponseCreator */
18
    private $responseCreator;
19
20
    /**
21
     * @param ResponseCreator $responseCreator
22
     */
23 4
    public function __construct(ResponseCreator $responseCreator)
24
    {
25 4
        $this->responseCreator = $responseCreator;
26 4
    }
27
28
    /**
29
     * @param \Exception          $exception
30
     * @param JsonRpcRequest|null $fromRequest
31
     *
32
     * @return JsonRpcResponse
33
     */
34 4
    public function getJsonRpcResponseFromException(
35
        \Exception $exception,
36
        JsonRpcRequest $fromRequest = null
37
    ) : JsonRpcResponse {
38 4
        $event = new ActionEvent\OnExceptionEvent($exception, $fromRequest);
39 4
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
40
41 4
        return $this->responseCreator->createErrorResponse($event->getException(), $fromRequest);
42
    }
43
}
44