GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AbstractResponse::withError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 3
ccs 7
cts 7
cp 1
crap 1
1
<?php
2
3
namespace EllipseSynergie\ApiResponse;
4
5
use EllipseSynergie\ApiResponse\Contracts\Response;
6
use League\Fractal\Resource\Collection;
7
use League\Fractal\Resource\Item;
8
use League\Fractal\Manager;
9
use League\Fractal\Pagination\Cursor;
10
11
/**
12
 * Class Response
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @package EllipseSynergie\ApiResponse
18
 * @author Maxime Beaudoin <[email protected]>
19
 * @author Phil Sturgeon <[email protected]>
20
 */
21
abstract class AbstractResponse implements Response
22
{
23
24
    const CODE_WRONG_ARGS = 'GEN-WRONG-ARGS';
25
26
    const CODE_NOT_FOUND = 'GEN-NOT-FOUND';
27
28
    const CODE_INTERNAL_ERROR = 'GEN-INTERNAL-ERROR';
29
30
    const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED';
31
32
    const CODE_FORBIDDEN = 'GEN-FORBIDDEN';
33
34
    const CODE_GONE = 'GEN-GONE';
35
36
    const CODE_METHOD_NOT_ALLOWED = 'GEN-METHOD-NOT-ALLOWED';
37
38
    const CODE_UNWILLING_TO_PROCESS = 'GEN-UNWILLING-TO-PROCESS';
39
    
40
    const CODE_UNPROCESSABLE = 'GEN-UNPROCESSABLE';
41
    
42
43
    /**
44
     * HTTP Status code
45
     *
46
     * @var int
47
     */
48
    protected $statusCode = 200;
49
50
    /**
51
     * Fractal manager
52
     *
53
     * @var \League\Fractal\Manager
54
     */
55
    protected $manager;
56
57
    /**
58
     * @param \League\Fractal\Manager $manager
59
     */
60 23
    public function __construct(Manager $manager)
61
    {
62 23
        $this->manager = $manager;
63 23
    }
64
65
    /**
66
     * @return \League\Fractal\Manager
67
     */
68 2
    public function getManager()
69
    {
70 2
        return $this->manager;
71
    }
72
73
    /**
74
     * Getter for statusCode
75
     *
76
     * @return int
77
     */
78 2
    public function getStatusCode()
79
    {
80 2
        return $this->statusCode;
81
    }
82
83
    /**
84
     * Setter for status code
85
     *
86
     * @param int $statusCode
87
     * @return \EllipseSynergie\ApiResponse\AbstractResponse
88
     */
89 12
    public function setStatusCode($statusCode)
90
    {
91 12
        $this->statusCode = $statusCode;
92 12
        return $this;
93
    }
94
95
    /**
96
     * Implement this !!!
97
     * This method return the final response output
98
     *
99
     * @param array $array
100
     * @param array $headers
101
     * @param int $json_options  @link http://php.net/manual/en/function.json-encode.php
102
     * @return
103
     */
104
    abstract public function withArray(array $array, array $headers = [], $json_options = 0);
105
106
    /**
107
     * Response for one item
108
     *
109
     * @param $data
110
     * @param callable|\League\Fractal\TransformerAbstract $transformer
111
     * @param string $resourceKey
112
     * @param array $meta
113
     * @param array $headers
114
     * @return mixed
115
     */
116 2
    public function withItem($data, $transformer, $resourceKey = null, $meta = [], array $headers = [])
117
    {
118 2
        $resource = new Item($data, $transformer, $resourceKey);
119
120 2
        foreach ($meta as $metaKey => $metaValue) {
121 1
            $resource->setMetaValue($metaKey, $metaValue);
122 2
        }
123
124 2
        $rootScope = $this->manager->createData($resource);
125
126 2
        return $this->withArray($rootScope->toArray(), $headers);
127
    }
128
129
    /**
130
     * Response for collection of items
131
     *
132
     * @param $data
133
     * @param callable|\League\Fractal\TransformerAbstract $transformer
134
     * @param string $resourceKey
135
     * @param Cursor $cursor
136
     * @param array $meta
137
     * @param array $headers
138
     * @return mixed
139
     */
140 3
    public function withCollection($data, $transformer, $resourceKey = null, Cursor $cursor = null, $meta = [], array $headers = [])
141
    {
142 3
        $resource = new Collection($data, $transformer, $resourceKey);
143
144 3
        foreach ($meta as $metaKey => $metaValue) {
145 2
            $resource->setMetaValue($metaKey, $metaValue);
146 3
        }
147
148 3
        if (!is_null($cursor)) {
149 1
            $resource->setCursor($cursor);
150 1
        }
151
152 3
        $rootScope = $this->manager->createData($resource);
153
154 3
        return $this->withArray($rootScope->toArray(), $headers);
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, EllipseSynergie\ApiRespo...ctResponse::withArray() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
155
    }
156
157
    /**
158
     * Response for errors
159
     *
160
     * @param string $message
161
     * @param string $errorCode
162
     * @param array  $headers
163
     * @return mixed
164
     */
165 11
    public function withError($message, $errorCode, array $headers = [])
166
    {
167 11
        return $this->withArray([
168
                'error' => [
169 11
                    'code' => $errorCode,
170 11
                    'http_code' => $this->statusCode,
171
                    'message' => $message
172 11
                ]
173 11
            ],
174
            $headers
175 11
        );
176
    }
177
178
    /**
179
     * Generates a response with a 403 HTTP header and a given message.
180
     *
181
     * @param string $message
182
     * @param array  $headers
183
     * @return mixed
184
     */
185 1
    public function errorForbidden($message = 'Forbidden', array $headers = [])
186
    {
187 1
        return $this->setStatusCode(403)->withError($message, static::CODE_FORBIDDEN, $headers);
188
    }
189
190
    /**
191
     * Generates a response with a 500 HTTP header and a given message.
192
     *
193
     * @param string $message
194
     * @param array  $headers
195
     * @return mixed
196
     */
197 1
    public function errorInternalError($message = 'Internal Error', array $headers = [])
198
    {
199 1
        return $this->setStatusCode(500)->withError($message, static::CODE_INTERNAL_ERROR, $headers);
200
    }
201
202
    /**
203
     * Generates a response with a 404 HTTP header and a given message.
204
     *
205
     * @param string $message
206
     * @param array  $headers
207
     * @return mixed
208
     */
209 1
    public function errorNotFound($message = 'Resource Not Found', array $headers = [])
210
    {
211 1
        return $this->setStatusCode(404)->withError($message, static::CODE_NOT_FOUND, $headers);
212
    }
213
214
    /**
215
     * Generates a response with a 401 HTTP header and a given message.
216
     *
217
     * @param string $message
218
     * @param array  $headers
219
     * @return mixed
220
     */
221 1
    public function errorUnauthorized($message = 'Unauthorized', array $headers = [])
222
    {
223 1
        return $this->setStatusCode(401)->withError($message, static::CODE_UNAUTHORIZED, $headers);
224
    }
225
226
    /**
227
     * Generates a response with a 400 HTTP header and a given message.
228
     *
229
     * @param string $message
230
     * @param array  $headers
231
     * @return mixed
232
     */
233 2
    public function errorWrongArgs($message = 'Wrong Arguments', array $headers = [])
234
    {
235 2
        return $this->setStatusCode(400)->withError($message, static::CODE_WRONG_ARGS, $headers);
236
    }
237
238
    /**
239
     * Generates a response with a 410 HTTP header and a given message.
240
     *
241
     * @param string $message
242
     * @param array  $headers
243
     * @return mixed
244
     */
245 1
    public function errorGone($message = 'Resource No Longer Available', array $headers = [])
246
    {
247 1
        return $this->setStatusCode(410)->withError($message, static::CODE_GONE, $headers);
248
    }
249
250
    /**
251
     * Generates a response with a 405 HTTP header and a given message.
252
     *
253
     * @param string $message
254
     * @param array  $headers
255
     * @return mixed
256
     */
257 1
    public function errorMethodNotAllowed($message = 'Method Not Allowed', array $headers = [])
258
    {
259 1
        return $this->setStatusCode(405)->withError($message, static::CODE_METHOD_NOT_ALLOWED, $headers);
260
    }
261
262
    /**
263
     * Generates a Response with a 431 HTTP header and a given message.
264
     *
265
     * @param string $message
266
     * @param array  $headers
267
     * @return mixed
268
     */
269 1
    public function errorUnwillingToProcess($message = 'Server is unwilling to process the request', array $headers = [])
270
    {
271 1
        return $this->setStatusCode(431)->withError($message, static::CODE_UNWILLING_TO_PROCESS, $headers);
272
    }
273
    
274
    /**
275
     * Generates a Response with a 422 HTTP header and a given message.
276
     *
277
     * @param string $message
278
     * @param array  $headers
279
     * @return mixed
280
     */
281 1
    public function errorUnprocessable($message = 'Unprocessable Entity', array $headers = [])
282
    {
283 1
        return $this->setStatusCode(422)->withError($message, static::CODE_UNPROCESSABLE, $headers);
284
    }
285
}
286