CampaignController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 211
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

5 Methods

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

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\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 View Code Duplication
class CampaignController 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="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
    public function cgetAction(Request $request)
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"="Show an element"
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
    public function getAction(Request $request)
101
    {
102
        $manager = $this->get("starkerxp_campaign.manager.campaign");
103
        try {
104
            $options = $this->resolveParams()->resolve($request->query->all());
105
            /** @var Campaign $entite */
106
            if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) {
107
                return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
108
            }
109
        } catch (\Exception $e) {
110
            return new JsonResponse(["payload" => $e->getMessage()], 400);
111
        }
112
        $retour = $manager->toArray($entite, $this->getFields($options['fields']));
113
114
        return new JsonResponse($retour);
115
    }
116
117
    /**
118
     * @ApiDoc(
119
     *      resource=true,
120
     *      description="Add campaign.",
121
     *      section="Campaign",
122
     *      views = {"default"}
123
     * )
124
     */
125
    public function postAction(Request $request)
126
    {
127
        $manager = $this->get("starkerxp_campaign.manager.campaign");
128
        try {
129
            $entite = new Campaign();
130
            $form = $this->createForm(CampaignType::class, $entite, ['method' => 'POST']);
131
            $form->submit($this->getRequestData($request));
132
            if ($form->isValid()) {
133
                $entite = $form->getData();
134
                $manager->insert($entite);
135
                $this->dispatch(Events::CAMPAIGN_CREATED, new GenericEvent($entite));
136
137
                return new JsonResponse(["payload" => $this->translate("entity.created", "campaign"), "token" => $entite->getId()], 201);
138
            }
139
        } catch (\Exception $e) {
140
            $manager->rollback();
141
142
            return new JsonResponse(["payload" => $e->getMessage()], 400);
143
        }
144
145
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
146
    }
147
148
    /**
149
     * @ApiDoc(
150
     *      resource=true,
151
     *      description="Edit campaign.",
152
     *      section="Campaign",
153
     *      requirements={
154
     *          {
155
     *              "name"="campaign_id",
156
     *              "dataType"="integer",
157
     *              "requirement"="\d+",
158
     *              "description"="Edit an element."
159
     *          }
160
     *      },
161
     *      views = { "default" }
162
     * )
163
     */
164
    public function putAction(Request $request)
165
    {
166
        $manager = $this->get("starkerxp_campaign.manager.campaign");
167
        if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) {
168
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
169
        }
170
        $manager->beginTransaction();
171
        try {
172
            $form = $this->createForm(CampaignType::class, $entite, ['method' => 'PUT']);
173
            $form->submit($this->getRequestData($request), false);
174
            if ($form->isValid()) {
175
                $entite = $form->getData();
176
                $manager->update($entite);
177
                $this->dispatch(Events::CAMPAIGN_UPDATED, new GenericEvent($entite));
178
179
                return new JsonResponse(["payload" => $this->translate("entity.updated", "campaign")], 204);
180
            }
181
        } catch (\Exception $e) {
182
            $manager->rollback();
183
184
            return new JsonResponse(["payload" => $e->getMessage()], 400);
185
        }
186
187
        return new JsonResponse(["payload" => $this->getFormErrors($form)], 400);
188
    }
189
190
    /**
191
     * @ApiDoc(
192
     *      resource=true,
193
     *      description="Delete campaign.",
194
     *      section="Campaign",
195
     *      requirements={
196
     *          {
197
     *              "name"="campaign_id",
198
     *              "dataType"="integer",
199
     *              "requirement"="\d+",
200
     *              "description"="Delete an element."
201
     *          }
202
     *      },
203
     *      views = { "default" }
204
     * )
205
     */
206
    public function deleteAction(Request $request)
207
    {
208
        $manager = $this->get("starkerxp_campaign.manager.campaign");
209
210
        if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) {
211
            return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404);
212
        }
213
        try {
214
            $manager->delete($entite);
215
        } catch (\Exception $e) {
216
            $manager->rollback();
217
218
            return new JsonResponse(["payload" => $e->getMessage()], 400);
219
        }
220
        $this->dispatch(Events::CAMPAIGN_DELETED, new GenericEvent($request->get('campaign_id')));
221
222
        return new JsonResponse(["payload" => $this->translate("entity.deleted", "campaign")], 204);
223
    }
224
225
}
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...
226