1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Service; |
4
|
|
|
|
5
|
|
|
use App\Repository\TaskRepository; |
6
|
|
|
use App\Validation\TaskValidation as vs; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Tasks Service. |
10
|
|
|
*/ |
11
|
|
|
class TaskService extends BaseService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param TaskRepository $taskRepository |
15
|
|
|
*/ |
16
|
|
|
public function __construct(TaskRepository $taskRepository) |
17
|
|
|
{ |
18
|
|
|
$this->taskRepository = $taskRepository; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return TaskRepository |
23
|
|
|
*/ |
24
|
|
|
protected function getTaskRepository() |
25
|
|
|
{ |
26
|
|
|
return $this->taskRepository; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Check if the task exists. |
31
|
|
|
* |
32
|
|
|
* @param int $taskId |
33
|
|
|
* @return object |
34
|
|
|
*/ |
35
|
|
|
protected function checkTask($taskId) |
36
|
|
|
{ |
37
|
|
|
$task = $this->getTaskRepository()->checkTask($taskId); |
38
|
|
|
|
39
|
|
|
return $task; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get all tasks. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function getTasks() |
48
|
|
|
{ |
49
|
|
|
$tasks = $this->getTaskRepository()->getTasks(); |
50
|
|
|
|
51
|
|
|
return $tasks; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get one task by id. |
56
|
|
|
* |
57
|
|
|
* @param int $taskId |
58
|
|
|
* @return object |
59
|
|
|
*/ |
60
|
|
|
public function getTask($taskId) |
61
|
|
|
{ |
62
|
|
|
$task = $this->checkTask($taskId); |
63
|
|
|
|
64
|
|
|
return $task; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Search tasks by name. |
69
|
|
|
* |
70
|
|
|
* @param string $tasksName |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function searchTasks($tasksName) |
74
|
|
|
{ |
75
|
|
|
$tasks = $this->getTaskRepository()->searchTasks($tasksName); |
76
|
|
|
|
77
|
|
|
return $tasks; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create a task. |
82
|
|
|
* |
83
|
|
|
* @param array|object|null $input |
84
|
|
|
* @return object |
85
|
|
|
*/ |
86
|
|
|
public function createTask($input) |
87
|
|
|
{ |
88
|
|
|
$data = vs::validateInputOnCreateTask($input); |
89
|
|
|
$task = $this->getTaskRepository()->createTask($data); |
90
|
|
|
|
91
|
|
|
return $task; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update a task. |
96
|
|
|
* |
97
|
|
|
* @param array|object|null $input |
98
|
|
|
* @param int $taskId |
99
|
|
|
* @return object |
100
|
|
|
*/ |
101
|
|
|
public function updateTask($input, $taskId) |
102
|
|
|
{ |
103
|
|
|
$checkTask = $this->checkTask($taskId); |
104
|
|
|
$data = vs::validateInputOnUpdateTask($input, $checkTask); |
105
|
|
|
$task = $this->getTaskRepository()->updateTask($data, $taskId); |
106
|
|
|
|
107
|
|
|
return $task; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Delete a task. |
112
|
|
|
* |
113
|
|
|
* @param int $taskId |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function deleteTask($taskId) |
117
|
|
|
{ |
118
|
|
|
$this->checkTask($taskId); |
119
|
|
|
$response = $this->getTaskRepository()->deleteTask($taskId); |
120
|
|
|
|
121
|
|
|
return $response; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: