|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace KI\CoreBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use KI\CoreBundle\Entity\Likeable; |
|
6
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
7
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
9
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
10
|
|
|
|
|
11
|
|
|
// Fonctions de like/dislike/commentaire |
|
12
|
|
|
class LikeableController extends BaseController |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Précise si une classe peut être likée par l'utilisateur actuel |
|
16
|
|
|
* @param mixed $item L'item à tester |
|
17
|
|
|
* @return boolean |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function isLikeable($item) |
|
20
|
|
|
{ |
|
21
|
|
|
if (!$this->is('USER') || $this->is('ADMISSIBLE') || $this->is('EXTERIEUR')) { |
|
22
|
|
|
throw new AccessDeniedException('Accès refusé'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if (!$item instanceof Likeable) { |
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Sert à initialiser le controleur avec la bonne classe quand il est appelé |
|
32
|
|
|
* par les routes génériques de like. Par exemple on veut l'initialiser |
|
33
|
|
|
* avec la classe Newsitems si la route est /newsitems/{slug}/like |
|
34
|
|
|
* @param string $object Le type d'objet à initialiser |
|
35
|
|
|
* @throws Exception Si l'objet ne correspond à aucun entité likeable connue |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function autoInitialize($object) |
|
38
|
|
|
{ |
|
39
|
|
|
$likeables = $this->getParameter('likeables'); |
|
40
|
|
|
$className = ucfirst(preg_replace('/s$/', '', $object)); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($likeables as $bundle => $classes) { |
|
43
|
|
|
if (gettype($classes) != 'array') { |
|
44
|
|
|
continue; |
|
45
|
|
|
} |
|
46
|
|
|
foreach ($classes as $class) { |
|
47
|
|
|
if ($class === $className) { |
|
48
|
|
|
$bundle = ucfirst($bundle); |
|
49
|
|
|
return $this->initialize($class, $bundle); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
throw new \Exception('Initialisation impossible du controleur'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Marque un objet likeable comme liké |
|
58
|
|
|
* @param LikeClass $item |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
protected function like($item) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->isLikeable($item); |
|
63
|
|
|
|
|
64
|
|
|
// Si l'utilisateur n'a pas déjà liké cet objet on le rajoute |
|
65
|
|
|
if (!$item->isLiked($this->user)) { |
|
66
|
|
|
$item->addLike($this->user); |
|
67
|
|
|
$item->setLike(true); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Si l'utilisateur avait précédemment unliké, on l'enlève |
|
71
|
|
|
if ($item->isDisliked($this->user)) { |
|
72
|
|
|
$item->removeDislike($this->user); |
|
73
|
|
|
$item->setDislike(false); |
|
74
|
|
|
} |
|
75
|
|
|
$this->manager->flush(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Marque un objet likeable comme disliké |
|
80
|
|
|
* @param LikeClass $item |
|
81
|
|
|
*/ |
|
82
|
|
View Code Duplication |
protected function dislike($item) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->isLikeable($item); |
|
85
|
|
|
|
|
86
|
|
|
// Si l'utilisateur n'a pas déjà unliké cet objet on le rajoute |
|
87
|
|
|
if (!$item->isDisliked($this->user)) { |
|
88
|
|
|
$item->addDislike($this->user); |
|
89
|
|
|
$item->setDislike(true); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Si l'utilisateur avait précédemment liké, on l'enlève |
|
93
|
|
|
if ($item->isLiked($this->user)) { |
|
94
|
|
|
$item->removeLike($this->user); |
|
95
|
|
|
$item->setLike(false); |
|
96
|
|
|
} |
|
97
|
|
|
$this->manager->flush(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Marque un objet likeable comme non liké |
|
102
|
|
|
* @param LikeClass $item |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function deleteLike($item) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->isLikeable($item); |
|
107
|
|
|
|
|
108
|
|
|
// Si l'utilisateur a déjà unliké on l'enlève |
|
109
|
|
|
if ($item->isLiked($this->user)) { |
|
110
|
|
|
$item->removeLike($this->user); |
|
111
|
|
|
$item->setLike(false); |
|
112
|
|
|
} |
|
113
|
|
|
$this->manager->flush(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Marque un objet likeable comme non disliké |
|
118
|
|
|
* @param LikeClass $item |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function deleteDislike($item) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->isLikeable($item); |
|
123
|
|
|
|
|
124
|
|
|
// Si l'utilisateur a déjà unliké on l'enlève |
|
125
|
|
|
if ($item->isDisliked($this->user)) { |
|
126
|
|
|
$item->removeDislike($this->user); |
|
127
|
|
|
$item->setDislike(false); |
|
128
|
|
|
} |
|
129
|
|
|
$this->manager->flush(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @ApiDoc( |
|
134
|
|
|
* description="Like", |
|
135
|
|
|
* statusCodes={ |
|
136
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
137
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
138
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
139
|
|
|
* 404="Ressource non trouvée", |
|
140
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
141
|
|
|
* }, |
|
142
|
|
|
* section="Likeable" |
|
143
|
|
|
* ) |
|
144
|
|
|
* @Route("/{object}/{slug}/like") |
|
145
|
|
|
* @Method("POST") |
|
146
|
|
|
*/ |
|
147
|
|
|
public function likeAction($object, $slug) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->autoInitialize($object); |
|
150
|
|
|
$this->like($this->findBySlug($slug)); |
|
151
|
|
|
return $this->json(null, 204); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @ApiDoc( |
|
156
|
|
|
* description="Dislike", |
|
157
|
|
|
* statusCodes={ |
|
158
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
159
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
160
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
161
|
|
|
* 404="Ressource non trouvée", |
|
162
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
163
|
|
|
* }, |
|
164
|
|
|
* section="Likeable" |
|
165
|
|
|
* ) |
|
166
|
|
|
* @Route("/{object}/{slug}/dislike") |
|
167
|
|
|
* @Method("POST") |
|
168
|
|
|
*/ |
|
169
|
|
|
public function dislikeAction($object, $slug) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->autoInitialize($object); |
|
172
|
|
|
$this->dislike($this->findBySlug($slug)); |
|
173
|
|
|
return $this->json(null, 204); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @ApiDoc( |
|
178
|
|
|
* description="Enlève son like", |
|
179
|
|
|
* statusCodes={ |
|
180
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
181
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
182
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
183
|
|
|
* 404="Ressource non trouvée", |
|
184
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
185
|
|
|
* }, |
|
186
|
|
|
* section="Likeable" |
|
187
|
|
|
* ) |
|
188
|
|
|
* @Route("/{object}/{slug}/like") |
|
189
|
|
|
* @Method("DELETE") |
|
190
|
|
|
*/ |
|
191
|
|
|
public function deleteLikeAction($object, $slug) |
|
192
|
|
|
{ |
|
193
|
|
|
$this->autoInitialize($object); |
|
194
|
|
|
$this->deleteLike($this->findBySlug($slug)); |
|
195
|
|
|
return $this->json(null, 204); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @ApiDoc( |
|
200
|
|
|
* description="Enlève son dislike", |
|
201
|
|
|
* statusCodes={ |
|
202
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
203
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
204
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
205
|
|
|
* 404="Ressource non trouvée", |
|
206
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
207
|
|
|
* }, |
|
208
|
|
|
* section="Likeable" |
|
209
|
|
|
* ) |
|
210
|
|
|
* @Route("/{object}/{slug}/dislike") |
|
211
|
|
|
* @Method("DELETE") |
|
212
|
|
|
*/ |
|
213
|
|
|
public function deleteDislikeAction($object, $slug) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->autoInitialize($object); |
|
216
|
|
|
$this->deleteDislike($this->findBySlug($slug)); |
|
217
|
|
|
return $this->json(null, 204); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @ApiDoc( |
|
222
|
|
|
* description="Like une sous ressource", |
|
223
|
|
|
* statusCodes={ |
|
224
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
225
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
226
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
227
|
|
|
* 404="Ressource non trouvée", |
|
228
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
229
|
|
|
* }, |
|
230
|
|
|
* section="Likeable" |
|
231
|
|
|
* ) |
|
232
|
|
|
* @Route("/{object}/{slug}/{subobject}/{subslug}/like") |
|
233
|
|
|
* @Method("POST") |
|
234
|
|
|
*/ |
|
235
|
|
|
public function likeSubAction($object, $slug, $subobject, $subslug) |
|
|
|
|
|
|
236
|
|
|
{ |
|
237
|
|
|
$this->autoInitialize($subobject); |
|
238
|
|
|
$this->like($this->findBySlug($subslug)); |
|
239
|
|
|
return $this->json(null, 204); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* @ApiDoc( |
|
244
|
|
|
* description="Dislike une sous ressource", |
|
245
|
|
|
* statusCodes={ |
|
246
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
247
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
248
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
249
|
|
|
* 404="Ressource non trouvée", |
|
250
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
251
|
|
|
* }, |
|
252
|
|
|
* section="Likeable" |
|
253
|
|
|
* ) |
|
254
|
|
|
* @Route("/{object}/{slug}/{subobject}/{subslug}/dislike") |
|
255
|
|
|
* @Method("POST") |
|
256
|
|
|
*/ |
|
257
|
|
|
public function dislikeSubAction($object, $slug, $subobject, $subslug) |
|
|
|
|
|
|
258
|
|
|
{ |
|
259
|
|
|
$this->autoInitialize($subobject); |
|
260
|
|
|
$this->dislike($this->findBySlug($subslug)); |
|
261
|
|
|
return $this->json(null, 204); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @ApiDoc( |
|
266
|
|
|
* description="Enlève son like d'une sous ressource", |
|
267
|
|
|
* statusCodes={ |
|
268
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
269
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
270
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
271
|
|
|
* 404="Ressource non trouvée", |
|
272
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
273
|
|
|
* }, |
|
274
|
|
|
* section="Likeable" |
|
275
|
|
|
* ) |
|
276
|
|
|
* @Route("/{object}/{slug}/{subobject}/{subslug}/like") |
|
277
|
|
|
* @Method("DELETE") |
|
278
|
|
|
*/ |
|
279
|
|
|
public function deleteLikeSubAction($object, $slug, $subobject, $subslug) |
|
|
|
|
|
|
280
|
|
|
{ |
|
281
|
|
|
$this->autoInitialize($subobject); |
|
282
|
|
|
$this->deleteLike($this->findBySlug($subslug)); |
|
283
|
|
|
return $this->json(null, 204); |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @ApiDoc( |
|
288
|
|
|
* description="Enlève son dislike d'une sous ressource", |
|
289
|
|
|
* statusCodes={ |
|
290
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
|
291
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette View", |
|
292
|
|
|
* 403="Pas les droits suffisants pour effectuer cette View", |
|
293
|
|
|
* 404="Ressource non trouvée", |
|
294
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
|
295
|
|
|
* }, |
|
296
|
|
|
* section="Likeable" |
|
297
|
|
|
* ) |
|
298
|
|
|
* @Route("/{object}/{slug}/{subobject}/{subslug}/dislike") |
|
299
|
|
|
* @Method("DELETE") |
|
300
|
|
|
*/ |
|
301
|
|
|
public function deleteDislikeSubAction($object, $slug, $subobject, $subslug) |
|
|
|
|
|
|
302
|
|
|
{ |
|
303
|
|
|
$this->autoInitialize($subobject); |
|
304
|
|
|
$this->deleteDislike($this->findBySlug($subslug)); |
|
305
|
|
|
return $this->json(null, 204); |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.