Test Failed
Pull Request — master (#1)
by Yo
10:49 queued 05:04
created

CustomExceptionCreator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A createFor() 0 14 4
1
<?php
2
namespace Yoanm\JsonRpcServer\App\Creator;
3
4
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcException;
5
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcExceptionInterface;
6
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInternalErrorException;
7
8
/**
9
 * Class CustomExceptionCreator
10
 */
11
class CustomExceptionCreator
12
{
13
    const MAX_VALID_ERROR_CODE = -32000;
14
    const MIN_VALID_ERROR_CODE = -32099;
15
16
    const ERROR_DATA_PREVIOUS_KEY = 'previous';
17
18
    /**
19
     * @param \Exception $exception
20
     *
21
     * @return JsonRpcExceptionInterface
22
     */
23
    public function createFor(\Exception $exception) : JsonRpcExceptionInterface
24
    {
25
        $errorCode = (int) $exception->getCode();
26
        if ($errorCode < self::MIN_VALID_ERROR_CODE || $errorCode > self::MAX_VALID_ERROR_CODE) {
27
            return new JsonRpcInternalErrorException($exception);
28
        }
29
30
        $data = [];
31
32
        if ($exception->getPrevious()) {
33
            $data[self::ERROR_DATA_PREVIOUS_KEY] = $exception->getPrevious();
34
        }
35
36
        return new JsonRpcException($errorCode, $exception->getMessage(), $data);
37
    }
38
}
39