Passed
Push — master ( d7d8c0...c26592 )
by Mads
03:35
created

NappApiHandler::getStatusCode()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 8
nop 1
dl 0
loc 24
ccs 0
cts 17
cp 0
crap 72
rs 5.7377
c 0
b 0
f 0
1
<?php
2
3
namespace Napp\Core\Api\Exceptions;
4
5
use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException;
6
use Napp\Core\Api\Exceptions\Renderer\DebugRenderer;
7
use Napp\Core\Api\Exceptions\Renderer\Renderer;
8
use Napp\Core\Api\Exceptions\Renderer\RendererInterface;
9
use Illuminate\Http\JsonResponse;
10
11
class NappApiHandler
12
{
13
    /**
14
     * @var RendererInterface
15
     */
16
    protected $displayer;
17
18
    /**
19
     * @param \Exception $e
20
     */
21
    public function __construct(\Exception $e)
22
    {
23
        if (true === config('app.debug')) {
24
            $this->displayer = new DebugRenderer();
25
        } else {
26
            $this->displayer = new Renderer();
27
        }
28
29
        $this->displayer->setException($e);
30
        $this->displayer->setResponseCode($this->getResponseCode($e));
31
        $this->displayer->setStatusCode($this->getStatusCode($e));
32
        $this->displayer->setStatusMessage($this->getStatusMessage($e));
33
    }
34
35
    /**
36
     * @return JsonResponse
37
     */
38
    public function render(): JsonResponse
39
    {
40
        return $this->displayer->render();
41
    }
42
43
    /**
44
     * @param \Exception $e
45
     * @return int
46
     */
47
    protected function getResponseCode(\Exception $e)
48
    {
49
        if (true === $e instanceof NappException) {
50
            /** @var NappException $e */
51
            return $e->getResponseCode();
52
        }
53
54
        if (true === method_exists($e, 'getStatusCode')) {
55
            return $e->getStatusCode();
56
        }
57
58
        switch (last(explode('\\', get_class($e)))) {
59
            case 'ModelNotFoundException':
60
                return 404;
61
            default:
62
                return 500;
63
        }
64
    }
65
66
    /**
67
     * @param \Exception $e
68
     * @return int
69
     */
70
    protected function getStatusCode(\Exception $e)
71
    {
72
        if (true === $e instanceof NappException) {
73
            /** @var NappException $e */
74
            return $e->getStatusCode();
75
        }
76
77
        if ($e->getCode()) {
78
            return $e->getCode();
79
        }
80
81
        switch ($this->getResponseCode($e)) {
82
            case 400:
83
                return 220;
84
            case 401:
85
                return 135;
86
            case 403:
87
                return 64;
88
            case 404:
89
                return 34;
90
            case 405:
91
                return 34;
92
            default:
93
                return 500;
94
        }
95
    }
96
97
    /**
98
     * @param \Exception $e
99
     * @return string
100
     */
101
    protected function getStatusMessage(\Exception $e)
102
    {
103
        if (true === $e instanceof NappException) {
104
            /** @var NappException $e */
105
            return $e->getStatusMessage();
106
        }
107
108
        if ($e->getMessage()) {
109
            return $e->getMessage();
110
        }
111
112
        return (new \ReflectionClass($e))->getShortName();
113
    }
114
}
115