Controller   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendError() 0 12 2
A filter() 0 25 6
A sendResponse() 0 9 1
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Controllers\Api;
4
5
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
6
use Illuminate\Foundation\Bus\DispatchesJobs;
7
use Illuminate\Foundation\Validation\ValidatesRequests;
8
use Illuminate\Http\Request;
9
use Illuminate\Routing\Controller as BaseController;
10
11
class Controller extends BaseController
12
{
13
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
14
15
    /**
16
     * success response method.
17
     *
18
     * @return \Illuminate\Http\Response
19
     */
20
    public function sendResponse($result, $message)
21
    {
22
        $response = [
23
            'success' => true,
24
            'data' => $result,
25
            'message' => $message,
26
        ];
27
28
        return response()->json($response, 200);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response, 200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
29
    }
30
31
    /**
32
     * return error response.
33
     *
34
     * @return \Illuminate\Http\Response
35
     */
36
    public function sendError($error, $errorMessages = [], $code = 404)
37
    {
38
        $response = [
39
            'success' => false,
40
            'message' => $error,
41
        ];
42
43
        if (! empty($errorMessages)) {
44
            $response['data'] = $errorMessages;
45
        }
46
47
        return response()->json($response, $code);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($response, $code) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
48
    }
49
50
    public function filter(Request $request, $query)
51
    {
52
        // Where Search
53
        if ($request->has('where')) {
54
            $where = json_decode(str_replace(' ', '', str_replace("'", '"', $request->where)), true);
55
            $query->where($where);
56
        }
57
58
        // orWhere Search
59
        if ($request->has('orWhere')) {
60
            $orWhere = json_decode(str_replace(' ', '', str_replace("'", '"', $request->orWhere)), true);
61
            $query->orWhere($orWhere);
62
        }
63
64
        // Order By
65
        if ($request->has('orderBy')) {
66
            $query->orderBy($request->orderBy, $request->order ? $request->order : 'asc');
67
        }
68
69
        // Limit
70
        if ($request->has('limit')) {
71
            $query->limit((int) $request->limit);
72
        }
73
74
        return $query;
75
    }
76
}
77