1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace KI\UserBundle\Controller; |
4
|
|
|
|
5
|
|
|
use KI\CoreBundle\Controller\ResourceController; |
6
|
|
|
use KI\UserBundle\Entity\Pontlyvalent; |
7
|
|
|
use KI\UserBundle\Entity\User; |
8
|
|
|
use KI\UserBundle\Form\PontlyvalentType; |
9
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
|
17
|
|
|
class PontlyvalentsController extends ResourceController |
18
|
|
|
{ |
19
|
|
|
public function setContainer(ContainerInterface $container = null) |
20
|
|
|
{ |
21
|
|
|
parent::setContainer($container); |
22
|
|
|
$this->initialize('Pontlyvalent', 'User'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
private function checkPontlyvalentOpen(){ |
26
|
|
|
$lastPromo = $this->getConfig('promos.latest'); |
27
|
|
|
|
28
|
|
|
if ($this->user->getPromo() == $lastPromo) { |
29
|
|
|
throw new BadRequestHttpException('Ton tour n\'est pas encore arrivé, petit ' . $lastPromo . ' !'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if($this->getConfig('pontlyvalent.open')) { |
33
|
|
|
throw new BadRequestHttpException('Le pontlyvalent est fermé !'); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @ApiDoc( |
39
|
|
|
* resource=true, |
40
|
|
|
* description="Liste les commentaires", |
41
|
|
|
* output="KI\UserBundle\Entity\Pontlyvalent", |
42
|
|
|
* statusCodes={ |
43
|
|
|
* 200="Requête traitée avec succès", |
44
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette action", |
45
|
|
|
* 403="Pas les droits suffisants pour effectuer cette action", |
46
|
|
|
* }, |
47
|
|
|
* section="Utilisateurs" |
48
|
|
|
* ) |
49
|
|
|
* @Route("/users/pontlyvalent") |
50
|
|
|
* @Method("GET") |
51
|
|
|
*/ |
52
|
|
|
public function getPontlyvalentsAction() |
53
|
|
|
{ |
54
|
|
|
$paginateHelper = $this->get('ki_core.helper.paginate'); |
55
|
|
|
extract($paginateHelper->paginateData($this->repository)); |
56
|
|
|
|
57
|
|
|
if ($this->is('MODO') || $this->isClubMember('bde')) { |
58
|
|
|
$results = $this->repository->findBy($findBy); |
59
|
|
|
} else { |
60
|
|
|
$results = $this->repository->findBy([ |
61
|
|
|
'author' => $this->user |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->json($results); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @ApiDoc( |
70
|
|
|
* description="Liste les commentaires sur un user", |
71
|
|
|
* output="KI\UserBundle\Entity\Pontlyvalent", |
72
|
|
|
* statusCodes={ |
73
|
|
|
* 200="Requête traitée avec succès", |
74
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette action", |
75
|
|
|
* 403="Pas les droits suffisants pour effectuer cette action", |
76
|
|
|
* 404="Ressource non trouvée" |
77
|
|
|
* }, |
78
|
|
|
* section="Utilisateurs" |
79
|
|
|
* ) |
80
|
|
|
* @Route("/users/{targetUsername}/pontlyvalent") |
81
|
|
|
* @Method("GET") |
82
|
|
|
*/ |
83
|
|
|
public function getPontlyvalentAction($targetUsername) |
84
|
|
|
{ |
85
|
|
|
$this->checkPontlyvalentOpen(); |
86
|
|
|
|
87
|
|
|
$target = $this->manager->getRepository('KIUserBundle:User')->findOneByUsername($targetUsername); |
88
|
|
|
|
89
|
|
|
$pontlyvalent = $this->repository->getPontlyvalent($target, $this->user); |
90
|
|
|
|
91
|
|
|
if(count($pontlyvalent) != 1) |
92
|
|
|
throw new NotFoundHttpException(); |
93
|
|
|
|
94
|
|
|
return $this->json($pontlyvalent[0]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @ApiDoc( |
99
|
|
|
* description="Ecrit un commentaire sur quelqu'un", |
100
|
|
|
* input="KI\UserBundle\Form\PontlyvalentType", |
101
|
|
|
* output="KI\UserBundle\Entity\Pontlyvalent", |
102
|
|
|
* statusCodes={ |
103
|
|
|
* 201="Requête traitée avec succès avec création d’un document", |
104
|
|
|
* 400="La syntaxe de la requête est erronée", |
105
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette action", |
106
|
|
|
* 403="Pas les droits suffisants pour effectuer cette action", |
107
|
|
|
* }, |
108
|
|
|
* section="Utilisateurs" |
109
|
|
|
* ) |
110
|
|
|
* @Route("/users/{targetUsername}/pontlyvalent") |
111
|
|
|
* @Method("POST") |
112
|
|
|
*/ |
113
|
|
|
public function postPontlyvalentAction(Request $request, $targetUsername) |
114
|
|
|
{ |
115
|
|
|
$this->checkPontlyvalentOpen(); |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var User $target |
119
|
|
|
*/ |
120
|
|
|
$target = $this->manager->getRepository('KIUserBundle:User')->findOneByUsername($targetUsername); |
121
|
|
|
|
122
|
|
|
$targetPromo = $this->getConfig('promos.assos'); |
123
|
|
|
if ($target->getPromo() != $targetPromo) { |
124
|
|
|
throw new BadRequestHttpException('Ce n\'est pas un ' . $targetPromo . ' !'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$pontlyvalent = $this->repository->getPontlyvalent($target, $this->user); |
128
|
|
|
if (count($pontlyvalent) == 0) { |
129
|
|
|
$pontlyvalent = new Pontlyvalent(); |
130
|
|
|
$pontlyvalent->setTarget($target); |
131
|
|
|
$pontlyvalent->setAuthor($this->user); |
132
|
|
|
} else { |
133
|
|
|
$pontlyvalent = $pontlyvalent[0]; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$pontlyvalent->setDate(time()); |
137
|
|
|
|
138
|
|
|
$form = $this->createForm(PontlyvalentType::class, $pontlyvalent, ['method' => 'POST']); |
139
|
|
|
$form->handleRequest($request); |
140
|
|
|
|
141
|
|
View Code Duplication |
if ($form->isValid()) { |
|
|
|
|
142
|
|
|
$this->manager->persist($pontlyvalent); |
143
|
|
|
$this->manager->flush(); |
144
|
|
|
|
145
|
|
|
return $this->json($pontlyvalent, 201); |
146
|
|
|
} else { |
147
|
|
|
$this->manager->detach($pontlyvalent); |
148
|
|
|
return $this->json($form, 400); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @ApiDoc( |
154
|
|
|
* description="Supprime un commentaire", |
155
|
|
|
* input="KI\UserBundle\Form\PontlyvalentType", |
156
|
|
|
* statusCodes={ |
157
|
|
|
* 204="Requête traitée avec succès mais pas d’information à renvoyer", |
158
|
|
|
* 400="La syntaxe de la requête est erronée", |
159
|
|
|
* 401="Une authentification est nécessaire pour effectuer cette action", |
160
|
|
|
* 403="Pas les droits suffisants pour effectuer cette action", |
161
|
|
|
* 404="Ressource non trouvée", |
162
|
|
|
* 503="Service temporairement indisponible ou en maintenance", |
163
|
|
|
* }, |
164
|
|
|
* section="Utilisateurs" |
165
|
|
|
* ) |
166
|
|
|
* @Route("/users/{targetUsername}/pontlyvalent") |
167
|
|
|
* @Method("DELETE") |
168
|
|
|
*/ |
169
|
|
|
public function deletePontlyvalentAction($targetUsername) |
170
|
|
|
{ |
171
|
|
|
$this->checkPontlyvalentOpen(); |
172
|
|
|
|
173
|
|
|
$target = $this->manager->getRepository('KIUserBundle:User')->findOneByUsername($targetUsername); |
174
|
|
|
|
175
|
|
|
$pontlyvalent = $this->repository->getPontlyvalent($target, $this->user); |
176
|
|
|
|
177
|
|
|
if (count($pontlyvalent) != 1) { |
178
|
|
|
throw new NotFoundHttpException('Commentaire non trouvé'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
$this->manager->remove($pontlyvalent[0]); |
182
|
|
|
$this->manager->flush(); |
183
|
|
|
|
184
|
|
|
return $this->json(null, 204); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
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.