TaskController::createAction()   C
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 51
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 6.2727
c 0
b 0
f 0
cc 9
eloc 28
nc 24
nop 3

How to fix   Long Method   

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 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 Web\FrontendBundle\Form\CreateTaskForm;
22
use Web\FrontendBundle\Form\UpdateTaskForm;
23
24
/**
25
 * Class TaskController
26
 *
27
 * @category None
28
 * @package  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 mixed
65
     * @throws \LogicException
66
     */
67
    public function createAction(
68
        FormFactoryInterface $formFactory,
69
        Request $request,
70
        Command $taskCommand
71
    ) {
72
        $errors = [];
73
74
        try {
75
            $createTaskForm = $formFactory->create(
76
                CreateTaskForm::class,
77
                $request->request->all()
78
            );
79
        } catch (InvalidOptionsException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Option...InvalidOptionsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
80
            $errors[] = $e->getMessage();
81
        }
82
83
        if (count($errors) === 0) {
84
            $createTaskForm->handleRequest($request);
85
            if ($createTaskForm->isSubmitted() && $createTaskForm->isValid()) {
86
                try {
87
                    $name = $createTaskForm->getData()['name'];
88
                } catch (\OutOfBoundsException $e) {
89
                    $errors[] = $e->getMessage();
90
                }
91
92
                /** @noinspection NotOptimalIfConditionsInspection */
93
                if (count($errors) === 0) {
94
                    try {
95
                        $taskCommand->addNewTask($name);
96
                    } catch (TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
97
                        $errors[] = $e->getMessage();
98
                    }
99
100
                    /** @noinspection NotOptimalIfConditionsInspection */
101
                    if (count($errors) === 0) {
102
                        return $this->redirectToRoute('task.list');
103
                    }
104
                }
105
106
107
108
            }
109
        }
110
111
112
113
        return [
114
            'errors' => $errors,
115
            'create_task_form' => $createTaskForm->createView()
116
        ];
117
    }
118
119
    /**
120
     * CompleteAction
121
     *
122
     * @Route("/{taskId}/complete",name="task.complete")
123
     * @Method({"GET"})
124
     *
125
     * @return mixed
126
     * @throws TaskCannotBeSavedException
127
     * @throws TaskNotFoundException
128
     */
129
    public function completeAction(
130
        Command $taskCommand,
131
        $taskId
132
    ) {
133
        try {
134
            $taskCommand->completeTask($taskId);
135
        } catch (TaskNotFoundException | TaskCannotBeSavedException $e) {
136
            throw $e;
137
        }
138
139
        return $this->redirectToRoute('task.list');
140
    }
141
142
    /**
143
     * RedoAction
144
     *
145
     * @Route("/{taskId}/redo",name="task.redo")
146
     * @Method({"GET"})
147
     *
148
     * @return mixed
149
     * @throws TaskCannotBeSavedException
150
     * @throws TaskNotFoundException
151
     */
152
    public function redoAction(
153
        Command $taskCommand,
154
        $taskId
155
    ) {
156
        try {
157
            $taskCommand->redoTask($taskId);
158
        } catch (TaskNotFoundException | TaskCannotBeSavedException $e) {
159
            throw $e;
160
        }
161
162
        return $this->redirectToRoute('task.list');
163
    }/** @noinspection MoreThanThreeArgumentsInspection */
164
165
    /**
166
     * UpdateAction
167
     *
168
     * @Route("/{taskId}/update",name="task.update")
169
     * @Method({"GET","POST"})
170
     * @Template()
171
     *
172
     * @return mixed
173
     */
174
    public function updateAction(
175
        FormFactoryInterface $formFactory,
176
        Request $request,
177
        Query $taskQuery,
178
        Command $taskCommand,
179
        $taskId
180
    ) {
181
        $errors = [];
182
        try {
183
            $task = $taskQuery->getTaskById($taskId);
184
        } catch (TaskNotFoundException $e) {
185
            $errors[] = $e->getMessage();
186
        }
187
188
        $updateTaskForm = null;
189
        if (count($errors) === 0) {
190
            try {
191
                $updateTaskForm = $formFactory->create(
192
                    UpdateTaskForm::class,
193
                    ($request->get('name') !== null) ? $request->request->all() : $task
194
                );
195
            } catch (InvalidOptionsException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Option...InvalidOptionsException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
196
                $errors[] = $e->getMessage();
197
            }
198
        }
199
200
        if ($updateTaskForm !== null && count($errors) === 0) {
201
            $updateTaskForm->handleRequest($request);
202
            if ($updateTaskForm->isSubmitted() && $updateTaskForm->isValid()) {
203
                $name = null;
204
                $status = null;
205
                try {
206
                    /** @var Task $task */
207
                    $task = $updateTaskForm->getData();
208
209
                    $name = $task->getName();
210
                    $status = $task->getStatus();
211
212
                } catch (\OutOfBoundsException | \LogicException $e) {
213
                    $errors[] = $e->getMessage();
214
                }
215
216
                /** @noinspection NotOptimalIfConditionsInspection */
217
                if ($name !== null && $status !== null && count($errors) === 0) {
218
                    try {
219
                        $taskCommand->editTask(
220
                            $taskId,
221
                            [
222
                                'name'   => $name,
223
                                'status' => $status,
224
                            ]
225
                        );
226
                    } catch (TaskNotFoundException | TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
227
                        $errors[] = $e->getMessage();
228
229
                    }
230
                }
231
232
                /** @noinspection NotOptimalIfConditionsInspection */
233
                if (count($errors) === 0) {
234
                    return $this->redirectToRoute('task.list');
235
                }
236
237
238
            }
239
240
        }
241
242
243
        return [
244
            'errors' => $errors,
245
            'update_task_form' => $updateTaskForm->createView()
246
        ];
247
    }
248
249
    /**
250
     * DeleteAction
251
     *
252
     * @Route("/{taskId}/delete",name="task.delete")
253
     * @Method({"GET"})
254
     *
255
     * @return mixed
256
     * @throws TaskNotFoundException
257
     * @throws TaskCannotBeRemovedException
258
     */
259
    public function deleteAction(
260
        Command $taskCommand,
261
        $taskId
262
    ) {
263
        try {
264
            $taskCommand->removeTask($taskId);
265
        } catch (TaskNotFoundException | TaskCannotBeRemovedException $e) {
266
            throw $e;
267
        }
268
269
        return $this->redirectToRoute('task.list');
270
271
    }
272
273
    /**
274
     * CleanAction
275
     *
276
     * @Route("/clean",name="task.clean")
277
     * @Method({"GET"})
278
     *
279
     * @return mixed
280
     * @throws TaskCannotBeRemovedException
281
     */
282
    public function cleanAction(
283
        Command $taskCommand
284
    ) {
285
        try {
286
            $taskCommand->cleanAllCompletedTasks();
287
        } catch (TaskCannotBeRemovedException $e) {
288
            throw $e;
289
        }
290
        return $this->redirectToRoute('task.list');
291
    }
292
293
}
294