1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Napp\Core\Api\Exceptions\Renderer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
use Napp\Core\Api\Exceptions\Exceptions\Exception as NappException; |
7
|
|
|
|
8
|
|
|
class Renderer implements RendererInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Exception |
12
|
|
|
*/ |
13
|
|
|
protected $exception; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $statusCode; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $statusMessage; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
protected $responseCode; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return JsonResponse |
32
|
|
|
*/ |
33
|
|
|
public function render(): JsonResponse |
34
|
|
|
{ |
35
|
|
|
if (true === $this->exception instanceof NappException) { |
36
|
|
|
return response()->json( |
37
|
|
|
[ |
38
|
|
|
'error' => [ |
39
|
|
|
'code' => $this->statusCode, |
40
|
|
|
'message' => $this->statusMessage, |
41
|
|
|
]], |
42
|
|
|
$this->responseCode |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
switch ($this->responseCode) { |
47
|
|
View Code Duplication |
case 400: |
|
|
|
|
48
|
|
|
return response()->json( |
49
|
|
|
[ |
50
|
|
|
'error' => [ |
51
|
|
|
'code' => $this->statusCode, |
52
|
|
|
'message' => 'Unprocessable Entity' |
53
|
|
|
]], |
54
|
|
|
$this->responseCode |
55
|
|
|
); |
56
|
|
View Code Duplication |
case 401: |
|
|
|
|
57
|
|
|
return response()->json( |
58
|
|
|
[ |
59
|
|
|
'error' => [ |
60
|
|
|
'code' => $this->statusCode, |
61
|
|
|
'message' => 'Authentication credentials were missing or incorrect' |
62
|
|
|
]], |
63
|
|
|
$this->responseCode |
64
|
|
|
); |
65
|
|
View Code Duplication |
case 403: |
|
|
|
|
66
|
|
|
return response()->json( |
67
|
|
|
[ |
68
|
|
|
'error' => [ |
69
|
|
|
'code' => $this->statusCode, |
70
|
|
|
'message' => 'Forbidden' |
71
|
|
|
]], |
72
|
|
|
$this->responseCode |
73
|
|
|
); |
74
|
|
View Code Duplication |
case 404: |
|
|
|
|
75
|
|
|
return response()->json( |
76
|
|
|
[ |
77
|
|
|
'error' => [ |
78
|
|
|
'code' => $this->statusCode, |
79
|
|
|
'message' => 'Not Found' |
80
|
|
|
]], |
81
|
|
|
$this->responseCode |
82
|
|
|
); |
83
|
|
View Code Duplication |
case 405: |
|
|
|
|
84
|
|
|
return response()->json( |
85
|
|
|
[ |
86
|
|
|
'error' => [ |
87
|
|
|
'code' => $this->statusCode, |
88
|
|
|
'message' => 'Method Not Allowed' |
89
|
|
|
]], |
90
|
|
|
$this->responseCode |
91
|
|
|
); |
92
|
|
View Code Duplication |
default: |
|
|
|
|
93
|
|
|
return response()->json( |
94
|
|
|
[ |
95
|
|
|
'error' => [ |
96
|
|
|
'code' => $this->statusCode, |
97
|
|
|
'message' => 'Internal Server Error' |
98
|
|
|
]], |
99
|
|
|
500 |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param \Exception $e |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function setException(\Exception $e) |
109
|
|
|
{ |
110
|
|
|
$this->exception = $e; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param int $responseCode |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
|
|
public function setResponseCode($responseCode) |
118
|
|
|
{ |
119
|
|
|
$this->responseCode = $responseCode; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param int $statusCode |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function setStatusCode($statusCode) |
127
|
|
|
{ |
128
|
|
|
$this->statusCode = $statusCode; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $statusMessage |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
public function setStatusMessage($statusMessage) |
136
|
|
|
{ |
137
|
|
|
$this->statusMessage = $statusMessage; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.