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

CampaignController::putAction()   B

Complexity

Conditions 4
Paths 10

Size

Total Lines 25
Code Lines 17

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 10
nop 1
1
<?php
2
3
namespace Starkerxp\CampaignBundle\Controller;
4
5
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
6
use Starkerxp\CampaignBundle\Entity\Campaign;
7
use Starkerxp\CampaignBundle\Events;
8
use Starkerxp\CampaignBundle\Form\Type\CampaignType;
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 CampaignController extends StructureController
16
{
17
	/**
18
     * @ApiDoc(
19
     *      resource=true,
20
     *      description="Show campaigns 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 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...
56
    {
57
        $manager = $this->get("starkerxp_campaign.manager.campaign");
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
        $retour = array_map(
66
            function ($element) use ($manager, $options) {
67
                return $manager->toArray($element, $this->getFields($options['fields']));
68
            },
69
            $resultSets
70
        );
71
72
        return new JsonResponse($retour);
73
    }
74
75
    /**
76
     * @ApiDoc(
77
     *      resource=true,
78
     *      description="Show campaign.",
79
     *      section="Campaign",
80
     *      requirements={
81
     *          {
82
     *              "name"="campaign_id",
83
     *              "dataType"="integer",
84
     *              "requirement"="\d+",
85
     *              "description"="Permet d'afficher l'élément choisis"
86
     *          }
87
     *      },
88
     *      parameters={
89
     *          {
90
     *              "name"="fields",
91
     *              "dataType"="string",
92
     *              "requirement"="\w+",
93
     *              "description"="starkerxp_structure.doc.list_field.entity",
94
     *              "required"="false"
95
     *          }
96
     *      },
97
     *      views = { "default" }
98
     * )
99
     */
100 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...
101
    {
102
        $manager = $this->get("starkerxp_campaign.manager.campaign");
103
        try {
104
            $options = $this->resolveParams()->resolve($request->query->all());
105
            if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) {
106
                return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
107
            }
108
        } catch (\Exception $e) {
109
            return new JsonResponse(["payload" => $e->getMessage()], 400);
110
        }
111
        $retour = $manager->toArray($entite, $this->getFields($options['fields']));
112
113
        return new JsonResponse($retour);
114
    }
115
116
    /**
117
     * @ApiDoc(
118
     *      resource=true,
119
     *      description="Add campaign.",
120
     *      section="Campaign",
121
     *      views = {"default"}
122
     * )
123
     */
124
    public function postAction(Request $request)
125
    {
126
        $manager = $this->get("starkerxp_campaign.manager.campaign");
127
        try {
128
            $entite = new Campaign();
129
            $form = $this->createForm(CampaignType::class, $entite, ['method' => 'POST']);
130
            $form->submit($this->getRequestData($request));
131
            if ($form->isValid()) {
132
                $entite = $form->getData();
133
                $manager->insert($entite);
134
                $this->dispatch(Events::CAMPAIGN_CREATED, new GenericEvent($entite));
135
136
                return new JsonResponse(["payload" => $this->translate("entity.created", "campaign"), "token" => $entite->getId()], 201);
137
            }
138
        } catch (\Exception $e) {
139
            $manager->rollback();
140
141
            return new JsonResponse(["payload" => $e->getMessage()], 400);
142
        }
143
144
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
145
    }
146
147
    /**
148
     * @ApiDoc(
149
     *      resource=true,
150
     *      description="Edit campaign.",
151
     *      section="Campaign",
152
     *      requirements={
153
     *          {
154
     *              "name"="campaign_id",
155
     *              "dataType"="integer",
156
     *              "requirement"="\d+",
157
     *              "description"="Permet de modifier l'élément choisi."
158
     *          }
159
     *      },
160
     *      views = { "default" }
161
     * )
162
     */
163 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...
164
    {
165
        $manager = $this->get("starkerxp_campaign.manager.campaign");
166
        if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) {
167
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
168
        }
169
        $manager->beginTransaction();
170
        try {
171
            $form = $this->createForm(CampaignType::class, $entite, ['method' => 'PUT']);
172
            $form->submit($this->getRequestData($request));
173
            if ($form->isValid()) {
174
                $entite = $form->getData();
175
                $manager->update($entite);
176
                $this->dispatch(Events::CAMPAIGN_UPDATED, new GenericEvent($entite));
177
178
                return new JsonResponse(["payload" => $this->translate("entity.updated", "campaign")], 204);
179
            }
180
        } catch (\Exception $e) {
181
            $manager->rollback();
182
183
            return new JsonResponse(["payload" => $e->getMessage()], 400);
184
        }
185
186
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
187
    }
188
189
    /**
190
     * @ApiDoc(
191
     *      resource=true,
192
     *      description="Delete campaign.",
193
     *      section="Campaign",
194
     *      requirements={
195
     *          {
196
     *              "name"="campaign_id",
197
     *              "dataType"="integer",
198
     *              "requirement"="\d+",
199
     *              "description"="Permet de supprimer l'élément choisi."
200
     *          }
201
     *      },
202
     *      views = { "default" }
203
     * )
204
     */
205 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...
206
    {
207
        $manager = $this->get("starkerxp_campaign.manager.campaign");
208
        if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) {
209
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
210
        }
211
        try {
212
            $manager->delete($entite);
213
        } catch (\Exception $e) {
214
            $manager->rollback();
215
216
            return new JsonResponse(["payload" => $e->getMessage()], 400);
217
        }
218
        $this->dispatch(Events::CAMPAIGN_DELETED, new GenericEvent($request->get('campaign_id')));
219
220
        return new JsonResponse(["payload" => $this->translate("entity.deleted", "campaign")], 204);
221
    }
222
223
} 
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...
224