Passed
Push — master ( 783d3e...cfd8c6 )
by Martin
02:33
created

TaskController::updateAction()   C

Complexity

Conditions 12
Paths 156

Size

Total Lines 70
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 70
rs 5.1917
c 0
b 0
f 0
cc 12
eloc 41
nc 156
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,
0 ignored issues
show
Unused Code introduced by
The parameter $session is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
        Command $taskCommand
72
    ) {
73
        $errors = [];
74
75
        try {
76
            $createTaskForm = $formFactory->create(
77
                CreateTaskForm::class,
78
                $request->request->all()
79
            );
80
        } catch (InvalidOptionsException $e) {
81
            $errors[] = $e->getMessage();
82
        }
83
84
        if (count($errors) === 0) {
85
            $createTaskForm->handleRequest($request);
86
            if ($createTaskForm->isSubmitted() && $createTaskForm->isValid()) {
87
                try {
88
                    $name = $createTaskForm->getData()['name'];
89
                } catch (\OutOfBoundsException $e) {
90
                    $errors[] = $e->getMessage();
91
                }
92
93
                if (count($errors) === 0) {
94
                    try {
95
                        $taskCommand->addNewTask($name);
96
                    } catch (TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
97
                        $errors[] = $e->getMessage();
98
                    }
99
100
                    if (count($errors) === 0) {
101
                        return $this->redirectToRoute('task.list');
102
                    }
103
                }
104
105
106
107
            }
108
        }
109
110
111
112
        return [
113
            'errors' => $errors,
114
            'create_task_form' => $createTaskForm->createView()
115
        ];
116
    }
117
118
    /**
119
     * UpdateStatusAction
120
     *
121
     * @Route("/{taskId}/updateStatus/{taskStatus}",name="task.updateStatus")
122
     * @Method({"GET"})
123
     *
124
     * @return array
125
     * @throws \Exception
126
     */
127
    public function updateStatusAction(
128
        Command $taskCommand,
129
        $taskId,
130
        $taskStatus
131
    ) {
132
        if ($taskStatus === Task::STATUS_COMPLETED) {
133
            try {
134
                $taskCommand->completeTask($taskId);
135
            } catch (TaskCannotBeSavedException $e) {
136
                throw $e;
137
            }
138
        } else if ($taskStatus === Task::STATUS_REMAINING) {
139
            try {
140
                $taskCommand->redoTask($taskId);
141
            } catch (TaskCannotBeSavedException $e) {
142
                throw $e;
143
            }
144
        } else {
145
            throw new \Exception('Unknown status');
146
        }
147
148
        return $this->redirectToRoute('task.list');
149
    }
150
151
    /**
152
     * UpdateAction
153
     *
154
     * @Route("/{taskId}/update",name="task.update")
155
     * @Method({"GET","POST"})
156
     * @Template()
157
     *
158
     * @return array
159
     */
160
    public function updateAction(
161
        FormFactoryInterface $formFactory,
162
        Request $request,
163
        Session $session,
0 ignored issues
show
Unused Code introduced by
The parameter $session is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
        Query $taskQuery,
165
        Command $taskCommand,
166
        $taskId
167
    ) {
168
        $errors = [];
169
        try {
170
            $task = $taskQuery->getTaskById($taskId);
171
        } catch (TaskNotFoundException $e) {
172
            $errors[] = $e->getMessage();
173
        }
174
175
        if (count($errors) === 0) {
176
            try {
177
                $updateTaskForm = $formFactory->create(
178
                    UpdateTaskForm::class,
179
                    ($request->get('name') !== null) ? $request->request->all() : $task
180
                );
181
            } catch (InvalidOptionsException $e) {
182
                $errors[] = $e->getMessage();
183
            }
184
        }
185
186
        if (count($errors) === 0) {
187
            $updateTaskForm->handleRequest($request);
0 ignored issues
show
Bug introduced by
The variable $updateTaskForm does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
188
            if ($updateTaskForm->isSubmitted() && $updateTaskForm->isValid()) {
189
                try {
190
                    /** @var Task $task */
191
                    $task = $updateTaskForm->getData();
192
193
                    $name = $task->getName();
194
                    $status = $task->getStatus();
195
196
                } catch (\OutOfBoundsException | \LogicException $e) {
197
                    $errors[] = $e->getMessage();
198
                }
199
200
                if (count($errors) === 0) {
201
                    try {
202
                        $taskCommand->editTask(
203
                            $taskId,
204
                            [
205
                                'name'   => $name,
0 ignored issues
show
Bug introduced by
The variable $name does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
206
                                'status' => $status,
0 ignored issues
show
Bug introduced by
The variable $status does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
207
                            ]
208
                        );
209
                    } catch (TaskNotFoundException | TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
210
                        $errors[] = $e->getMessage();
211
212
                    }
213
                }
214
215
                if (count($errors) === 0) {
216
                    return $this->redirectToRoute('task.list');
217
                }
218
219
220
            }
221
222
        }
223
224
225
        return [
226
            'errors' => $errors,
227
            'update_task_form' => $updateTaskForm->createView()
228
        ];
229
    }
230
231
    /**
232
     * DeleteAction
233
     *
234
     * @Route("/{taskId}/delete",name="task.delete")
235
     * @Method({"GET"})
236
     *
237
     * @return array
238
     * @throws TaskNotFoundException
239
     * @throws TaskCannotBeRemovedException
240
     */
241
    public function deleteAction(
242
        Command $taskCommand,
243
        $taskId
244
    ) {
245
        try {
246
            $taskCommand->removeTask($taskId);
247
        } catch (TaskNotFoundException | TaskCannotBeRemovedException $e) {
248
            throw $e;
249
        }
250
251
        return $this->redirectToRoute('task.list');
252
253
    }
254
255
    /**
256
     * CleanAction
257
     *
258
     * @Route("/clean")
259
     * @Method({"GET"})
260
     *
261
     * @return array
262
     */
263
    public function cleanAction()
264
    {
265
        return [];
266
    }
267
268
}
269