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.
Completed
Push — master ( a20e33...3b1634 )
by Maxime
02:15
created

AbstractResponse::errorGone()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
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 21
    public function __construct(Manager $manager)
61
    {
62 21
        $this->manager = $manager;
63 21
    }
64
65
    /**
66
     * @return \League\Fractal\Manager
67
     */
68 1
    public function getManager()
69
    {
70 1
        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
     */
102
    abstract public function withArray(array $array, array $headers = []);
103
104
    /**
105
     * Response for one item
106
     *
107
     * @param $data
108
     * @param callable|\League\Fractal\TransformerAbstract $transformer
109
     * @param string $resourceKey
110
     * @param array $meta
111
     * @param array $headers
112
     * @return mixed
113
     */
114 2
    public function withItem($data, $transformer, $resourceKey = null, $meta = [], array $headers = [])
115
    {
116 2
        $resource = new Item($data, $transformer, $resourceKey);
117
118 2
        foreach ($meta as $metaKey => $metaValue) {
119 1
            $resource->setMetaValue($metaKey, $metaValue);
120 2
        }
121
122 2
        $rootScope = $this->manager->createData($resource);
123
124 2
        return $this->withArray($rootScope->toArray(), $headers);
125
    }
126
127
    /**
128
     * Response for collection of items
129
     *
130
     * @param $data
131
     * @param callable|\League\Fractal\TransformerAbstract $transformer
132
     * @param string $resourceKey
133
     * @param Cursor $cursor
134
     * @param array $meta
135
     * @param array $headers
136
     * @return mixed
137
     */
138 3
    public function withCollection($data, $transformer, $resourceKey = null, Cursor $cursor = null, $meta = [], array $headers = [])
139
    {
140 3
        $resource = new Collection($data, $transformer, $resourceKey);
141
142 3
        foreach ($meta as $metaKey => $metaValue) {
143 2
            $resource->setMetaValue($metaKey, $metaValue);
144 3
        }
145
146 3
        if (!is_null($cursor)) {
147 1
            $resource->setCursor($cursor);
148 1
        }
149
150 3
        $rootScope = $this->manager->createData($resource);
151
152 3
        return $this->withArray($rootScope->toArray(), $headers);
153
    }
154
155
    /**
156
     * Response for errors
157
     *
158
     * @param string $message
159
     * @param string $errorCode
160
     * @param array  $headers
161
     * @return mixed
162
     */
163 11
    public function withError($message, $errorCode, array $headers = [])
164
    {
165 11
        return $this->withArray([
166
                'error' => [
167 11
                    'code' => $errorCode,
168 11
                    'http_code' => $this->statusCode,
169
                    'message' => $message
170 11
                ]
171 11
            ],
172
            $headers
173 11
        );
174
    }
175
176
    /**
177
     * Generates a response with a 403 HTTP header and a given message.
178
     *
179
     * @param string $message
180
     * @param array  $headers
181
     * @return mixed
182
     */
183 1
    public function errorForbidden($message = 'Forbidden', array $headers = [])
184
    {
185 1
        return $this->setStatusCode(403)->withError($message, static::CODE_FORBIDDEN, $headers);
186
    }
187
188
    /**
189
     * Generates a response with a 500 HTTP header and a given message.
190
     *
191
     * @param string $message
192
     * @param array  $headers
193
     * @return mixed
194
     */
195 1
    public function errorInternalError($message = 'Internal Error', array $headers = [])
196
    {
197 1
        return $this->setStatusCode(500)->withError($message, static::CODE_INTERNAL_ERROR, $headers);
198
    }
199
200
    /**
201
     * Generates a response with a 404 HTTP header and a given message.
202
     *
203
     * @param string $message
204
     * @param array  $headers
205
     * @return mixed
206
     */
207 1
    public function errorNotFound($message = 'Resource Not Found', array $headers = [])
208
    {
209 1
        return $this->setStatusCode(404)->withError($message, static::CODE_NOT_FOUND, $headers);
210
    }
211
212
    /**
213
     * Generates a response with a 401 HTTP header and a given message.
214
     *
215
     * @param string $message
216
     * @param array  $headers
217
     * @return mixed
218
     */
219 1
    public function errorUnauthorized($message = 'Unauthorized', array $headers = [])
220
    {
221 1
        return $this->setStatusCode(401)->withError($message, static::CODE_UNAUTHORIZED, $headers);
222
    }
223
224
    /**
225
     * Generates a response with a 400 HTTP header and a given message.
226
     *
227
     * @param string $message
228
     * @param array  $headers
229
     * @return mixed
230
     */
231 2
    public function errorWrongArgs($message = 'Wrong Arguments', array $headers = [])
232
    {
233 2
        return $this->setStatusCode(400)->withError($message, static::CODE_WRONG_ARGS, $headers);
234
    }
235
236
    /**
237
     * Generates a response with a 410 HTTP header and a given message.
238
     *
239
     * @param string $message
240
     * @param array  $headers
241
     * @return mixed
242
     */
243 1
    public function errorGone($message = 'Resource No Longer Available', array $headers = [])
244
    {
245 1
        return $this->setStatusCode(410)->withError($message, static::CODE_GONE, $headers);
246
    }
247
248
    /**
249
     * Generates a response with a 405 HTTP header and a given message.
250
     *
251
     * @param string $message
252
     * @param array  $headers
253
     * @return mixed
254
     */
255 1
    public function errorMethodNotAllowed($message = 'Method Not Allowed', array $headers = [])
256
    {
257 1
        return $this->setStatusCode(405)->withError($message, static::CODE_METHOD_NOT_ALLOWED, $headers);
258
    }
259
260
    /**
261
     * Generates a Response with a 431 HTTP header and a given message.
262
     *
263
     * @param string $message
264
     * @param array  $headers
265
     * @return mixed
266
     */
267 1
    public function errorUnwillingToProcess($message = 'Server is unwilling to process the request', array $headers = [])
268
    {
269 1
        return $this->setStatusCode(431)->withError($message, static::CODE_UNWILLING_TO_PROCESS, $headers);
270
    }
271
    
272
    /**
273
     * Generates a Response with a 422 HTTP header and a given message.
274
     *
275
     * @param string $message
276
     * @param array  $headers
277
     * @return mixed
278
     */
279 1
    public function errorUnprocessable($message = 'Unprocessable Entity', array $headers = [])
280
    {
281 1
        return $this->setStatusCode(422)->withError($message, static::CODE_UNPROCESSABLE, $headers);
282
    }
283
}
284