ExceptionHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getJsonRpcResponseFromException() 0 8 1
A __construct() 0 3 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 47
    public function __construct(ResponseCreator $responseCreator)
24
    {
25 47
        $this->responseCreator = $responseCreator;
26
    }
27
28
    /**
29
     * @param \Exception          $exception
30
     * @param JsonRpcRequest|null $fromRequest
31
     *
32
     * @return JsonRpcResponse
33
     */
34 33
    public function getJsonRpcResponseFromException(
35
        \Exception $exception,
36
        JsonRpcRequest $fromRequest = null
37
    ) : JsonRpcResponse {
38 33
        $event = new ActionEvent\OnExceptionEvent($exception, $fromRequest);
39 33
        $this->dispatchJsonRpcEvent($event::EVENT_NAME, $event);
40
41 33
        return $this->responseCreator->createErrorResponse($event->getException(), $fromRequest);
42
    }
43
}
44