Test Failed
Push — master ( 810670...0a801e )
by Martin
02:32
created

TaskController::updateStatusAction()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 33
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 25
nc 7
nop 3
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
23
/**
24
 * Class TaskController
25
 *
26
 * @category None
27
 * @package  Todo\Web\FrontendBundle\Controller
28
 * @author   Martin Pham <[email protected]>
29
 * @license  None http://
30
 * @link     None
31
 *
32
 * @Route("/task")
33
 */
34
class TaskController extends Controller
35
{
36
    /**
37
     * List
38
     *
39
     * @param Query $taskQuery Task Query
40
     *
41
     * @Route("/list",name="task.list")
42
     * @Method({"GET"})
43
     * @Template()
44
     *
45
     * @return array
46
     */
47
    public function listAction(
48
        Query $taskQuery
49
    ) {
50
        return [
51
            'remaining_tasks' => $taskQuery->getAllRemainingTasks(),
52
            'completed_tasks' => $taskQuery->getAllCompletedTasks()
53
        ];
54
    }
55
56
    /**
57
     * CreateAction
58
     *
59
     * @Route("/create",name="task.create")
60
     * @Template()
61
     * @Method({"GET","POST"})
62
     *
63
     * @return array
64
     * @throws \LogicException
65
     */
66
    public function createAction(
67
        FormFactoryInterface $formFactory,
68
        Request $request,
69
        Session $session,
70
        Command $taskCommand
71
    ) {
72
        try {
73
            $createTaskForm = $formFactory->create(CreateTaskForm::class);
74
        } catch (InvalidOptionsException $e) {
75
            try {
76
                $this->addFlash('error', $e->getMessage());
77
            } catch (\LogicException $e) {
78
                throw $e;
79
            }
80
            return $this->redirectToRoute('task.create');
81
        }
82
83
        $createTaskForm->handleRequest($request);
84
        if ($createTaskForm->isSubmitted() && $createTaskForm->isValid()) {
85
            try {
86
                $name = $createTaskForm->getData()['name'];
87
                $this->addFlash('form_name', $name);
88
            } catch (\OutOfBoundsException $e) {
89
                try {
90
                    $this->addFlash('error', $e->getMessage());
91
                } catch (\LogicException $e) {
92
                    throw $e;
93
                }
94
                return $this->redirectToRoute('task.create');
95
            }
96
97
            try {
98
                $taskCommand->addNewTask($name);
99
            } catch (TaskNameIsEmptyException | TaskNameIsAlreadyExistedException | TaskCannotBeSavedException $e) {
100
                try {
101
                    $this->addFlash('error', $e->getMessage());
102
                } catch (\LogicException $e) {
103
                    throw $e;
104
                }
105
                return $this->redirectToRoute('task.create');
106
            }
107
108
            $formName = $session->getFlashBag()->set('form_name', null);
0 ignored issues
show
Unused Code introduced by
$formName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
            return $this->redirectToRoute('task.list');
110
111
112
        }
113
114
        if (count($formName = $session->getFlashBag()->get('form_name')) > 0) {
115
            try {
116
                $createTaskForm->get('name')->setData($formName[0]);
117
            } catch (\OutOfBoundsException $e) {
118
                try {
119
                    $this->addFlash('error', $e->getMessage());
120
                } catch (\LogicException $e) {
121
                    throw $e;
122
                }
123
                return $this->redirectToRoute('task.create');
124
            }
125
        }
126
127
128
129
        return [
130
            'create_task_form' => $createTaskForm->createView()
131
        ];
132
    }
133
134
    /**
135
     * UpdateStatusAction
136
     *
137
     * @Route("/{taskId}/updateStatus/{taskStatus}",name="task.updateStatus")
138
     * @Method({"GET"})
139
     *
140
     * @return array
141
     * @throws \Exception
142
     */
143
    public function updateStatusAction(
144
        Command $taskCommand,
145
        $taskId,
146
        $taskStatus
147
    ) {
148
        if ($taskStatus === Task::STATUS_COMPLETED) {
149
            try {
150
                $taskCommand->completeTask($taskId);
151
            } catch (TaskCannotBeSavedException $e) {
152
                try {
153
                    $this->addFlash('error', $e->getMessage());
154
                } catch (\LogicException $e) {
155
                    throw $e;
156
                }
157
                return $this->redirectToRoute('task.list');
158
            }
159
        } else if ($taskStatus === Task::STATUS_REMAINING) {
160
            try {
161
                $taskCommand->redoTask($taskId);
162
            } catch (TaskCannotBeSavedException $e) {
163
                try {
164
                    $this->addFlash('error', $e->getMessage());
165
                } catch (\LogicException $e) {
166
                    throw $e;
167
                }
168
                return $this->redirectToRoute('task.list');
169
            }
170
        } else {
171
            throw new \Exception('Unknown status');
172
        }
173
174
        return $this->redirectToRoute('task.list');
175
    }
176
177
    /**
178
     * UpdateAction
179
     *
180
     * @Route("/update")
181
     * @Method({"GET"})
182
     *
183
     * @return array
184
     */
185
    public function updateAction()
186
    {
187
        return [];
188
    }
189
190
    /**
191
     * DeleteAction
192
     *
193
     * @Route("/{taskId}/delete",name="task.delete")
194
     * @Method({"GET"})
195
     *
196
     * @return array
197
     * @throws TaskNotFoundException
198
     * @throws TaskCannotBeRemovedException
199
     */
200
    public function deleteAction(
201
        Command $taskCommand,
202
        $taskId
203
    ) {
204
        try {
205
            $taskCommand->removeTask($taskId);
206
        } catch (TaskNotFoundException | TaskCannotBeRemovedException $e) {
207
            throw $e;
208
        }
209
210
        return $this->redirectToRoute('task.list');
211
212
    }
213
214
    /**
215
     * CleanAction
216
     *
217
     * @Route("/clean")
218
     * @Method({"GET"})
219
     *
220
     * @return array
221
     */
222
    public function cleanAction()
223
    {
224
        return [];
225
    }
226
227
}
228