TemplateController   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 215
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 8
dl 215
loc 215
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A cgetAction() 22 22 3
A getAction() 16 16 3
A postAction() 22 22 3
A deleteAction() 17 17 3
B putAction() 25 25 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class TemplateController extends StructureController
0 ignored issues
show
Duplication introduced by
This class 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...
16
{
17
    /**
18
     * @ApiDoc(
19
     *      resource=true,
20
     *      description="List templates.",
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
     */
56
    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
76
        return new JsonResponse($retour);
77
    }
78
79
80
    /**
81
     * @ApiDoc(
82
     *      resource=true,
83
     *      description="Affiche un template.",
84
     *      section="Campaign",
85
     *      requirements={
86
     *          {
87
     *              "name"="template_id",
88
     *              "dataType"="integer",
89
     *              "requirement"="\d+",
90
     *              "description"="Show an element"
91
     *          }
92
     *      },
93
     *      parameters={
94
     *          {
95
     *              "name"="fields",
96
     *              "dataType"="string",
97
     *              "requirement"="\w+",
98
     *              "description"="starkerxp_structure.doc.list_field.entity",
99
     *              "required"="false"
100
     *          }
101
     *      },
102
     *      views = { "default" }
103
     * )
104
     */
105
    public function getAction(Request $request)
106
    {
107
        $manager = $this->get("starkerxp_campaign.manager.template");
108
        try {
109
            $options = $this->resolveParams()->resolve($request->query->all());
110
            if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) {
111
                return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404);
112
            }
113
        } catch (\Exception $e) {
114
            return new JsonResponse(["payload" => $e->getMessage()], 400);
115
        }
116
117
        $retour = $manager->toArray($entite, $this->getFields($options['fields']));
118
119
        return new JsonResponse($retour);
120
    }
121
122
    /**
123
     * @ApiDoc(
124
     *      resource=true,
125
     *      description="Ajoute un template.",
126
     *      section="Campaign",
127
     *      views = { "default" }
128
     * )
129
     */
130
    public function postAction(Request $request)
131
    {
132
        $manager = $this->get("starkerxp_campaign.manager.template");
133
        try {
134
            $entite = new Template();
135
            $form = $this->createForm(TemplateType::class, $entite, ['method' => 'POST']);
136
            $form->submit($this->getRequestData($request));
137
            if ($form->isValid()) {
138
                $template = $form->getData();
139
                $manager->insert($template);
140
                $this->dispatch(Events::TEMPLATE_CREATED, new GenericEvent($entite));
141
142
                return new JsonResponse(["payload" => $this->translate("entity.created", "template")], 201);
143
            }
144
        } catch (\Exception $e) {
145
            $manager->rollback();
146
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="Modifie un template.",
157
     *      section="Campaign",
158
     *      requirements={
159
     *          {
160
     *              "name"="template_id",
161
     *              "dataType"="integer",
162
     *              "requirement"="\d+",
163
     *              "description"="Edit an element."
164
     *          }
165
     *      },
166
     *      views = { "default" }
167
     * )
168
     */
169
    public function putAction(Request $request)
170
    {
171
        $manager = $this->get("starkerxp_campaign.manager.template");
172
        if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) {
173
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404);
174
        }
175
        $manager->beginTransaction();
176
        try {
177
            $form = $this->createForm(TemplateType::class, $entite, ['method' => 'PUT']);
178
            $form->submit($this->getRequestData($request), false);
179
            if ($form->isValid()) {
180
                $entite = $form->getData();
181
                $manager->update($entite);
182
                $this->dispatch(Events::TEMPLATE_UPDATED, new GenericEvent($entite));
183
184
                return new JsonResponse(["payload" => $this->translate("entity.updated", "template")], 204);
185
            }
186
        } catch (\Exception $e) {
187
            $manager->rollback();
188
189
            return new JsonResponse(["payload" => $e->getMessage()], 400);
190
        }
191
192
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
193
    }
194
195
    /**
196
     * @ApiDoc(
197
     *      resource=true,
198
     *      description="Delete a template.",
199
     *      section="Campaign",
200
     *      requirements={
201
     *          {
202
     *              "name"="template_id",
203
     *              "dataType"="integer",
204
     *              "requirement"="\d+",
205
     *              "description"="Delete an element."
206
     *          }
207
     *      },
208
     *      views = { "default" }
209
     * )
210
     */
211
    public function deleteAction(Request $request)
212
    {
213
        $manager = $this->get("starkerxp_campaign.manager.template");
214
        if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) {
215
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404);
216
        }
217
        try {
218
            $manager->delete($entite);
219
        } catch (\Exception $e) {
220
            $manager->rollback();
221
222
            return new JsonResponse(["payload" => $e->getMessage()], 400);
223
        }
224
        $this->dispatch(Events::TEMPLATE_DELETED, new GenericEvent($request->get('template_id')));
225
226
        return new JsonResponse(["payload" => $this->translate("entity.deleted", "template")], 204);
227
    }
228
229
}
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...
230