1 | <?php |
||||
2 | |||||
3 | namespace RafflesArgentina\ResourceController\Traits; |
||||
4 | |||||
5 | use Illuminate\Support\MessageBag; |
||||
6 | |||||
7 | trait FormatsValidJsonResponses |
||||
8 | { |
||||
9 | /** |
||||
10 | * Return a valid 500 Internal Server Error json response. |
||||
11 | * |
||||
12 | * @param Exception $exception The exception object. |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
13 | * @param string $message The response message. |
||||
14 | * |
||||
15 | * @return \Illuminate\Http\JsonResponse |
||||
16 | */ |
||||
17 | public function validInternalServerErrorJsonResponse($exception, $message = 'Error') |
||||
0 ignored issues
–
show
The parameter
$message is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||
18 | { |
||||
19 | return response()->json( |
||||
20 | [ |
||||
21 | 'exception' => class_basename($exception), |
||||
22 | 'file' => basename($exception->getFile()), |
||||
23 | 'line' => $exception->getLine(), |
||||
24 | 'message' => $exception->getMessage(), |
||||
25 | 'trace' => $exception->getTrace(), |
||||
26 | ], 500, [], JSON_PRETTY_PRINT |
||||
27 | ); |
||||
28 | } |
||||
29 | |||||
30 | /** |
||||
31 | * Return a valid 404 Not found json response. |
||||
32 | * |
||||
33 | * @param string $message The response message. |
||||
34 | * @param string $redirect The redirection url. |
||||
35 | * |
||||
36 | * @return \Illuminate\Http\JsonResponse |
||||
37 | */ |
||||
38 | public function validNotFoundJsonResponse($message = 'Not found', $redirect = null) |
||||
39 | { |
||||
40 | return response()->json( |
||||
41 | [ |
||||
42 | 'code' => '404', |
||||
43 | 'message' => $message, |
||||
44 | 'errors' => [], |
||||
45 | 'redirect' => $redirect, |
||||
46 | ], 404, [], JSON_PRETTY_PRINT |
||||
47 | ); |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * Return a valid 200 Success json response. |
||||
52 | * |
||||
53 | * @param string $message The response message. |
||||
54 | * @param array $data The passed data. |
||||
55 | * @param string $redirect The redirection url. |
||||
56 | * |
||||
57 | * @return \Illuminate\Http\JsonResponse |
||||
58 | */ |
||||
59 | public function validSuccessJsonResponse($message = 'Success', $data = [], $redirect = null) |
||||
60 | { |
||||
61 | return response()->json( |
||||
62 | [ |
||||
63 | 'code' => '200', |
||||
64 | 'message' => $message, |
||||
65 | 'data' => $data, |
||||
66 | 'errors' => [], |
||||
67 | 'redirect' => $redirect, |
||||
68 | ], 200, [], JSON_PRETTY_PRINT |
||||
69 | ); |
||||
70 | } |
||||
71 | |||||
72 | /** |
||||
73 | * Return a valid 422 Unprocessable entity json response. |
||||
74 | * |
||||
75 | * @param \Illuminate\Support\MessageBag $errors The message bag errors. |
||||
76 | * @param string $message The response message. |
||||
77 | * @param string $redirect The redirection url. |
||||
78 | * |
||||
79 | * @return \Illuminate\Http\JsonResponse |
||||
80 | */ |
||||
81 | public function validUnprocessableEntityJsonResponse(MessageBag $errors, $message = 'Unprocessable Entity', $redirect = null) |
||||
82 | { |
||||
83 | return response()->json( |
||||
84 | [ |
||||
85 | 'code' => '422', |
||||
86 | 'message' => $message, |
||||
87 | 'errors' => $errors, |
||||
88 | 'redirect' => $redirect, |
||||
89 | ], 422, [], JSON_PRETTY_PRINT |
||||
90 | ); |
||||
91 | } |
||||
92 | } |
||||
93 |