Passed
Push — master ( 9fdafe...783d3e )
by Martin
02:30
created

TaskController::updateAction()   D

Complexity

Conditions 18
Paths 32

Size

Total Lines 113
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 4.7996
c 0
b 0
f 0
cc 18
eloc 75
nc 32
nop 6

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Todo\Web\FrontendBundle\Controller;
4
5
use Symfony\Component\Form\FormFactoryInterface;
6
use Symfony\Component\HttpFoundation\Request;
7
use Symfony\Component\HttpFoundation\Session\Session;
8
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
9
use Todo\Application\Task\Command;
10
use Todo\Application\Task\Exception\TaskCannotBeRemovedException;
11
use Todo\Application\Task\Exception\TaskCannotBeSavedException;
12
use Todo\Application\Task\Query;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
14
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
16
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
17
use Todo\Domain\Exception\TaskNameIsAlreadyExistedException;
18
use Todo\Domain\Exception\TaskNameIsEmptyException;
19
use Todo\Domain\Exception\TaskNotFoundException;
20
use Todo\Domain\Task;
21
use Todo\Web\FrontendBundle\Form\CreateTaskForm;
22
use Todo\Web\FrontendBundle\Form\UpdateTaskForm;
23
24
/**
25
 * Class TaskController
26
 *
27
 * @category None
28
 * @package  Todo\Web\FrontendBundle\Controller
29
 * @author   Martin Pham <[email protected]>
30
 * @license  None http://
31
 * @link     None
32
 *
33
 * @Route("/task")
34
 */
35
class TaskController extends Controller
36
{
37
    /**
38
     * List
39
     *
40
     * @param Query $taskQuery Task Query
41
     *
42
     * @Route("/list",name="task.list")
43
     * @Method({"GET"})
44
     * @Template()
45
     *
46
     * @return array
47
     */
48
    public function listAction(
49
        Query $taskQuery
50
    ) {
51
        return [
52
            'remaining_tasks' => $taskQuery->getAllRemainingTasks(),
53
            'completed_tasks' => $taskQuery->getAllCompletedTasks()
54
        ];
55
    }
56
57
    /**
58
     * CreateAction
59
     *
60
     * @Route("/create",name="task.create")
61
     * @Template()
62
     * @Method({"GET","POST"})
63
     *
64
     * @return array
65
     * @throws \LogicException
66
     */
67
    public function createAction(
68
        FormFactoryInterface $formFactory,
69
        Request $request,
70
        Session $session,
71
        Command $taskCommand
72
    ) {
73
        try {
74
            $createTaskForm = $formFactory->create(CreateTaskForm::class);
75
        } catch (InvalidOptionsException $e) {
76
            try {
77
                $this->addFlash('error', $e->getMessage());
78
            } catch (\LogicException $e) {
79
                throw $e;
80
            }
81
            return $this->redirectToRoute('task.create');
82
        }
83
84
        $createTaskForm->handleRequest($request);
85
        if ($createTaskForm->isSubmitted() && $createTaskForm->isValid()) {
86
            try {
87
                $name = $createTaskForm->getData()['name'];
88
                $this->addFlash('form_name', $name);
89
            } catch (\OutOfBoundsException $e) {
90
                try {
91
                    $this->addFlash('error', $e->getMessage());
92
                } catch (\LogicException $e) {
93
                    throw $e;
94
                }
95
                return $this->redirectToRoute('task.create');
96
            }
97
98
            try {
99
                $taskCommand->addNewTask($name);
100
            } catch (TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
101
                try {
102
                    $this->addFlash('error', $e->getMessage());
103
                } catch (\LogicException $e) {
104
                    throw $e;
105
                }
106
                return $this->redirectToRoute('task.create');
107
            }
108
109
            $session->getFlashBag()->set('form_name', null);
110
            return $this->redirectToRoute('task.list');
111
112
113
        }
114
115
        if (count($formName = $session->getFlashBag()->get('form_name')) > 0) {
116
            try {
117
                $createTaskForm->get('name')->setData($formName[0]);
118
            } catch (\OutOfBoundsException $e) {
119
                try {
120
                    $this->addFlash('error', $e->getMessage());
121
                } catch (\LogicException $e) {
122
                    throw $e;
123
                }
124
                return $this->redirectToRoute('task.create');
125
            }
126
        }
127
128
129
130
        return [
131
            'create_task_form' => $createTaskForm->createView()
132
        ];
133
    }
134
135
    /**
136
     * UpdateStatusAction
137
     *
138
     * @Route("/{taskId}/updateStatus/{taskStatus}",name="task.updateStatus")
139
     * @Method({"GET"})
140
     *
141
     * @return array
142
     * @throws \Exception
143
     */
144
    public function updateStatusAction(
145
        Command $taskCommand,
146
        $taskId,
147
        $taskStatus
148
    ) {
149
        if ($taskStatus === Task::STATUS_COMPLETED) {
150
            try {
151
                $taskCommand->completeTask($taskId);
152
            } catch (TaskCannotBeSavedException $e) {
153
                try {
154
                    $this->addFlash('error', $e->getMessage());
155
                } catch (\LogicException $e) {
156
                    throw $e;
157
                }
158
                return $this->redirectToRoute('task.list');
159
            }
160
        } else if ($taskStatus === Task::STATUS_REMAINING) {
161
            try {
162
                $taskCommand->redoTask($taskId);
163
            } catch (TaskCannotBeSavedException $e) {
164
                try {
165
                    $this->addFlash('error', $e->getMessage());
166
                } catch (\LogicException $e) {
167
                    throw $e;
168
                }
169
                return $this->redirectToRoute('task.list');
170
            }
171
        } else {
172
            throw new \Exception('Unknown status');
173
        }
174
175
        return $this->redirectToRoute('task.list');
176
    }
177
178
    /**
179
     * UpdateAction
180
     *
181
     * @Route("/{taskId}/update",name="task.update")
182
     * @Method({"GET","POST"})
183
     * @Template()
184
     *
185
     * @return array
186
     * @throws \LogicException
187
     */
188
    public function updateAction(
189
        FormFactoryInterface $formFactory,
190
        Request $request,
191
        Session $session,
192
        Query $taskQuery,
193
        Command $taskCommand,
194
        $taskId
195
    ) {
196
        try {
197
            $task = $taskQuery->getTaskById($taskId);
198
        } catch (TaskNotFoundException $e) {
199
            try {
200
                $this->addFlash('error', $e->getMessage());
201
            } catch (\LogicException $e) {
202
                throw $e;
203
            }
204
            return $this->redirectToRoute('task.update', [ 'taskId' => $taskId ]);
205
        }
206
207
208
        try {
209
            $updateTaskForm = $formFactory->create(
210
                UpdateTaskForm::class,
211
                $task
212
            );
213
        } catch (InvalidOptionsException $e) {
214
            try {
215
                $this->addFlash('error', $e->getMessage());
216
            } catch (\LogicException $e) {
217
                throw $e;
218
            }
219
            return $this->redirectToRoute('task.update', [ 'taskId' => $taskId ]);
220
        }
221
222
        $updateTaskForm->handleRequest($request);
223
        if ($updateTaskForm->isSubmitted() && $updateTaskForm->isValid()) {
224
            try {
225
                /** @var Task $task */
226
                $task = $updateTaskForm->getData();
227
228
                $name = $task->getName();
229
                $status = $task->getStatus();
230
231
                $this->addFlash('form_name', $name);
232
                $this->addFlash('form_status', $status);
233
234
            } catch (\OutOfBoundsException $e) {
235
                try {
236
                    $this->addFlash('error', $e->getMessage());
237
                } catch (\LogicException $e) {
238
                    throw $e;
239
                }
240
                return $this->redirectToRoute('task.update', [ 'taskId' => $taskId ]);
241
            } catch (\LogicException $e) {
242
                throw $e;
243
            }
244
245
            try {
246
                $taskCommand->editTask(
247
                    $taskId,
248
                    [
249
                        'name' => $name,
250
                        'status' => $status,
251
                    ]
252
                );
253
            } catch (TaskNotFoundException | TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
254
                try {
255
                    $this->addFlash('error', $e->getMessage());
256
                } catch (\LogicException $e) {
257
                    throw $e;
258
                }
259
                return $this->redirectToRoute('task.update', [ 'taskId' => $taskId ]);
260
            }
261
262
            $session->getFlashBag()->set('form_name', null);
263
            $session->getFlashBag()->set('form_status', null);
264
265
            return $this->redirectToRoute('task.list');
266
267
268
        }
269
270
        if (count($formName = $session->getFlashBag()->get('form_name')) > 0) {
271
            try {
272
                $updateTaskForm->get('name')->setData($formName[0]);
273
            } catch (\OutOfBoundsException $e) {
274
                try {
275
                    $this->addFlash('error', $e->getMessage());
276
                } catch (\LogicException $e) {
277
                    throw $e;
278
                }
279
                return $this->redirectToRoute('task.update', [ 'taskId' => $taskId ]);
280
            }
281
        }
282
283
        if (count($formStatus = $session->getFlashBag()->get('form_status')) > 0) {
284
            try {
285
                $updateTaskForm->get('status')->setData($formStatus[0]);
286
            } catch (\OutOfBoundsException $e) {
287
                try {
288
                    $this->addFlash('error', $e->getMessage());
289
                } catch (\LogicException $e) {
290
                    throw $e;
291
                }
292
                return $this->redirectToRoute('task.update', [ 'taskId' => $taskId ]);
293
            }
294
        }
295
296
297
        return [
298
            'update_task_form' => $updateTaskForm->createView()
299
        ];
300
    }
301
302
    /**
303
     * DeleteAction
304
     *
305
     * @Route("/{taskId}/delete",name="task.delete")
306
     * @Method({"GET"})
307
     *
308
     * @return array
309
     * @throws TaskNotFoundException
310
     * @throws TaskCannotBeRemovedException
311
     */
312
    public function deleteAction(
313
        Command $taskCommand,
314
        $taskId
315
    ) {
316
        try {
317
            $taskCommand->removeTask($taskId);
318
        } catch (TaskNotFoundException | TaskCannotBeRemovedException $e) {
319
            throw $e;
320
        }
321
322
        return $this->redirectToRoute('task.list');
323
324
    }
325
326
    /**
327
     * CleanAction
328
     *
329
     * @Route("/clean")
330
     * @Method({"GET"})
331
     *
332
     * @return array
333
     */
334
    public function cleanAction()
335
    {
336
        return [];
337
    }
338
339
}
340