Completed
Push — master ( 814c4e...3deb5a )
by Tobias
07:53
created

ResponseFactory::withPaginator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Happyr\ApiBundle\Service;
4
5
use League\Fractal\Manager;
6
use League\Fractal\Pagination\CursorInterface;
7
use League\Fractal\Pagination\PaginatorInterface;
8
use League\Fractal\Resource\Collection;
9
use League\Fractal\Resource\Item;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
12
/**
13
 * @author Tobias Nyholm <[email protected]>
14
 */
15
final class ResponseFactory
16
{
17
    const CODE_WRONG_ARGS = 'GEN-ARGUMENTS';
18
19
    const CODE_NOT_FOUND = 'GEN-NOTFOUND';
20
21
    const CODE_INTERNAL_ERROR = 'GEN-SERVERERROR';
22
23
    const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED';
24
25
    const CODE_FORBIDDEN = 'GEN-FORBIDDEN';
26
27
    /**
28
     * @var Manager
29
     */
30
    private $fractal;
31
32
    /**
33
     * @var PaginatorInterface
34
     */
35
    private $paginator;
36
37
    /**
38
     * @var CursorInterface
39
     */
40
    private $cursor;
41
42
    /**
43
     * @param Manager $fractal
44
     */
45
    public function __construct(Manager $fractal)
46
    {
47
        $this->fractal = $fractal;
48
    }
49
50
    /**
51
     * @return Manager
52
     */
53
    public function getFractal()
54
    {
55
        return $this->fractal;
56
    }
57
58
    /**
59
     * @param mixed $item
60
     * @param $callback
61
     *
62
     * @return JsonResponse
63
     */
64
    public function createWithItem($item, $callback)
65
    {
66
        $resource = new Item($item, $callback);
67
        $rootScope = $this->fractal->createData($resource);
68
69
        return $this->createWithArray($rootScope->toArray());
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Happyr\ApiBundle\Service...tory::createWithArray() 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...
70
    }
71
72
    /**
73
     * @param mixed $collection
74
     * @param $callback
75
     *
76
     * @return JsonResponse
77
     */
78
    public function createWithCollection($collection, $callback)
79
    {
80
        $resource = new Collection($collection, $callback);
81
        if (null !== $this->paginator) {
82
            $resource->setPaginator($this->paginator);
83
        } elseif (null !== $this->cursor) {
84
            $resource->setCursor($this->cursor);
85
        }
86
        $rootScope = $this->fractal->createData($resource);
87
88
        return $this->createWithArray($rootScope->toArray());
0 ignored issues
show
Bug introduced by
It seems like $rootScope->toArray() targeting League\Fractal\Scope::toArray() can also be of type null; however, Happyr\ApiBundle\Service...tory::createWithArray() 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...
89
    }
90
91
    /**
92
     * @param PaginatorInterface $paginator
93
     *
94
     * @return ResponseFactory
95
     */
96
    public function withPaginator(PaginatorInterface $paginator)
97
    {
98
        $new = clone $this;
99
        $new->paginator = $paginator;
100
101
        return $new;
102
    }
103
104
    /**
105
     * @param CursorInterface $cursor
106
     *
107
     * @return ResponseFactory
108
     */
109
    public function withCursor(CursorInterface $cursor)
110
    {
111
        $new = clone $this;
112
        $new->cursor = $cursor;
113
114
        return $new;
115
    }
116
117
    /**
118
     * @param array $array
119
     * @param int   $statusCode
120
     * @param array $headers
121
     *
122
     * @return JsonResponse
123
     */
124
    public function createWithArray(array $array, $statusCode = 200, array $headers = [])
125
    {
126
        return new JsonResponse($array, $statusCode, $headers);
127
    }
128
129
    /**
130
     * @param string $message
131
     * @param int    $statusCode
132
     * @param string $errorCode
133
     *
134
     * @return JsonResponse
135
     */
136
    public function createWithError($message, $statusCode, $errorCode)
137
    {
138
        if (200 === $statusCode) {
139
            trigger_error(
140
                'You better have a really good reason for erroring on a 200...',
141
                E_USER_WARNING
142
            );
143
        }
144
145
        return $this->createWithArray([
146
            'error' => [
147
                'code' => $errorCode,
148
                'http_code' => $statusCode,
149
                'message' => $message,
150
            ],
151
        ], $statusCode);
152
    }
153
154
    /**
155
     * Generates a Response with a 403 HTTP header and a given message.
156
     *
157
     * @param string $message
158
     *
159
     * @return JsonResponse
160
     */
161
    public function createForbidden($message = 'Forbidden')
162
    {
163
        return $this->createWithError($message, 403, self::CODE_FORBIDDEN);
164
    }
165
166
    /**
167
     * Generates a Response with a 500 HTTP header and a given message.
168
     *
169
     * @param string $message
170
     *
171
     * @return JsonResponse
172
     */
173
    public function createInternalError($message = 'Internal Error')
174
    {
175
        return $this->createWithError($message, 500, self::CODE_INTERNAL_ERROR);
176
    }
177
178
    /**
179
     * Generates a Response with a 404 HTTP header and a given message.
180
     *
181
     * @param string $message
182
     *
183
     * @return JsonResponse
184
     */
185
    public function createNotFound($message = 'Resource Not Found')
186
    {
187
        return $this->createWithError($message, 404, self::CODE_NOT_FOUND);
188
    }
189
190
    /**
191
     * Generates a Response with a 401 HTTP header and a given message.
192
     *
193
     * @param string $message
194
     *
195
     * @return JsonResponse
196
     */
197
    public function createUnauthorized($message = 'Unauthorized')
198
    {
199
        return $this->createWithError($message, 401, self::CODE_UNAUTHORIZED);
200
    }
201
202
    /**
203
     * Generates a Response with a 400 HTTP header and a given message.
204
     *
205
     * @param string $message
206
     *
207
     * @return JsonResponse
208
     */
209
    public function createWrongArgs($message = 'Wrong Arguments')
210
    {
211
        return $this->createWithError($message, 400, self::CODE_WRONG_ARGS);
212
    }
213
}
214