1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Happyr\ApiBundle\Controller; |
4
|
|
|
|
5
|
|
|
use League\Fractal\Resource\Collection; |
6
|
|
|
use League\Fractal\Resource\Item; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Tobias Nyholm <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
abstract class BaseApiController extends Controller |
14
|
|
|
{ |
15
|
|
|
private $statusCode = 200; |
16
|
|
|
|
17
|
|
|
const CODE_WRONG_ARGS = 'GEN-ARGUMENTS'; |
18
|
|
|
const CODE_NOT_FOUND = 'GEN-NOTFOUND'; |
19
|
|
|
const CODE_INTERNAL_ERROR = 'GEN-SERVERERROR'; |
20
|
|
|
const CODE_UNAUTHORIZED = 'GEN-UNAUTHORIZED'; |
21
|
|
|
const CODE_FORBIDDEN = 'GEN-FORBIDDEN'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Getter for statusCode. |
25
|
|
|
* |
26
|
|
|
* @return int |
27
|
|
|
*/ |
28
|
|
|
public function getStatusCode() |
29
|
|
|
{ |
30
|
|
|
return $this->statusCode; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Setter for statusCode. |
35
|
|
|
* |
36
|
|
|
* @param int $statusCode Value to set |
37
|
|
|
* |
38
|
|
|
* @return self |
39
|
|
|
*/ |
40
|
|
|
public function setStatusCode($statusCode) |
41
|
|
|
{ |
42
|
|
|
$this->statusCode = $statusCode; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param mixed $item |
49
|
|
|
* @param $callback |
50
|
|
|
* |
51
|
|
|
* @return JsonResponse |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
protected function respondWithItem($item, $callback) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$resource = new Item($item, $callback); |
56
|
|
|
$rootScope = $this->get('app.fractal')->createData($resource); |
57
|
|
|
|
58
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $collection |
63
|
|
|
* @param $callback |
64
|
|
|
* |
65
|
|
|
* @return JsonResponse |
66
|
|
|
*/ |
67
|
|
View Code Duplication |
protected function respondWithCollection($collection, $callback) |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
$resource = new Collection($collection, $callback); |
70
|
|
|
$rootScope = $this->get('app.fractal')->createData($resource); |
71
|
|
|
|
72
|
|
|
return $this->respondWithArray($rootScope->toArray()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $message |
|
|
|
|
77
|
|
|
* @param int $errorCode |
|
|
|
|
78
|
|
|
* |
79
|
|
|
* @return JsonResponse |
80
|
|
|
*/ |
81
|
|
|
protected function respondWithArray(array $array, array $headers = []) |
82
|
|
|
{ |
83
|
|
|
return new JsonResponse($array, $this->statusCode, $headers); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $message |
88
|
|
|
* @param int $errorCode |
89
|
|
|
* |
90
|
|
|
* @return JsonResponse |
91
|
|
|
*/ |
92
|
|
|
protected function respondWithError($message, $errorCode) |
93
|
|
|
{ |
94
|
|
|
if ($this->statusCode === 200) { |
95
|
|
|
trigger_error( |
96
|
|
|
'You better have a really good reason for erroring on a 200...', |
97
|
|
|
E_USER_WARNING |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->respondWithArray([ |
102
|
|
|
'error' => [ |
103
|
|
|
'code' => $errorCode, |
104
|
|
|
'http_code' => $this->statusCode, |
105
|
|
|
'message' => $message, |
106
|
|
|
], |
107
|
|
|
]); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Generates a Response with a 403 HTTP header and a given message. |
112
|
|
|
* |
113
|
|
|
* @return JsonResponse |
114
|
|
|
*/ |
115
|
|
|
public function errorForbidden($message = 'Forbidden') |
116
|
|
|
{ |
117
|
|
|
return $this->setStatusCode(403) |
118
|
|
|
->respondWithError($message, self::CODE_FORBIDDEN); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Generates a Response with a 500 HTTP header and a given message. |
123
|
|
|
* |
124
|
|
|
* @return JsonResponse |
125
|
|
|
*/ |
126
|
|
|
public function errorInternalError($message = 'Internal Error') |
127
|
|
|
{ |
128
|
|
|
return $this->setStatusCode(500) |
129
|
|
|
->respondWithError($message, self::CODE_INTERNAL_ERROR); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Generates a Response with a 404 HTTP header and a given message. |
134
|
|
|
* |
135
|
|
|
* @return JsonResponse |
136
|
|
|
*/ |
137
|
|
|
public function errorNotFound($message = 'Resource Not Found') |
138
|
|
|
{ |
139
|
|
|
return $this->setStatusCode(404) |
140
|
|
|
->respondWithError($message, self::CODE_NOT_FOUND); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Generates a Response with a 401 HTTP header and a given message. |
145
|
|
|
* |
146
|
|
|
* @return JsonResponse |
147
|
|
|
*/ |
148
|
|
|
public function errorUnauthorized($message = 'Unauthorized') |
149
|
|
|
{ |
150
|
|
|
return $this->setStatusCode(401) |
151
|
|
|
->respondWithError($message, self::CODE_UNAUTHORIZED); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Generates a Response with a 400 HTTP header and a given message. |
156
|
|
|
* |
157
|
|
|
* @return JsonResponse |
158
|
|
|
*/ |
159
|
|
|
public function errorWrongArgs($message = 'Wrong Arguments') |
160
|
|
|
{ |
161
|
|
|
return $this->setStatusCode(400) |
162
|
|
|
->respondWithError($message, self::CODE_WRONG_ARGS); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.