Passed
Push — master ( a6b5f6...3b4983 )
by Michael
04:41
created

ApiController::responseNoContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

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