Completed
Push — master ( e4a474...3a3d8d )
by Guillaume
02:43
created

EventController::getAction()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 5
nop 1
1
<?php
2
3
namespace Starkerxp\CampaignBundle\Controller;
4
5
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6
use Starkerxp\CampaignBundle\Entity\Event;
7
use Starkerxp\CampaignBundle\Events;
8
use Starkerxp\CampaignBundle\Form\Type\EventType;
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 EventController extends StructureController
16
{
17
    /**
18
     * @ApiDoc(
19
     *      resource=true,
20
     *      description="Show events list.",
21
     *      section="Campaign",
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
    public function cgetAction(Request $request)
56
    {
57
        $manager = $this->get("starkerxp_campaign.manager.event");
58
        try {
59
            $options = $this->resolveParams()->resolve($request->query->all());
60
            $orderBy = $this->getOrderBy($options['sort']);
61
            $resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']);
62
        } catch (\Exception $e) {
63
            return new JsonResponse(["payload" => $e->getMessage()], 400);
64
        }
65
        if (empty($resultSets)) {
66
            return new JsonResponse([]);
67
        }
68
        $retour = array_map(
69
            function ($element) use ($manager, $options) {
70
                return $manager->toArray($element, $this->getFields($options['fields']));
71
            },
72
            $resultSets
73
        );
74
75
        return new JsonResponse($retour);
76
    }
77
78
79
    /**
80
     * @ApiDoc(
81
     *      resource=true,
82
     *      description="Show event.",
83
     *      section="Campaign",
84
     *      requirements={
85
     *          {
86
     *              "name"="campaign_id",
87
     *              "dataType"="integer",
88
     *              "requirement"="\d+",
89
     *              "description"="Show an element"
90
     *          },
91
     *          {
92
     *              "name"="event_id",
93
     *              "dataType"="integer",
94
     *              "requirement"="\d+",
95
     *              "description"="Show an element"
96
     *          }
97
     *      },
98
     *      parameters={
99
     *          {
100
     *              "name"="fields",
101
     *              "dataType"="string",
102
     *              "requirement"="\w+",
103
     *              "description"="starkerxp_structure.doc.list_field.entity",
104
     *              "required"="false"
105
     *          }
106
     *      },
107
     *      views = { "default" }
108
     * )
109
     */
110 View Code Duplication
    public function getAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
111
    {
112
        $manager = $this->get("starkerxp_campaign.manager.event");
113
        try {
114
            $options = $this->resolveParams()->resolve($request->query->all());
115
            /** @var Event $entite */
116
            if (!$entite = $manager->findOneBy(['id' => $request->get('event_id')])) {
117
                return new JsonResponse(["payload" => $this->translate("entity.not_found", "event")], 404);
118
            }
119
        } catch (\Exception $e) {
120
            return new JsonResponse(["payload" => $e->getMessage()], 400);
121
        }
122
        $retour = $manager->toArray($entite, $this->getFields($options['fields']));
123
124
        return new JsonResponse($retour);
125
    }
126
127
    /**
128
     * @ApiDoc(
129
     *      resource=true,
130
     *      description="Ajoute un event.",
131
     *      section="Campaign",
132
     *      views = { "default" }
133
     * )
134
     */
135
    public function postAction(Request $request)
136
    {
137
        $manager = $this->get("starkerxp_campaign.manager.event");
138
        try {
139
            $event = new Event();
140
            if (!$campaign = $this->getEntityManager()->getRepository("StarkerxpCampaignBundle:Campaign")->find($request->get('campaign_id'))) {
141
                return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
142
            }
143
            $event->setCampaign($campaign);
144
            $form = $this->createForm(EventType::class, $event, ['method' => 'POST']);
145
            $form->submit($this->getRequestData($request), false);
146
            if ($form->isValid()) {
147
                $event = $form->getData();
148
                $manager->insert($event);
149
                $this->dispatch(Events::EVENT_CREATED, new GenericEvent($event));
150
151
                return new JsonResponse(["payload" => $this->translate("entity.created", "event")], 201);
152
            }
153
        } catch (\Exception $e) {
154
            $manager->rollback();
155
156
            return new JsonResponse(["payload" => $e->getMessage()], 400);
157
        }
158
159
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
160
    }
161
162
    /**
163
     * @ApiDoc(
164
     *      resource=true,
165
     *      description="Edit event.",
166
     *      section="Campaign",
167
     *      requirements={
168
     *          {
169
     *              "name"="campaign_id",
170
     *              "dataType"="integer",
171
     *              "requirement"="\d+",
172
     *              "description"="Permet de modifier un élément de la campaign"
173
     *          },
174
     *          {
175
     *              "name"="event_id",
176
     *              "dataType"="integer",
177
     *              "requirement"="\d+",
178
     *              "description"="edit an elements"
179
     *          }
180
     *      },
181
     *      views = { "default" }
182
     * )
183
     */
184
    public function putAction(Request $request)
185
    {
186
        $manager = $this->get("starkerxp_campaign.manager.event");
187
        if (!$entite = $manager->findOneBy(['id' => $request->get('event_id')])) {
188
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "event")], 404);
189
        }
190
        $manager->beginTransaction();
191
        try {
192
            $form = $this->createForm(EventType::class, $entite, ['method' => 'PUT']);
193
            $form->submit($this->getRequestData($request), false);
194
            if ($form->isValid()) {
195
                $entite = $form->getData();
196
                $manager->update($entite);
197
                $this->dispatch(Events::EVENT_UPDATED, new GenericEvent($entite));
198
199
                return new JsonResponse(["payload" => $this->translate("entity.updated", "event")], 204);
200
            }
201
        } catch (\Exception $e) {
202
            $manager->rollback();
203
204
            return new JsonResponse(["payload" => $e->getMessage()], 400);
205
        }
206
207
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
208
    }
209
210
    /**
211
     * @ApiDoc(
212
     *      resource=true,
213
     *      description="Delete event.",
214
     *      section="Campaign",
215
     *      requirements={
216
     *          {
217
     *              "name"="campaign_id",
218
     *              "dataType"="integer",
219
     *              "requirement"="\d+",
220
     *              "description"="Permet de supprimer un élément de la campaign"
221
     *          },
222
     *          {
223
     *              "name"="event_id",
224
     *              "dataType"="integer",
225
     *              "requirement"="\d+",
226
     *              "description"="Delete an elements"
227
     *          }
228
     *      },
229
     *      views = { "default" }
230
     * )
231
     */
232 View Code Duplication
    public function deleteAction(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
233
    {
234
        $manager = $this->get("starkerxp_campaign.manager.event");
235
        if (!$entite = $manager->findOneBy(['id' => $request->get('event_id')])) {
236
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "event")], 404);
237
        }
238
239
        try {
240
            $manager->delete($entite);
241
        } catch (\Exception $e) {
242
            $manager->rollback();
243
244
            return new JsonResponse(["payload" => $e->getMessage()], 400);
245
        }
246
        $this->dispatch(Events::EVENT_DELETED, new GenericEvent($request->get('event_id')));
247
248
        return new JsonResponse(["payload" => $this->translate("entity.deleted", "event")], 204);
249
    }
250
251
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
252