PluginProbe
Metricool – Social media and site statistics / trunk
Metricool – Social media and site statistics vtrunk
2.1.0 2.0.2 2.0.1 2.0.0 1.27 trunk
metricool / app / Features / TaskManagement / TaskManagementService.php

TaskManagementService.php in Metricool – Social media and site statistics trunk, at app/Features/TaskManagement/TaskManagementService.php

182 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Metricool\Features\TaskManagement;
6
7 use Metricool\Bootstrap\App;
8 use Metricool\Interfaces\TaskInterface;
9
10 class TaskManagementService
11 {
12 private TaskManagementRepository $repository;
13
14 public function __construct(TaskManagementRepository $repository)
15 {
16 $this->repository = $repository;
17 }
18
19 /**
20 * Check if there are tasks
21 */
22 public function hasTasks(): bool
23 {
24 return !empty($this->repository->getAllTasks());
25 }
26
27 /**
28 * Get a task by its identifier
29 */
30 public function getTask(string $taskId): ?TaskInterface
31 {
32 return $this->repository->getTask($taskId);
33 }
34
35 /**
36 * Get all tasks
37 * @return TaskInterface[]
38 */
39 public function getAllTasks(bool $strict = false): array
40 {
41 return $this->repository->getAllTasks($strict);
42 }
43
44 /**
45 * Add or update a task
46 */
47 public function addTask(TaskInterface $task, bool $save = true): void
48 {
49 $this->repository->addTask($task, $save);
50 }
51
52 /**
53 * Add or update multiple tasks at once
54 * @param class-string<TaskInterface>[] $tasks
55 * @throws \Exception If task class cannot be instantiated
56 */
57 public function addTasks(array $tasks): void
58 {
59 foreach ($tasks as $taskClassString) {
60 $task = App::getInstance()->make($taskClassString);
61
62 $this->repository->addTask($task, false);
63 }
64 $this->repository->saveTasksToDatabase();
65 }
66
67 /**
68 * Upgrade the tasks. Only replace existing tasks with same identifier if
69 * the version is lower than the new task version. Add missing tasks and
70 * remove tasks that are no longer present.
71 * @param class-string<TaskInterface>[] $tasks
72 * @throws \Exception If task class cannot be instantiated
73 */
74 public function upgradeTasks(array $tasks): void
75 {
76 // Remove tasks that are no longer present. Maybe that are them all?
77 $deletableTasksList = $this->repository->getAllTasks();
78
79 foreach ($tasks as $taskClassString) {
80 $task = App::getInstance()->make($taskClassString);
81
82 $this->repository->upgradeTask($task, false);
83
84 // Current tasks is not deletable so remove it from the list
85 unset($deletableTasksList[$task->getId()]);
86 }
87
88 // If list still contains tasks, the upgrade requests them to be removed
89 if (!empty($deletableTasksList)) {
90 $this->removeDeletableTasksAfterUpgrade($deletableTasksList, false);
91 }
92
93 $this->repository->saveTasksToDatabase();
94 }
95
96 /**
97 * Open a task in the repository
98 */
99 public function openTask(string $taskId): void
100 {
101 $task = $this->getTask($taskId);
102 if ($task) {
103 $this->addTask($task->open());
104 }
105 }
106
107 /**
108 * Dismiss a task
109 */
110 public function dismissTask(string $taskId): void
111 {
112 $task = $this->getTask($taskId);
113 if ($task) {
114 $this->addTask($task->dismiss());
115 }
116 }
117
118 /**
119 * Complete a task
120 */
121 public function completeTask(string $taskId): void
122 {
123 $task = $this->getTask($taskId);
124 if ($task) {
125 $this->addTask($task->complete());
126 }
127 }
128
129 /**
130 * Hide a task
131 */
132 public function hideTask(string $taskId): void
133 {
134 $task = $this->getTask($taskId);
135 if ($task) {
136 $this->addTask($task->hide());
137 }
138 }
139
140 /**
141 * Flag a task as urgent
142 */
143 public function flagTaskUrgent(string $taskId): void
144 {
145 $task = $this->getTask($taskId);
146 if ($task) {
147 $this->addTask($task->urgent());
148 }
149 }
150
151 /**
152 * Remove multiple tasks at once
153 * @param TaskInterface[] $tasks
154 */
155 public function removeTasks(array $tasks, bool $save = true): void
156 {
157 foreach ($tasks as $task) {
158 $this->repository->removeTask($task, $save);
159 }
160
161 if ($save) {
162 $this->repository->saveTasksToDatabase();
163 }
164 }
165
166 /**
167 * Remove tasks that are no longer present in our Task Object list. Such
168 * tasks are now a __PHP_Incomplete_Class and do not implement the
169 * TaskInterface. Because of this we cannot use the task classes.
170 */
171 private function removeDeletableTasksAfterUpgrade(array $deletableTasksList, bool $save = true): void
172 {
173 foreach ($deletableTasksList as $taskId => $deletedTask) {
174 $this->repository->removeTaskById($taskId, $save);
175 }
176
177 if ($save) {
178 $this->repository->saveTasksToDatabase();
179 }
180 }
181 }
182