1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\ApiBundle\Service; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Resource\Collection; |
6
|
|
|
use League\Fractal\Resource\Item; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use League\Fractal\Manager; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Tobias Nyholm <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class ResponseFactory |
14
|
|
|
{ |
15
|
|
|
const CODE_WRONG_ARGS = 'GEN-ARGUMENTS'; |
16
|
|
|
const CODE_NOT_FOUND = 'GEN-NOTFOUND'; |
17
|
|
|
const CODE_INTERNAL_ERROR = 'GEN-SERVERERROR'; |
18
|
|
|
const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED'; |
19
|
|
|
const CODE_FORBIDDEN = 'GEN-FORBIDDEN'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Manager |
23
|
|
|
*/ |
24
|
|
|
private $fractal; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Manager $fractal |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Manager $fractal) |
30
|
|
|
{ |
31
|
|
|
$this->fractal = $fractal; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return Manager |
36
|
|
|
*/ |
37
|
|
|
public function getFractal() |
38
|
|
|
{ |
39
|
|
|
return $this->fractal; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $item |
44
|
|
|
* @param $callback |
45
|
|
|
* |
46
|
|
|
* @return JsonResponse |
47
|
|
|
*/ |
48
|
|
|
public function createWithItem($item, $callback) |
49
|
|
|
{ |
50
|
|
|
$resource = new Item($item, $callback); |
51
|
|
|
$rootScope = $this->fractal->createData($resource); |
52
|
|
|
|
53
|
|
|
return $this->createWithArray($rootScope->toArray()); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param mixed $collection |
58
|
|
|
* @param $callback |
59
|
|
|
* |
60
|
|
|
* @return JsonResponse |
61
|
|
|
*/ |
62
|
|
|
public function createWithCollection($collection, $callback) |
63
|
|
|
{ |
64
|
|
|
$resource = new Collection($collection, $callback); |
65
|
|
|
$rootScope = $this->fractal->createData($resource); |
66
|
|
|
|
67
|
|
|
return $this->createWithArray($rootScope->toArray()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param array $array |
72
|
|
|
* @param int $statusCode |
73
|
|
|
* @param array $headers |
74
|
|
|
* |
75
|
|
|
* @return JsonResponse |
76
|
|
|
*/ |
77
|
|
|
public function createWithArray(array $array, $statusCode = 200, array $headers = []) |
78
|
|
|
{ |
79
|
|
|
return new JsonResponse($array, $statusCode, $headers); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $message |
84
|
|
|
* @param int $statusCode |
85
|
|
|
* @param int $errorCode |
86
|
|
|
* |
87
|
|
|
* @return JsonResponse |
88
|
|
|
*/ |
89
|
|
|
public function createWithError($message, $statusCode, $errorCode) |
90
|
|
|
{ |
91
|
|
|
if ($statusCode === 200) { |
92
|
|
|
trigger_error( |
93
|
|
|
'You better have a really good reason for erroring on a 200...', |
94
|
|
|
E_USER_WARNING |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this->createWithArray([ |
99
|
|
|
'error' => [ |
100
|
|
|
'code' => $errorCode, |
101
|
|
|
'http_code' => $statusCode, |
102
|
|
|
'message' => $message, |
103
|
|
|
], |
104
|
|
|
], $statusCode); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Generates a Response with a 403 HTTP header and a given message. |
109
|
|
|
* |
110
|
|
|
* @return JsonResponse |
111
|
|
|
*/ |
112
|
|
|
public function createForbidden($message = 'Forbidden') |
113
|
|
|
{ |
114
|
|
|
return $this->createWithError($message, 403, self::CODE_FORBIDDEN); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Generates a Response with a 500 HTTP header and a given message. |
119
|
|
|
* |
120
|
|
|
* @return JsonResponse |
121
|
|
|
*/ |
122
|
|
|
public function createInternalError($message = 'Internal Error') |
123
|
|
|
{ |
124
|
|
|
return $this->createWithError($message, 500, self::CODE_INTERNAL_ERROR); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Generates a Response with a 404 HTTP header and a given message. |
129
|
|
|
* |
130
|
|
|
* @return JsonResponse |
131
|
|
|
*/ |
132
|
|
|
public function createNotFound($message = 'Resource Not Found') |
133
|
|
|
{ |
134
|
|
|
return $this->createWithError($message, 404, self::CODE_NOT_FOUND); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Generates a Response with a 401 HTTP header and a given message. |
139
|
|
|
* |
140
|
|
|
* @return JsonResponse |
141
|
|
|
*/ |
142
|
|
|
public function createUnauthorized($message = 'Unauthorized') |
143
|
|
|
{ |
144
|
|
|
return $this->createWithError($message, 401, self::CODE_UNAUTHORIZED); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Generates a Response with a 400 HTTP header and a given message. |
149
|
|
|
* |
150
|
|
|
* @return JsonResponse |
151
|
|
|
*/ |
152
|
|
|
public function createWrongArgs($message = 'Wrong Arguments') |
153
|
|
|
{ |
154
|
|
|
return $this->createWithError($message, 400, self::CODE_WRONG_ARGS); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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.