1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\LeadBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
6
|
|
|
use Starkerxp\LeadBundle\Entity\LeadAction; |
7
|
|
|
use Starkerxp\LeadBundle\Events; |
8
|
|
|
use Starkerxp\LeadBundle\Form\Type\LeadType; |
9
|
|
|
use Starkerxp\StructureBundle\Controller\StructureController; |
10
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
11
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
|
14
|
|
|
|
15
|
|
View Code Duplication |
class LeadController extends StructureController |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @ApiDoc( |
19
|
|
|
* resource=true, |
20
|
|
|
* description="Liste les leads.", |
21
|
|
|
* section="starkerxp_lead.lead", |
22
|
|
|
* parameters={ |
23
|
|
|
* { |
24
|
|
|
* "name"="offset", |
25
|
|
|
* "dataType"="integer", |
26
|
|
|
* "requirement"="\d+", |
27
|
|
|
* "description"="starkerxp_structure.doc.offset.result", |
28
|
|
|
* "required"="false" |
29
|
|
|
* }, |
30
|
|
|
* { |
31
|
|
|
* "name"="limit", |
32
|
|
|
* "dataType"="integer", |
33
|
|
|
* "requirement"="\d+", |
34
|
|
|
* "description"="starkerxp_structure.doc.limit.result", |
35
|
|
|
* "required"="false" |
36
|
|
|
* }, |
37
|
|
|
* { |
38
|
|
|
* "name"="fields", |
39
|
|
|
* "dataType"="string", |
40
|
|
|
* "requirement"="\w+", |
41
|
|
|
* "description"="starkerxp_structure.doc.list_field.entity", |
42
|
|
|
* "required"="false" |
43
|
|
|
* }, |
44
|
|
|
* { |
45
|
|
|
* "name"="sort", |
46
|
|
|
* "dataType"="string", |
47
|
|
|
* "requirement"="\w+", |
48
|
|
|
* "description"="starkerxp_structure.doc.sort.result", |
49
|
|
|
* "required"="false" |
50
|
|
|
* } |
51
|
|
|
* }, |
52
|
|
|
* views = { "default" } |
53
|
|
|
* ) |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
|
public function cgetAction(Request $request) |
57
|
|
|
{ |
58
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
59
|
|
|
try { |
60
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
61
|
|
|
$orderBy = $this->getOrderBy($options['sort']); |
62
|
|
|
$resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
63
|
|
|
} catch (\Exception $e) { |
64
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
65
|
|
|
} |
66
|
|
|
if (empty($resultSets)) { |
67
|
|
|
return new JsonResponse([]); |
68
|
|
|
} |
69
|
|
|
$retour = array_map( |
70
|
|
|
function ($element) use ($manager, $options) { |
71
|
|
|
return $manager->toArray($element, $this->getFields($options['fields'])); |
72
|
|
|
}, |
73
|
|
|
$resultSets |
74
|
|
|
); |
75
|
|
|
return new JsonResponse($retour); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @ApiDoc( |
81
|
|
|
* resource=true, |
82
|
|
|
* description="Affiche un lead.", |
83
|
|
|
* section="starkerxp_lead.lead", |
84
|
|
|
* requirements={ |
85
|
|
|
* { |
86
|
|
|
* "name"="lead_id", |
87
|
|
|
* "dataType"="integer", |
88
|
|
|
* "requirement"="\d+", |
89
|
|
|
* "description"="Permet d'afficher l'élément choisis" |
90
|
|
|
* } |
91
|
|
|
* }, |
92
|
|
|
* parameters={ |
93
|
|
|
* { |
94
|
|
|
* "name"="fields", |
95
|
|
|
* "dataType"="string", |
96
|
|
|
* "requirement"="\w+", |
97
|
|
|
* "description"="starkerxp_structure.doc.list_field.entity", |
98
|
|
|
* "required"="false" |
99
|
|
|
* } |
100
|
|
|
* }, |
101
|
|
|
* views = { "default" } |
102
|
|
|
* ) |
103
|
|
|
*/ |
104
|
|
|
public function getAction(Request $request) |
105
|
|
|
{ |
106
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
107
|
|
|
try { |
108
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
109
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) { |
110
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
111
|
|
|
} |
112
|
|
|
} catch (\Exception $e) { |
113
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$retour = $manager->toArray($entite, $this->getFields($options['fields'])); |
117
|
|
|
|
118
|
|
|
return new JsonResponse($retour); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @ApiDoc( |
123
|
|
|
* resource=true, |
124
|
|
|
* description="Ajoute un lead.", |
125
|
|
|
* section="starkerxp_lead.lead", |
126
|
|
|
* views = { "default" } |
127
|
|
|
* ) |
128
|
|
|
*/ |
129
|
|
|
public function postAction(Request $request) |
130
|
|
|
{ |
131
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
132
|
|
|
try { |
133
|
|
|
$entite = new Lead(); |
134
|
|
|
$form = $this->createForm(LeadType::class, $entite, ['method' => 'POST']); |
135
|
|
|
$form->submit($this->getRequestData($request)); |
136
|
|
|
if ($form->isValid()) { |
137
|
|
|
$lead = $form->getData(); |
138
|
|
|
$lead->setUuid($this->getUuid()); |
139
|
|
|
$manager->insert($lead); |
140
|
|
|
$this->dispatch(Events::LEAD_CREATED, new GenericEvent($entite)); |
141
|
|
|
return new JsonResponse(["payload" => $this->translate("leadaction.entity.created", "lead")], 201); |
142
|
|
|
} |
143
|
|
|
} catch (\Exception $e) { |
144
|
|
|
$manager->rollback(); |
145
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @ApiDoc( |
153
|
|
|
* resource=true, |
154
|
|
|
* description="Modifie un lead.", |
155
|
|
|
* section="starkerxp_lead.lead", |
156
|
|
|
* requirements={ |
157
|
|
|
* { |
158
|
|
|
* "name"="lead_id", |
159
|
|
|
* "dataType"="integer", |
160
|
|
|
* "requirement"="\d+", |
161
|
|
|
* "description"="Permet de modifier l'élément choisi." |
162
|
|
|
* } |
163
|
|
|
* }, |
164
|
|
|
* views = { "default" } |
165
|
|
|
* ) |
166
|
|
|
*/ |
167
|
|
|
public function putAction(Request $request) |
168
|
|
|
{ |
169
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
170
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) { |
171
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
172
|
|
|
} |
173
|
|
|
$manager->beginTransaction(); |
174
|
|
|
try { |
175
|
|
|
$form = $this->createForm(LeadType::class, $entite, ['method' => 'PUT']); |
176
|
|
|
$form->submit($this->getRequestData($request)); |
177
|
|
|
if ($form->isValid()) { |
178
|
|
|
$entite = $form->getData(); |
179
|
|
|
$manager->update($lead); |
|
|
|
|
180
|
|
|
$this->dispatch(Events::LEAD_UPDATED, new GenericEvent($entite)); |
181
|
|
|
return new JsonResponse(["payload" => $this->translate("leadaction.entity.updated", "lead")], 204); |
182
|
|
|
} |
183
|
|
|
} catch (\Exception $e) { |
184
|
|
|
$manager->rollback(); |
185
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
186
|
|
|
} |
187
|
|
|
return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @ApiDoc( |
192
|
|
|
* resource=true, |
193
|
|
|
* description="Supprime un lead.", |
194
|
|
|
* section="starkerxp_lead.lead", |
195
|
|
|
* requirements={ |
196
|
|
|
* { |
197
|
|
|
* "name"="lead_id", |
198
|
|
|
* "dataType"="integer", |
199
|
|
|
* "requirement"="\d+", |
200
|
|
|
* "description"="Permet de supprimer l'élément choisi." |
201
|
|
|
* } |
202
|
|
|
* }, |
203
|
|
|
* views = { "default" } |
204
|
|
|
* ) |
205
|
|
|
*/ |
206
|
|
|
public function deleteAction(Request $request) |
207
|
|
|
{ |
208
|
|
|
$manager = $this->get("starkerxp_lead.manager.lead"); |
209
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('lead_id')])) { |
210
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "lead")], 404); |
211
|
|
|
} |
212
|
|
|
try { |
213
|
|
|
$manager->delete($entite); |
214
|
|
|
} catch (\Exception $e) { |
215
|
|
|
$manager->rollback(); |
216
|
|
|
return new JsonResponse(["payload" => $e->getMessage()], 400); |
217
|
|
|
} |
218
|
|
|
$this->dispatch(Events::LEAD_DELETED, new GenericEvent($request->get('lead_id'))); |
219
|
|
|
|
220
|
|
|
return new JsonResponse(["payload" => $this->translate("leadaction.entity.deleted", "lead")], 204); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
} |
|
|
|
|
224
|
|
|
|
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.