|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use App\Repository\Query\TaskQuery; |
|
6
|
|
|
use App\Service\MessageService; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Tasks Repository. |
|
10
|
|
|
*/ |
|
11
|
|
|
class TaskRepository extends BaseRepository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @param object $database |
|
15
|
|
|
*/ |
|
16
|
|
|
public function __construct(\PDO $database = null) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->database = $database; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Check if the task exists. |
|
23
|
|
|
* |
|
24
|
|
|
* @param int $taskId |
|
25
|
|
|
* @return array $task |
|
26
|
|
|
* @throws \Exception |
|
27
|
|
|
*/ |
|
28
|
|
View Code Duplication |
public function checkTask($taskId) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$query = TaskQuery::getTaskQuery(); |
|
31
|
|
|
$statement = $this->database->prepare($query); |
|
32
|
|
|
$statement->bindParam('id', $taskId); |
|
33
|
|
|
$statement->execute(); |
|
34
|
|
|
$task = $statement->fetchObject(); |
|
35
|
|
|
if (empty($task)) { |
|
36
|
|
|
throw new \Exception(MessageService::TASK_NOT_FOUND, 404); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $task; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get all tasks. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getTasks() |
|
48
|
|
|
{ |
|
49
|
|
|
$query = TaskQuery::getTasksQuery(); |
|
50
|
|
|
$statement = $this->database->prepare($query); |
|
51
|
|
|
$statement->execute(); |
|
52
|
|
|
|
|
53
|
|
|
return $statement->fetchAll(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Search tasks by name. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $tasksName |
|
60
|
|
|
* @return array |
|
61
|
|
|
* @throws \Exception |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function searchTasks($tasksName) |
|
64
|
|
|
{ |
|
65
|
|
|
$query = TaskQuery::searchTasksQuery(); |
|
66
|
|
|
$statement = $this->database->prepare($query); |
|
67
|
|
|
$query = '%' . $tasksName . '%'; |
|
68
|
|
|
$statement->bindParam('query', $query); |
|
69
|
|
|
$statement->execute(); |
|
70
|
|
|
$tasks = $statement->fetchAll(); |
|
71
|
|
|
if (!$tasks) { |
|
72
|
|
|
throw new \Exception(MessageService::TASK_NOT_FOUND, 404); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $tasks; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Create a task. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $data |
|
82
|
|
|
* @return array |
|
83
|
|
|
* @throws \Exception |
|
84
|
|
|
*/ |
|
85
|
|
|
public function createTask($data) |
|
86
|
|
|
{ |
|
87
|
|
|
$query = TaskQuery::createTaskQuery(); |
|
88
|
|
|
$statement = $this->database->prepare($query); |
|
89
|
|
|
$statement->bindParam('task', $data['task']); |
|
90
|
|
|
$statement->bindParam('status', $data['status']); |
|
91
|
|
|
$statement->execute(); |
|
92
|
|
|
$task = $this->checkTask($this->database->lastInsertId()); |
|
93
|
|
|
|
|
94
|
|
|
return $task; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Update a task. |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $data |
|
101
|
|
|
* @param int $taskId |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function updateTask($data, $taskId) |
|
105
|
|
|
{ |
|
106
|
|
|
$query = TaskQuery::updateTaskQuery(); |
|
107
|
|
|
$statement = $this->database->prepare($query); |
|
108
|
|
|
$statement->bindParam('id', $taskId); |
|
109
|
|
|
$statement->bindParam('task', $data['task']); |
|
110
|
|
|
$statement->bindParam('status', $data['status']); |
|
111
|
|
|
$statement->execute(); |
|
112
|
|
|
$task = $this->checkTask($taskId); |
|
113
|
|
|
|
|
114
|
|
|
return $task; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Delete a task. |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $taskId |
|
121
|
|
|
* @return void |
|
122
|
|
|
*/ |
|
123
|
|
|
public function deleteTask($taskId) |
|
124
|
|
|
{ |
|
125
|
|
|
$query = TaskQuery::deleteTaskQuery(); |
|
126
|
|
|
$statement = $this->database->prepare($query); |
|
127
|
|
|
$statement->bindParam('id', $taskId); |
|
128
|
|
|
$statement->execute(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.