forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiController.php
More file actions
53 lines (46 loc) · 1.23 KB
/
ApiController.php
File metadata and controls
53 lines (46 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace App\Http\Controllers\Api\V1;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Log;
use OpenApi\Attributes as OA;
#[OA\Info(title: 'Speedtest Tracker API', version: '1.0.0')]
abstract class ApiController
{
/**
* Send a response.
*
* @param mixed $data
* @param string $message
* @param int $code
* @return \Illuminate\Http\JsonResponse
*/
public static function sendResponse($data, $filters = [], $message = 'ok', $code = 200)
{
$response = array_filter([
'data' => $data,
'filters' => $filters,
'message' => $message,
]);
if (! empty($message)) {
$response['message'] = $message;
}
return response()->json($response, $code);
}
/**
* Throw an exception.
*
* @param \Exception $e
* @param int $code
*
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
public static function throw($e, $code = 500)
{
Log::info($e);
throw new HttpResponseException(
response: response()->json([
'message' => $e->getMessage(),
], $code)
);
}
}