ApiController::responseNotFound()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
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
 */
12
class ApiController extends BaseController
13
{
14
    /**
15
     * @var int
16
     */
17
    protected $statusCode;
18
19
    /**
20
     * @var int
21
     */
22
    protected $responseCode = 200;
23
24
    /**
25
     * @return int
26
     */
27
    public function getResponseCode()
28
    {
29
        return $this->responseCode;
30
    }
31
32
    /**
33
     * @return int
34
     */
35
    public function getStatusCode()
36
    {
37
        return $this->statusCode;
38
    }
39
40
    /**
41
     * @param int $code
42
     *
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
     *
55
     * @return $this
56
     */
57
    public function setResponseCode($code)
58
    {
59
        $this->responseCode = $code;
60
61
        return $this;
62
    }
63
64
    /**
65
     * @param string $currentETag
66
     *
67
     * @return JsonResponse
68
     */
69
    public function checkETag($currentETag): ?JsonResponse
70
    {
71
        $newETag = request('ETag');
72
73
        if (null !== $newETag && $newETag === $currentETag) {
74
            return $this->responseNotModified(['ETag' => $currentETag]);
75
        }
76
    }
77
78
    /**
79
     * @param array $data
80
     * @param array $headers
81
     *
82
     * @return JsonResponse
83
     */
84
    public function respond(array $data, array $headers = []): JsonResponse
85
    {
86
        return Response::json($data, $this->getResponseCode(), $headers);
87
    }
88
89
    /**
90
     * @param array $data
91
     * @param array $headers
92
     *
93
     * @return JsonResponse
94
     */
95
    public function respondWithSingleObject(array $data, array $headers = []): JsonResponse
96
    {
97
        return Response::json(reset($data), $this->getResponseCode(), $headers);
98
    }
99
100
    /**
101
     * @param string $message
102
     *
103
     * @return JsonResponse
104
     */
105
    public function respondWithError(string $message): JsonResponse
106
    {
107
        return $this->respond([
108
            'error' => [
109
                'message' => $message,
110
                'code'    => $this->getStatusCode(),
111
            ],
112
        ]);
113
    }
114
115
    /**
116
     * @param array $data
117
     * @param array $headers
118
     *
119
     * @return JsonResponse
120
     */
121
    public function responseCreated(array $data, array $headers = []): JsonResponse
122
    {
123
        return $this->setResponseCode(201)
124
            ->respond($data, $headers);
125
    }
126
127
    /**
128
     * @param string $message
129
     * @param array  $headers
130
     *
131
     * @return JsonResponse
132
     */
133
    public function responseNoContent(
134
        string $message = 'The request was successful, but no content was send back.',
135
        array $headers = []
136
    ): JsonResponse {
137
        return $this->setResponseCode(204)
138
            ->respond(['message' => $message], $headers);
139
    }
140
141
    /**
142
     * @param string $message
143
     * @param array  $headers
144
     *
145
     * @return JsonResponse
146
     */
147
    public function responseAccepted(
148
        string $message = 'The request was accepted for processing',
149
        array $headers = []
150
    ): JsonResponse {
151
        return $this->setResponseCode(202)
152
            ->setStatusCode(224)
153
            ->respond(['message' => $message], $headers);
154
    }
155
156
    /**
157
     * @param array $headers
158
     *
159
     * @return JsonResponse
160
     */
161
    public function responseNotModified(array $headers = []): JsonResponse
162
    {
163
        return $this->setResponseCode(304)
164
            ->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

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