ApiController   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 159
ccs 0
cts 67
cp 0
rs 10
c 0
b 0
f 0
wmc 12
1
<?php
2
3
namespace App\Http\Controllers\Api;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Response as IlluminateResponse;
7
use Illuminate\Pagination\LengthAwarePaginator;
8
use Illuminate\Pagination\Paginator;
9
10
/**
11
 * Class ApiController
12
 */
13
class ApiController extends Controller
14
{
15
16
    /**
17
     * @var int Status Code.
18
     */
19
    protected $statusCode = 200;
20
21
    /**
22
     * Function to return an unauthorized response.
23
     *
24
     * @param string $message
25
     * @return mixed
26
     */
27
    public function respondUnauthorizedError($message = 'Unauthorized!')
28
    {
29
        return $this->setStatusCode(IlluminateResponse::HTTP_UNAUTHORIZED)->respondWithError($message);
30
    }
31
32
    /**
33
     * Function to return forbidden error response.
34
     * @param string $message
35
     * @return mixed
36
     */
37
    public function respondForbiddenError($message = 'Forbidden!')
38
    {
39
        return $this->setStatusCode(IlluminateResponse::HTTP_FORBIDDEN)->respondWithError($message);
40
    }
41
42
    /**
43
     * Function to return a Not Found response.
44
     *
45
     * @param string $message
46
     * @return mixed
47
     */
48
    public function respondNotFound($message = 'Not Found')
49
    {
50
        return $this->setStatusCode(IlluminateResponse::HTTP_NOT_FOUND)->respondWithError($message);
51
    }
52
53
    /**
54
     * Function to return an internal error response.
55
     *
56
     * @param string $message
57
     * @return mixed
58
     */
59
    public function respondInternalError($message = 'Internal Error!')
60
    {
61
        return $this->setStatusCode(IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR)->respondWithError($message);
62
    }
63
64
    /**
65
     * Function to return a service unavailable response.
66
     *
67
     * @param string $message
68
     * @return mixed
69
     */
70
    public function respondServiceUnavailable($message = "Service Unavailable!")
71
    {
72
        return $this->setStatusCode(IlluminateResponse::HTTP_SERVICE_UNAVAILABLE)->respondWithError($message);
73
    }
74
75
    /**
76
     * Function to return a generic response.
77
     *
78
     * @param $data Data to be used in response.
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Api\Data was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
79
     * @param array $headers Headers to b used in response.
80
     * @return mixed Return the response.
81
     */
82
    public function respond($data, $headers = [])
83
    {
84
        return response()->json($data, $this->getStatusCode(), $headers);
85
    }
86
87
    /**
88
     * Getter method to return stored status code.
89
     *
90
     * @return mixed
91
     */
92
    public function getStatusCode()
93
    {
94
        return $this->statusCode;
95
    }
96
97
    /**
98
     * Setter method to set status code.
99
     * It is returning current object
100
     * for chaining purposes.
101
     *
102
     * @param mixed $statusCode
103
     * @return current object.
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Api\current was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
     */
105
    public function setStatusCode($statusCode)
106
    {
107
        $this->statusCode = $statusCode;
108
109
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type App\Http\Controllers\Api\ApiController which is incompatible with the documented return type App\Http\Controllers\Api\current.
Loading history...
110
    }
111
112
    /**
113
     * Function to return an error response.
114
     *
115
     * @param $message
116
     * @return mixed
117
     */
118
    public function respondWithError($message)
119
    {
120
        return response()->json([
121
            'error' => [
122
                'message' => $message,
123
                'status_code' => $this->getStatusCode()
124
            ]
125
        ]);
126
    }
127
128
129
    /**
130
     * @param $message
131
     * @return mixed
132
     */
133
    protected function respondCreated($message)
134
    {
135
        return $this->setStatusCode(IlluminateResponse::HTTP_CREATED)
136
            ->respond([
137
                'message' => $message
138
            ]);
139
    }
140
141
142
    /**
143
     * @param $message
144
     * @return mixed
145
     */
146
    protected function respondUnprocessableEntity($message)
147
    {
148
        return $this->setStatusCode(IlluminateResponse::HTTP_UNPROCESSABLE_ENTITY)
149
            ->respond([
150
                'message' => $message
151
            ]);
152
    }
153
154
    /**
155
     * @param Paginator $tournaments
156
     * @param $data
157
     * @return mixed
158
     */
159
    protected function respondWithPagination(LengthAwarePaginator $tournaments, $data)
160
    {
161
162
        $data = array_merge($data, [
163
            'paginator' => [
164
                'total_count' => $tournaments->count(),
165
                'total_pages' => ceil($tournaments->count() / config('constants.PAGINATION')),
166
                'current_page' => 1, // TODO CHANGE
167
                'limit' => config('constants.PAGINATION')
168
            ]
169
        ]);
170
171
        return response()->json($data);
172
    }
173
}