Failed Conditions
Pull Request — release/3.0.0-dev (#32)
by Yo
01:57
created

ExceptionHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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