1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Service; |
4
|
|
|
|
5
|
|
|
use App\Repository\TasksRepository; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Tasks Service. |
9
|
|
|
*/ |
10
|
|
|
class TasksService extends BaseService |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Constructor of the class. |
14
|
|
|
* |
15
|
|
|
* @param object $database |
16
|
|
|
*/ |
17
|
|
|
public function __construct(\PDO $database) |
18
|
|
|
{ |
19
|
|
|
$this->database = $database; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Check if the task exists. |
24
|
|
|
* |
25
|
|
|
* @param int $taskId |
26
|
|
|
* @return object $task |
27
|
|
|
* @throws \Exception |
28
|
|
|
*/ |
29
|
|
|
public function checkTask($taskId) |
30
|
|
|
{ |
31
|
|
|
$tasksRepository = new TasksRepository; |
32
|
|
|
$query = $tasksRepository->getTaskQuery(); |
33
|
|
|
$statement = $this->database->prepare($query); |
34
|
|
|
$statement->bindParam('id', $taskId); |
35
|
|
|
$statement->execute(); |
36
|
|
|
$task = $statement->fetchObject(); |
37
|
|
|
if (!$task) { |
38
|
|
|
throw new \Exception(self::TASK_NOT_FOUND, 404); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $task; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get all tasks. |
46
|
|
|
* |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
public function getTasks() |
50
|
|
|
{ |
51
|
|
|
$repository = new TasksRepository; |
52
|
|
|
$query = $repository->getTasksQuery(); |
53
|
|
|
$statement = $this->database->prepare($query); |
54
|
|
|
$statement->execute(); |
55
|
|
|
|
56
|
|
|
return $statement->fetchAll(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get one task by id. |
61
|
|
|
* |
62
|
|
|
* @param int $taskId |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public function getTask($taskId) |
66
|
|
|
{ |
67
|
|
|
$task = $this->checkTask($taskId); |
68
|
|
|
|
69
|
|
|
return $task; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Search tasks by name. |
74
|
|
|
* |
75
|
|
|
* @param string $tasksName |
76
|
|
|
* @return array |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function searchTasks($tasksName) |
80
|
|
|
{ |
81
|
|
|
$repository = new TasksRepository; |
82
|
|
|
$query = $repository->searchTasksQuery(); |
83
|
|
|
$statement = $this->database->prepare($query); |
84
|
|
|
$query = '%' . $tasksName . '%'; |
85
|
|
|
$statement->bindParam('query', $query); |
86
|
|
|
$statement->execute(); |
87
|
|
|
$tasks = $statement->fetchAll(); |
88
|
|
|
if (!$tasks) { |
89
|
|
|
throw new \Exception(self::TASK_NOT_FOUND, 404); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $tasks; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create a task. |
97
|
|
|
* |
98
|
|
|
* @param array $input |
99
|
|
|
* @return array |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function createTask($input) |
103
|
|
|
{ |
104
|
|
|
$data = $this->validateInputOnCreateTask($input); |
105
|
|
|
$repository = new TasksRepository; |
106
|
|
|
$query = $repository->createTaskQuery(); |
107
|
|
|
$statement = $this->database->prepare($query); |
108
|
|
|
$statement->bindParam('task', $data['task']); |
109
|
|
|
$statement->bindParam('status', $data['status']); |
110
|
|
|
$statement->execute(); |
111
|
|
|
$task = $this->checkTask($this->database->lastInsertId()); |
112
|
|
|
|
113
|
|
|
return $task; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Update a task. |
118
|
|
|
* |
119
|
|
|
* @param array $input |
120
|
|
|
* @param int $taskId |
121
|
|
|
* @return array |
122
|
|
|
* @throws \Exception |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function updateTask($input, $taskId) |
125
|
|
|
{ |
126
|
|
|
$task = $this->checkTask($taskId); |
127
|
|
|
$data = $this->validateInputOnUpdateTask($input, $task); |
128
|
|
|
$repository = new TasksRepository; |
129
|
|
|
$query = $repository->updateTaskQuery(); |
130
|
|
|
$statement = $this->database->prepare($query); |
131
|
|
|
$statement->bindParam('id', $taskId); |
132
|
|
|
$statement->bindParam('task', $data['task']); |
133
|
|
|
$statement->bindParam('status', $data['status']); |
134
|
|
|
$statement->execute(); |
135
|
|
|
|
136
|
|
|
return $this->checkTask($taskId); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Delete a task. |
141
|
|
|
* |
142
|
|
|
* @param int $taskId |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function deleteTask($taskId) |
146
|
|
|
{ |
147
|
|
|
$this->checkTask($taskId); |
148
|
|
|
$repository = new TasksRepository; |
149
|
|
|
$query = $repository->deleteTaskQuery(); |
150
|
|
|
$statement = $this->database->prepare($query); |
151
|
|
|
$statement->bindParam('id', $taskId); |
152
|
|
|
$statement->execute(); |
153
|
|
|
|
154
|
|
|
return self::TASK_DELETED; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|