Passed
Push — master ( d7d8c0...c26592 )
by Mads
03:35
created

ApiController   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 274
ccs 0
cts 83
cp 0
rs 10
c 0
b 0
f 0
wmc 27

24 Methods

Rating   Name   Duplication   Size   Complexity  
A setResponseCode() 0 5 1
A getStatusCode() 0 3 1
A responseBadRequest() 0 5 1
A responseNotImplemented() 0 5 1
A responseForbidden() 0 5 1
A respond() 0 3 1
A setStatusCode() 0 5 1
A respondWithSingleObject() 0 3 1
A responseInternalServerError() 0 5 1
A getResponseCode() 0 3 1
A responseValidationFailed() 0 5 1
A responseServiceUnavailable() 0 5 1
A responseCreated() 0 4 1
A responseUnauthorized() 0 5 1
A getOffset() 0 3 1
A responseNotFound() 0 5 1
A responseHTTPVersionNotSupported() 0 5 1
A respondWithError() 0 6 1
A responseClientNotFound() 0 5 1
A getLimit() 0 9 2
A responseNoContent() 0 6 1
A responseUnprocessableEntity() 0 5 1
A responseNotModified() 0 4 1
A checkETag() 0 6 3
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 array $headers
132
     * @return JsonResponse
133
     */
134
    public function responseNotModified(array $headers = []): JsonResponse
135
    {
136
        return $this->setResponseCode(304)
137
            ->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

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