|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Front\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
6
|
|
|
use App\Http\Controllers\Controller as BaseController; |
|
7
|
|
|
|
|
8
|
|
|
class Controller extends BaseController |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var int |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $statusCode = Response::HTTP_OK; |
|
14
|
|
|
|
|
15
|
|
|
public function setStatusCode(int $statusCode) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->statusCode = $statusCode; |
|
18
|
|
|
|
|
19
|
|
|
return $this; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getStatusCode(): int |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->statusCode; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Send a response with a internal server error code. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $message |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Illuminate\Http\Response |
|
33
|
|
|
*/ |
|
34
|
|
|
public function respondWithInternalServerError($message = 'Internal Server Error') |
|
35
|
|
|
{ |
|
36
|
|
|
return $this |
|
37
|
|
|
->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR) |
|
38
|
|
|
->respondWithError($message); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Send a response with a bad request. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $message |
|
45
|
|
|
* |
|
46
|
|
|
* @return \Illuminate\Http\Response |
|
47
|
|
|
*/ |
|
48
|
|
|
public function respondWithBadRequest($message = 'Bad Request') |
|
49
|
|
|
{ |
|
50
|
|
|
return $this |
|
51
|
|
|
->setStatusCode(Response::HTTP_BAD_REQUEST) |
|
52
|
|
|
->respondWithError($message); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set a response. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* @param array $headers |
|
60
|
|
|
* |
|
61
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
62
|
|
|
*/ |
|
63
|
|
|
public function respond(array $data, array $headers = []) |
|
64
|
|
|
{ |
|
65
|
|
|
return response()->json($data, $this->getStatusCode(), $headers); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Respond with an error message. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $message |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
74
|
|
|
*/ |
|
75
|
|
|
public function respondWithError($message) |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->respond( |
|
78
|
|
|
[ |
|
79
|
|
|
'error' => $message, |
|
80
|
|
|
'status_code' => $this->getStatusCode(), |
|
81
|
|
|
] |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|