1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Helpers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class jsonApiResponseHelper |
9
|
|
|
* @package Helpers |
10
|
|
|
*/ |
11
|
|
|
trait JsonApiResponseHelper |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param array $errors |
15
|
|
|
* @param int $statusCode |
16
|
|
|
* @return JsonResponse |
17
|
|
|
*/ |
18
|
|
|
public function sendFailedResponse(array $errors, int $statusCode) |
19
|
|
|
{ |
20
|
|
|
$errorsJson = []; |
21
|
|
View Code Duplication |
if (!empty($errors['email'])) { |
|
|
|
|
22
|
|
|
$emailError = [ |
23
|
|
|
'status' => $statusCode, |
24
|
|
|
'title' => 'Email error', |
25
|
|
|
'detail' => $errors['email'][0] |
26
|
|
|
]; |
27
|
|
|
array_push($errorsJson, $emailError); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
View Code Duplication |
if (!empty($errors['password'])) { |
|
|
|
|
31
|
|
|
$passwordError = [ |
32
|
|
|
'status' => $statusCode, |
33
|
|
|
'title' => 'Password error', |
34
|
|
|
'detail' => $errors['password'][0] |
35
|
|
|
]; |
36
|
|
|
array_push($errorsJson, $passwordError); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
if (!empty($errors['password_confirmation'])) { |
|
|
|
|
40
|
|
|
$passwordConfirmationError = [ |
41
|
|
|
'status' => $statusCode, |
42
|
|
|
'title' => 'Password error', |
43
|
|
|
'detail' => $errors['password_confirmation'][0] |
44
|
|
|
]; |
45
|
|
|
array_push($errorsJson, $passwordConfirmationError); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
if (!empty($errors['name'])) { |
|
|
|
|
49
|
|
|
$nameError = [ |
50
|
|
|
'status' => $statusCode, |
51
|
|
|
'title' => 'Name error', |
52
|
|
|
'detail' => $errors['name'][0] |
53
|
|
|
]; |
54
|
|
|
array_push($errorsJson, $nameError); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
if (!empty($errors['token'])) { |
|
|
|
|
58
|
|
|
$tokenError = [ |
59
|
|
|
'status' => $statusCode, |
60
|
|
|
'title' => 'Token error', |
61
|
|
|
'detail' => $errors['token'] |
62
|
|
|
]; |
63
|
|
|
array_push($errorsJson, $tokenError); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
if (!empty($errors['token'])) { |
|
|
|
|
67
|
|
|
$tokenError = [ |
68
|
|
|
'status' => $statusCode, |
69
|
|
|
'title' => 'Token error', |
70
|
|
|
'detail' => $errors['token'] |
71
|
|
|
]; |
72
|
|
|
array_push($errorsJson, $tokenError); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
if (!empty($errors['activation_hash'])) { |
|
|
|
|
76
|
|
|
$activationHashError = [ |
77
|
|
|
'status' => $statusCode, |
78
|
|
|
'title' => 'Activation hash error', |
79
|
|
|
'detail' => $errors['activation_hash'][0] |
80
|
|
|
]; |
81
|
|
|
array_push($errorsJson, $activationHashError); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$errorsJson = ($errorsJson) ? $errorsJson : $errors; |
85
|
|
|
|
86
|
|
|
$responseJson = response()->json([ |
87
|
|
|
'errors' => |
88
|
|
|
$errorsJson |
89
|
|
|
], $statusCode); |
90
|
|
|
|
91
|
|
|
return $responseJson; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return JsonResponse |
96
|
|
|
*/ |
97
|
|
|
public function sendSuccessResponse() |
98
|
|
|
{ |
99
|
|
|
return response()->json([], self::HTTP_CODE_OK); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.