Skip to content

Commit 2f32a20

Browse files
Optimize and simplified the code
1 parent be84846 commit 2f32a20

File tree

1 file changed

+52
-50
lines changed

1 file changed

+52
-50
lines changed

ControllerHelpers.php

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,108 +9,103 @@
99
trait ControllerHelpers
1010
{
1111
/**
12-
* Response json
12+
* Response JSON
1313
*/
14-
public function responseJson(mixed $arr, int $code = 200): JsonResponse
14+
public function responseJson($data, int $code = 200): JsonResponse
1515
{
16-
return response()->json($arr, $code);
16+
return response()->json($data, $code);
1717
}
1818

1919
/**
20-
* Response json validation error
20+
* Response JSON validation error
2121
*/
22-
public function responseJsonValidate(\Illuminate\Support\MessageBag $error, int $code = 422): JsonResponse
22+
public function responseJsonValidate(\Illuminate\Support\MessageBag $errors, int $code = 422): JsonResponse
2323
{
24-
return response()->json(compact('error'), $code);
24+
return response()->json(['error' => $errors], $code);
2525
}
2626

2727
/**
28-
* Response json message
28+
* Response JSON message
2929
*/
3030
public function responseJsonMessage(string $message, int $code = 200): JsonResponse
3131
{
32-
return response()->json(compact('message'), $code);
32+
return response()->json(['message' => $message], $code);
3333
}
3434

3535
/**
36-
* Response json data
36+
* Response JSON data
3737
*/
38-
public function responseJsonData(mixed $data, string $message = 'success get data', int $code = 200): JsonResponse
38+
public function responseJsonData($data, string $message = 'success get data', int $code = 200): JsonResponse
3939
{
40-
return response()->json(compact('data', 'message'), $code);
40+
return response()->json(['data' => $data, 'message' => $message], $code);
4141
}
4242

4343
/**
44-
* Response json message crud
44+
* Response JSON message CRUD
4545
*/
46-
public function responseJsonMessageCrud(bool $success = true, string $method = 'create', string $message = null, string $exception_message = null, int $code = 200, mixed $data = null): JsonResponse
46+
public function responseJsonMessageCrud(bool $success = true, string $method = 'create', string $message = null, string $exception_message = null, int $code = 200, $data = null): JsonResponse
4747
{
48-
if ($success) {
49-
$final_message = 'Success ';
50-
} else {
51-
$final_message = 'Failed ';
52-
}
53-
54-
$methodBind = [
48+
$methods = [
5549
'create' => 'insert new data. ',
5650
'edit' => 'update data. ',
5751
'delete' => 'delete data. ',
5852
'restore' => 'restore data. ',
5953
'forceDelete' => 'force delete data. ',
6054
];
6155

62-
if (array_key_exists($method, $methodBind)) {
63-
$final_message .= $methodBind[$method];
56+
$final_message = ($success ? 'Success ' : 'Failed ');
57+
58+
if (isset($methods[$method])) {
59+
$final_message .= $methods[$method];
6460
}
6561

66-
if ($message != null) {
62+
if ($message !== null) {
6763
$final_message .= $message . ' ';
6864
}
6965

70-
if ($exception_message != null) {
66+
if ($exception_message !== null) {
7167
$final_message .= $exception_message;
7268
}
7369

74-
if ($data == null) {
75-
return response()->json(['message' => $final_message], $code);
76-
} else {
77-
return response()->json(['message' => $final_message, "result" => $data], $code);
70+
$response = ['message' => $final_message];
71+
72+
if ($data !== null) {
73+
$response['result'] = $data;
7874
}
75+
76+
return response()->json($response, $code);
7977
}
8078

8179
/**
82-
* Response message crud
80+
* Response message CRUD
8381
*/
8482
public function responseMessageCrud(bool $success = true, string $method = 'create', string $message = null, string $exception_message = null): array
8583
{
86-
if ($success) {
87-
$final_message = 'Success ';
88-
} else {
89-
$final_message = 'Failed ';
90-
}
91-
92-
$methodBind = [
84+
$methods = [
9385
'create' => 'insert new data. ',
9486
'edit' => 'update data. ',
9587
'delete' => 'delete data. ',
9688
'restore' => 'restore data. ',
9789
'forceDelete' => 'force delete data. ',
9890
];
9991

100-
if (array_key_exists($method, $methodBind)) {
101-
$final_message .= $methodBind[$method];
92+
$final_message = ($success ? 'Success ' : 'Failed ');
93+
94+
if (isset($methods[$method])) {
95+
$final_message .= $methods[$method];
10296
}
103-
if ($message != null) {
97+
98+
if ($message !== null) {
10499
$final_message .= $message . ' ';
105100
}
106101

107-
if ($exception_message != null) {
102+
if ($exception_message !== null) {
108103
$final_message .= $exception_message;
109104
}
110105

111106
return [
112107
'success' => $success,
113-
'message' => $final_message
108+
'message' => $final_message,
114109
];
115110
}
116111

@@ -119,15 +114,22 @@ public function responseMessageCrud(bool $success = true, string $method = 'crea
119114
*/
120115
public function responseFile(string $file_name): \Symfony\Component\HttpFoundation\BinaryFileResponse
121116
{
122-
return response()->file(storage_path('/app/public/' . $file_name));
117+
$file_path = Storage::disk('public')->path($file_name);
118+
119+
if (Storage::disk('public')->exists($file_name)) {
120+
return response()->file($file_path);
121+
}
122+
123+
return response()->file(public_path('/assets/media/auth/404-dark.png'));
123124
}
124125

125126
/**
126-
* Response download from storage
127+
* Response download from storage
127128
*/
128129
public function responseDownloadStorage(string $file): \Symfony\Component\HttpFoundation\BinaryFileResponse
129130
{
130-
return response()->download(storage_path('/app/public/' . $file));
131+
$file_path = storage_path('/app/public/' . $file);
132+
return response()->download($file_path);
131133
}
132134

133135
/**
@@ -143,26 +145,26 @@ public function responseDownload(string $file): \Symfony\Component\HttpFoundatio
143145
*/
144146
public function uploadFile(\Illuminate\Http\UploadedFile $file, string $folder = 'unknown'): string|bool
145147
{
146-
return Storage::disk('public')->put($folder, $file);
148+
return $file->store($folder, 'public');
147149
}
148150

149151
/**
150-
* Delete file from storage
152+
* Delete file from storage
151153
*/
152154
public function deleteFile(string $file_path): bool
153155
{
154156
return Storage::disk('public')->delete($file_path);
155157
}
156158

157159
/**
158-
* Validate api
160+
* Validate API request
159161
*/
160162
public function validateApi($request, $rules): bool|JsonResponse
161163
{
162-
$validate = Validator::make($request, $rules);
164+
$validator = Validator::make($request, $rules);
163165

164-
if ($validate->fails()) {
165-
return $this->responseJsonValidate($validate->errors());
166+
if ($validator->fails()) {
167+
return $this->responseJsonValidate($validator->errors());
166168
}
167169

168170
return true;

0 commit comments

Comments
 (0)