Passed
Push — master ( 301768...50e705 )
by Ivan
11:01
created

Handler::extractCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Boomdraw\RpcCore\Exceptions;
6
7
use Boomdraw\RpcCore\Request as RpcRequest;
8
use Illuminate\Auth\Access\AuthorizationException;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use Illuminate\Http\Request;
11
use Illuminate\Validation\ValidationException;
12
use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpKernel\Exception\HttpException;
15
use Throwable;
16
17
class Handler extends ExceptionHandler
18
{
19
    /**
20
     * A list of the exception types that should not be reported.
21
     *
22
     * @var array
23
     */
24
    protected $dontReport = [
25
        AuthorizationException::class,
26
        HttpException::class,
27
        ModelNotFoundException::class,
28
        ValidationException::class,
29
        InvalidParamsRpcException::class,
30
    ];
31
32
    /**
33
     * Render an exception into an HTTP response.
34
     *
35
     * @param Request $request
36
     * @param Throwable $e
37
     * @return Response
38
     * @throws Throwable
39
     */
40 14
    public function render($request, Throwable $e): Response
41
    {
42 14
        if ($e instanceof RpcException) {
43 9
            return $e->toResponse($request);
44
        }
45 6
        if (! $request instanceof RpcRequest) {
46 1
            return parent::render($request, $e);
47
        }
48 5
        $e = $this->transformThrowableToRPCException($e);
49
50 5
        return parent::render($request, $e);
51
    }
52
53
    /**
54
     * Transform throwable to RPC exception.
55
     *
56
     * @param Throwable $e
57
     * @return RpcException
58
     */
59 5
    protected function transformThrowableToRPCException(Throwable $e): RpcException
60
    {
61 5
        $code = $this->extractCode($e);
62 5
        $message = $e->getMessage();
63
        switch (true) {
64 5
            case $e instanceof ModelNotFoundException:
65 1
                return RpcException::make($message, -404);
66 4
            case $e instanceof AuthorizationException:
67 1
                return RpcException::make($message, -403);
68 3
            case $e instanceof ValidationException:
69 1
                return InvalidParamsRpcException::make($e->errors());
70 2
            case 500 > $code && $code >= 400:
71 1
                return RpcException::make($message, -$code);
72
            default:
73 1
                return ServerErrorRpcException::make($message);
74
        }
75
    }
76
77
    /**
78
     * Extract code from throwable.
79
     *
80
     * @param Throwable $e
81
     * @return int
82
     */
83 5
    protected function extractCode(Throwable $e): int
84
    {
85 5
        if (method_exists($e, 'getStatusCode')) {
86 2
            return $e->getStatusCode();
87
        }
88
89 3
        return $e->getCode();
90
    }
91
}
92