1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\CampaignBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
6
|
|
|
use Starkerxp\CampaignBundle\Entity\Template; |
7
|
|
|
use Starkerxp\CampaignBundle\Events; |
8
|
|
|
use Starkerxp\CampaignBundle\Form\Type\TemplateType; |
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
|
|
|
class TemplateController extends StructureController |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @ApiDoc( |
19
|
|
|
* resource=true, |
20
|
|
|
* description="List templates.", |
21
|
|
|
* section="starkerxp_campaign.template", |
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
|
|
View Code Duplication |
public function cgetAction(Request $request) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$manager = $this->get("starkerxp_campaign.manager.template"); |
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 template.", |
83
|
|
|
* section="starkerxp_campaign.template", |
84
|
|
|
* requirements={ |
85
|
|
|
* { |
86
|
|
|
* "name"="template_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
|
|
View Code Duplication |
public function getAction(Request $request) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$manager = $this->get("starkerxp_campaign.manager.template"); |
107
|
|
|
try { |
108
|
|
|
$options = $this->resolveParams()->resolve($request->query->all()); |
109
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) { |
110
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 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 template.", |
125
|
|
|
* section="starkerxp_campaign.template", |
126
|
|
|
* views = { "default" } |
127
|
|
|
* ) |
128
|
|
|
*/ |
129
|
|
|
public function postAction(Request $request) |
130
|
|
|
{ |
131
|
|
|
$manager = $this->get("starkerxp_campaign.manager.template"); |
132
|
|
|
try { |
133
|
|
|
$entite = new Template(); |
134
|
|
|
$form = $this->createForm(TemplateType::class, $entite, ['method' => 'POST']); |
135
|
|
|
$form->submit($this->getRequestData($request)); |
136
|
|
|
if ($form->isValid()) { |
137
|
|
|
$template = $form->getData(); |
138
|
|
|
$template->setUuid($this->getUuid()); |
139
|
|
|
$manager->insert($template); |
140
|
|
|
$this->dispatch(Events::TEMPLATE_CREATED, new GenericEvent($entite)); |
141
|
|
|
return new JsonResponse(["payload" => $this->translate("template.entity.created", "template")], 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 template.", |
155
|
|
|
* section="starkerxp_campaign.template", |
156
|
|
|
* requirements={ |
157
|
|
|
* { |
158
|
|
|
* "name"="template_id", |
159
|
|
|
* "dataType"="integer", |
160
|
|
|
* "requirement"="\d+", |
161
|
|
|
* "description"="Permet de modifier l'élément choisi." |
162
|
|
|
* } |
163
|
|
|
* }, |
164
|
|
|
* views = { "default" } |
165
|
|
|
* ) |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function putAction(Request $request) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
$manager = $this->get("starkerxp_campaign.manager.template"); |
170
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) { |
171
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404); |
172
|
|
|
} |
173
|
|
|
$manager->beginTransaction(); |
174
|
|
|
try { |
175
|
|
|
$form = $this->createForm(TemplateType::class, $entite, ['method' => 'PUT']); |
176
|
|
|
$form->submit($this->getRequestData($request)); |
177
|
|
|
if ($form->isValid()) { |
178
|
|
|
$entite = $form->getData(); |
179
|
|
|
$manager->update($entite); |
180
|
|
|
$this->dispatch(Events::TEMPLATE_UPDATED, new GenericEvent($entite)); |
181
|
|
|
return new JsonResponse(["payload" => $this->translate("template.entity.updated", "template")], 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 template.", |
194
|
|
|
* section="starkerxp_campaign.template", |
195
|
|
|
* requirements={ |
196
|
|
|
* { |
197
|
|
|
* "name"="template_id", |
198
|
|
|
* "dataType"="integer", |
199
|
|
|
* "requirement"="\d+", |
200
|
|
|
* "description"="Permet de supprimer l'élément choisi." |
201
|
|
|
* } |
202
|
|
|
* }, |
203
|
|
|
* views = { "default" } |
204
|
|
|
* ) |
205
|
|
|
*/ |
206
|
|
View Code Duplication |
public function deleteAction(Request $request) |
|
|
|
|
207
|
|
|
{ |
208
|
|
|
$manager = $this->get("starkerxp_campaign.manager.template"); |
209
|
|
|
if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) { |
210
|
|
|
return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 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::TEMPLATE_DELETED, new GenericEvent($request->get('template_id'))); |
219
|
|
|
|
220
|
|
|
return new JsonResponse(["payload" => $this->translate("template.entity.deleted", "template")], 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.