Test Failed
Push — master ( faf208...51c6fd )
by Martin
14:25
created

Command::editTask()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
1
<?php
2
3
namespace Todo\Application\Task;
4
use Todo\Application\Task\Exception\TaskCannotBeRemovedException;
5
use Todo\Application\Task\Exception\TaskCannotBeSavedException;
6
use Todo\Domain\Exception\TaskNameIsAlreadyExistedException;
7
use Todo\Domain\Exception\TaskNameIsEmptyException;
8
use Todo\Domain\Exception\TaskNotFoundException;
9
use Todo\Domain\Factory\TaskFactory;
10
use Todo\Domain\Repository\TaskRepositoryInterface;
11
use Todo\Domain\Task;
12
13
/**
14
 * Class Command
15
 *
16
 * @category None
17
 * @package  Application\Task
18
 * @author   Martin Pham <[email protected]>
19
 * @license  None http://
20
 * @link     None
21
 */
22
class Command
23
{
24
    /**
25
     * TaskRepository
26
     *
27
     * @var TaskRepositoryInterface
28
     */
29
    protected $taskRepository;
30
31
    /**
32
     * TaskFactory
33
     *
34
     * @var TaskFactory
35
     */
36
    protected $taskFactory;
37
38
    /**
39
     * Command constructor
40
     *
41
     * @param TaskRepositoryInterface $taskRepository Task Repository
42
     * @param TaskFactory             $taskFactory    Task Factory
43
     */
44
    public function __construct(
45
        TaskRepositoryInterface $taskRepository,
46
        TaskFactory $taskFactory
47
    ) {
48
        $this->taskRepository = $taskRepository;
49
        $this->taskFactory = $taskFactory;
50
    }
51
52
53
    /**
54
     * Add New Task
55
     *
56
     * @param string $name Name
57
     *
58
     * @return Task
59
     * @throws TaskCannotBeSavedException
60
     * @throws TaskNameIsAlreadyExistedException
61
     * @throws TaskNameIsEmptyException
62
     */
63
    public function addNewTask(string $name) : Task
64
    {
65
        try {
66
            $task = $this->taskFactory->createFromName($name);
67
        } catch (TaskNameIsEmptyException | TaskNameIsAlreadyExistedException $e) {
68
            throw $e;
69
        }
70
71
        $result = $this->taskRepository->save($task);
72
73
        if (!$result) {
74
            throw new TaskCannotBeSavedException(
75
                'Cannot save task into repository.'
76
            );
77
        }
78
79
        return $task;
80
    }
81
82
    /**
83
     * Complete Task
84
     *
85
     * @param Task $task Task
86
     *
87
     * @return Task
88
     * @throws TaskCannotBeSavedException
89
     */
90
    public function completeTask(Task $task) : Task
91
    {
92
        $task->setStatus(Task::STATUS_COMPLETED);
93
94
        $result = $this->taskRepository->save($task);
95
96
        if (!$result) {
97
            throw new TaskCannotBeSavedException(
98
                'Cannot save task into repository.'
99
            );
100
        }
101
102
        return $task;
103
    }
104
105
    /**
106
     * Redo Task
107
     *
108
     * @param Task $task Task
109
     *
110
     * @return Task
111
     * @throws TaskCannotBeSavedException
112
     */
113
    public function redoTask(Task $task) : Task
114
    {
115
        $task->setStatus(Task::STATUS_REMAINING);
116
117
        $result = $this->taskRepository->save($task);
118
119
        if (!$result) {
120
            throw new TaskCannotBeSavedException(
121
                'Cannot save task into repository.'
122
            );
123
        }
124
125
        return $task;
126
    }
127
128
    /**
129
     * EditTask
130
     *
131
     * @param string $taskId ID
132
     * @param array  $data   Field
133
     *
134
     * @return Task
135
     * @throws TaskCannotBeSavedException
136
     * @throws TaskNotFoundException
137
     */
138
    public function editTask(string $taskId, array $data) : Task
139
    {
140
        try {
141
            $task = $this->taskRepository->find($taskId);
142
        } catch (TaskNotFoundException $e) {
143
            throw $e;
144
        }
145
146
        $task->setName($data['name']);
147
        $task->setStatus($data['status']);
148
149
        $result = $this->taskRepository->save($task);
150
151
        if (!$result) {
152
            throw new TaskCannotBeSavedException(
153
                'Cannot save task into repository.'
154
            );
155
        }
156
157
        return $task;
158
    }
159
160
161
    /**
162
     * RemoveTask
163
     *
164
     * @param string $taskId ID
165
     *
166
     * @return void
167
     * @throws TaskCannotBeRemovedException
168
     * @throws TaskNotFoundException
169
     */
170
    public function removeTask($taskId)
171
    {
172
        try {
173
            $task = $this->taskRepository->find($taskId);
174
        } catch (TaskNotFoundException $e) {
175
            throw $e;
176
        }
177
178
        $result = $this->taskRepository->remove($task);
179
180
        if (!$result) {
181
            throw new TaskCannotBeRemovedException(
182
                'Cannot remove task(s) from repository.'
183
            );
184
        }
185
    }
186
187
    /**
188
     * CleanAllCompletedTasks
189
     *
190
     * @return void
191
     * @throws TaskCannotBeRemovedException
192
     */
193
    public function cleanAllCompletedTasks()
194
    {
195
        $result = $this->taskRepository->removeByStatus(Task::STATUS_COMPLETED);
196
197
        if (!$result) {
198
            throw new TaskCannotBeRemovedException(
199
                'Cannot remove task(s) from repository.'
200
            );
201
        }
202
    }
203
}
204