Completed
Push — master ( 4bb011...4b99be )
by Guillaume
13:23
created

EventController::putAction()   B

Complexity

Conditions 4
Paths 9

Size

Total Lines 22
Code Lines 17

Duplication

Lines 22
Ratio 100 %

Importance

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