Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
57.14% covered (warning)
57.14%
8 / 14
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiResponse
57.14% covered (warning)
57.14%
8 / 14
60.00% covered (warning)
60.00%
3 / 5
6.97
0.00% covered (danger)
0.00%
0 / 1
 rollback
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 throw
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 sendResponse
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 success
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 error
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Responses;
4
5use http\Env\Response;
6use Illuminate\Http\JsonResponse;
7use Illuminate\Support\Facades\DB;
8use Illuminate\Support\Facades\Log;
9
10class ApiResponse
11{
12    public static function rollback(
13        $error,
14        $message = "Something went wrong! Process did not complete."
15    ): void
16    {
17        DB::rollback();
18        self::throw($error, $message);
19    }
20
21    public static function throw(
22        $error,
23        $message = "Something went wrong! Process did not complete."
24    ): void
25    {
26        Log::error($error);
27        throw new \HttpResponseException(
28            response()->json(['message' => $message], 500)
29        );
30    }
31
32    public static function sendResponse(
33        $result,
34        $message,
35        $success = true,
36        $code = 200
37    ): JsonResponse
38    {
39        $response = [
40            'success' => $success,
41            'message' => $message ?? null,
42            'data' => $result,
43        ];
44
45        return response()->json($response, $code);
46    }
47
48    public static function success($result, $message, $code = 200): JsonResponse
49    {
50        return self::sendResponse($result, $message, true, $code);
51    }
52
53    public static function error($result, $message, $code = 500): JsonResponse
54    {
55        return self::sendResponse($result, $message, false, $code);
56    }
57
58}