Completed
Push — master ( 5848d4...d4e3f3 )
by Mads
06:11
created

ApiController::getPageSize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Napp\Core\Api\Controllers;
4
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Response;
8
9
/**
10
 * Class ApiController
11
 * @package Napp\Core\Api\Controllers
12
 */
13
class ApiController extends BaseController
14
{
15
    /**
16
     * @var int
17
     */
18
    protected $statusCode;
19
20
    /**
21
     * @var int
22
     */
23
    protected $responseCode = 200;
24
25
    /**
26
     * @return int
27
     */
28
    public function getResponseCode()
29
    {
30
        return $this->responseCode;
31
    }
32
33
    /**
34
     * @return int
35
     */
36
    public function getStatusCode()
37
    {
38
        return $this->statusCode;
39
    }
40
41
    /**
42
     * @param int $code
43
     * @return $this
44
     */
45
    public function setStatusCode($code)
46
    {
47
        $this->statusCode = $code;
48
49
        return $this;
50
    }
51
52
    /**
53
     * @param int $code
54
     * @return $this
55
     */
56
    public function setResponseCode($code)
57
    {
58
        $this->responseCode = $code;
59
60
        return $this;
61
    }
62
63
    /**
64
     * @param string $currentETag
65
     * @return JsonResponse
66
     */
67
    public function checkETag($currentETag): ?JsonResponse
68
    {
69
        $newETag = request('ETag');
70
71
        if (null !== $newETag && $newETag === $currentETag) {
72
            return $this->responseNotModified(['ETag' => $currentETag]);
73
        }
74
    }
75
76
    /**
77
     * @param array $data
78
     * @param array $headers
79
     * @return JsonResponse
80
     */
81
    public function respond(array $data, array $headers = []): JsonResponse
82
    {
83
        return Response::json($data, $this->getResponseCode(), $headers);
0 ignored issues
show
Bug introduced by
$data of type array is incompatible with the type string expected by parameter $| of Illuminate\Support\Facades\Response::json(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        return Response::json(/** @scrutinizer ignore-type */ $data, $this->getResponseCode(), $headers);
Loading history...
84
    }
85
86
    /**
87
     * @param array $data
88
     * @param array $headers
89
     * @return JsonResponse
90
     */
91
    public function respondWithSingleObject(array $data, array $headers = []): JsonResponse
92
    {
93
        return Response::json(reset($data), $this->getResponseCode(), $headers);
94
    }
95
96
    /**
97
     * @param string $message
98
     * @return JsonResponse
99
     */
100
    public function respondWithError(string $message): JsonResponse
101
    {
102
        return $this->respond([
103
            'error' => [
104
                'message' => $message,
105
                'code'    => $this->getStatusCode()
106
            ]
107
        ]);
108
    }
109
110
    /**
111
     * @param array $data
112
     * @param array $headers
113
     * @return JsonResponse
114
     */
115
    public function responseCreated(array $data, array $headers = []): JsonResponse
116
    {
117
        return $this->setResponseCode(201)
118
            ->respond($data, $headers);
119
    }
120
121
    /**
122
     * @param string $message
123
     * @param array $headers
124
     * @return JsonResponse
125
     */
126
    public function responseNoContent(
127
        string $message = 'The request was successful, but no content was send back.',
128
        array $headers = []
129
    ): JsonResponse {
130
        return $this->setResponseCode(204)
131
            ->respond(['message' => $message], $headers);
132
    }
133
    
134
        /**
135
     * @param string $message
136
     * @param array $headers
137
     * @return JsonResponse
138
     */
139
    public function responseAccepted(
140
        string $message = 'The request was accepted for processing',
141
        array $headers = []
142
    ): JsonResponse {
143
144
        return $this->setResponseCode(202)
145
            ->setStatusCode(224)
146
            ->respond(['message' => $message], $headers);
147
    }
148
149
    /**
150
     * @param array $headers
151
     * @return JsonResponse
152
     */
153
    public function responseNotModified(array $headers = []): JsonResponse
154
    {
155
        return $this->setResponseCode(304)
156
            ->respond(null, $headers);
0 ignored issues
show
Bug introduced by
null of type null is incompatible with the type array expected by parameter $data of Napp\Core\Api\Controllers\ApiController::respond(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

156
            ->respond(/** @scrutinizer ignore-type */ null, $headers);
Loading history...
157
    }
158
159
    /**
160
     * @param string $message
161
     * @return \Illuminate\Http\JsonResponse
162
     */
163
    public function responseBadRequest(string $message = 'The request failed due to an application error.'): JsonResponse
164
    {
165
        return $this->setStatusCode(215)
166
            ->setResponseCode(400)
167
            ->respondWithError($message);
168
    }
169
170
    /**
171
     * @param string $message
172
     * @return JsonResponse
173
     */
174
    public function responseValidationFailed(string $message = 'Validation error.'): JsonResponse
175
    {
176
        return $this->setStatusCode(215)
177
            ->setResponseCode(400)
178
            ->respondWithError($message);
179
    }
180
181
    /**
182
     * @param string $message
183
     * @return JsonResponse
184
     */
185
    public function responseUnprocessableEntity(string $message = 'The request was well-formed but was unable to be followed due to semantic errors.'): JsonResponse
186
    {
187
        return $this->setStatusCode(220)
188
            ->setResponseCode(422)
189
            ->respondWithError($message);
190
    }
191
192
    /**
193
     * @param string $message
194
     * @return JsonResponse
195
     */
196
    public function responseUnauthorized(string $message = 'Authentication credentials were missing or incorrect.'): JsonResponse
197
    {
198
        return $this->setStatusCode(135)
199
            ->setResponseCode(401)
200
            ->respondWithError($message);
201
    }
202
203
    /**
204
     * @param string $message
205
     * @return JsonResponse
206
     */
207
    public function responseUnauthorization(string $message = 'Authorization error. Requested resource is restricted.'): JsonResponse
208
    {
209
        return $this->setStatusCode(87)
210
            ->setResponseCode(403)
211
            ->respondWithError($message);
212
    }
213
214
    /**
215
     * @param string $message
216
     * @return JsonResponse
217
     */
218
    public function responseForbidden(string $message = 'Forbidden.'): JsonResponse
219
    {
220
        return $this->setStatusCode(64)
221
            ->setResponseCode(403)
222
            ->respondWithError($message);
223
    }
224
225
    /**
226
     * @param string $message
227
     * @return JsonResponse
228
     */
229
    public function responseNotFound(string $message = 'Not found.'): JsonResponse
230
    {
231
        return $this->setStatusCode(34)
232
            ->setResponseCode(404)
233
            ->respondWithError($message);
234
    }
235
236
    /**
237
     * @param string $message
238
     * @return JsonResponse
239
     */
240
    public function responseClientNotFound(string $message = 'Client not found.'): JsonResponse
241
    {
242
        return $this->setStatusCode(35)
243
            ->setResponseCode(404)
244
            ->respondWithError($message);
245
    }
246
247
    /**
248
     * @param string $message
249
     * @return JsonResponse
250
     */
251
    public function responseInternalServerError(string $message = 'Internal Server Error.'): JsonResponse
252
    {
253
        return $this->setStatusCode(131)
254
            ->setResponseCode(500)
255
            ->respondWithError($message);
256
    }
257
258
    /**
259
     * @param string $message
260
     * @return JsonResponse
261
     */
262
    public function responseNotImplemented(string $message = 'The request has not been implemented.'): JsonResponse
263
    {
264
        return $this->setStatusCode(131)
265
            ->setResponseCode(501)
266
            ->respondWithError($message);
267
    }
268
269
    /**
270
     * @param string $message
271
     * @return JsonResponse
272
     */
273
    public function responseServiceUnavailable(string $message = 'Service Unavailable.'): JsonResponse
274
    {
275
        return $this->setStatusCode(131)
276
            ->setResponseCode(503)
277
            ->respondWithError($message);
278
    }
279
280
    /**
281
     * @param string $message
282
     * @return JsonResponse
283
     */
284
    public function responseHTTPVersionNotSupported(string $message = 'This is returned if you use the HTTP protocol.'): JsonResponse
285
    {
286
        return $this->setStatusCode(251)
287
            ->setResponseCode(505)
288
            ->respondWithError($message);
289
    }
290
291
    /**
292
     * @param Request $request
293
     * @return int
294
     */
295
    public function getLimit(Request $request): int
296
    {
297
        $limit = (int) $request->get('limit', 30);
298
299
        if ($limit > 200) {
300
            $limit = 200;
301
        }
302
303
        return $limit;
304
    }
305
306
    /**
307
     * @param Request $request
308
     * @return int
309
     */
310
    protected function getOffset(Request $request): int
311
    {
312
        return (int) $request->get('offset', 0);
313
    }
314
315
    /**
316
     * Get the page for pagination
317
     *
318
     * @param Request $request
319
     * @return int
320
     */
321
    public function getPage(Request $request): int
322
    {
323
        return (int) $request->get('page', 1);
324
    }
325
326
    /**
327
     * Get the page size for pagination
328
     *
329
     * @param Request $request
330
     * @return int
331
     */
332
    public function getPageSize(Request $request): int
333
    {
334
        $pageSize = $request->get('pageSize', 50);
335
336
        if ($pageSize > 5000) {
337
            return 5000;
338
        }
339
340
        return (int) $pageSize;
341
    }
342
}
343