| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Metricool\Features\TaskManagement; |
| 6 |
|
| 7 |
use Metricool\Interfaces\TaskInterface; |
| 8 |
|
| 9 |
class TaskManagementRepository |
| 10 |
{ |
| 11 |
public const OPTION_NAME = 'metricool_tasks'; |
| 12 |
|
| 13 |
/** @var TaskInterface[] */ |
| 14 |
private array $tasks = []; |
| 15 |
|
| 16 |
public function __construct() |
| 17 |
{ |
| 18 |
$this->loadTasksFromDatabase(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Retrieve a single task by its ID |
| 23 |
*/ |
| 24 |
public function getTask(string $taskId): ?TaskInterface |
| 25 |
{ |
| 26 |
return $this->tasks[$taskId] ?? null; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Retrieve all registered tasks |
| 31 |
* @return TaskInterface[] |
| 32 |
*/ |
| 33 |
public function getAllTasks(bool $strict = false): array |
| 34 |
{ |
| 35 |
$tasks = $this->tasks; |
| 36 |
|
| 37 |
// If strict mode is enabled, remove tasks that are hidden |
| 38 |
if ($strict) { |
| 39 |
$tasks = array_filter($tasks, function ($task) { |
| 40 |
return $task->isHidden() === false; |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
return $tasks; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Adds or updates a single task to the repository |
| 49 |
*/ |
| 50 |
public function addTask(TaskInterface $task, bool $save = true): void |
| 51 |
{ |
| 52 |
$this->tasks[$task->getId()] = $task; |
| 53 |
|
| 54 |
if ($save) { |
| 55 |
$this->saveTasksToDatabase(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Upgrade a task in the repository. Only replace existing tasks with same |
| 61 |
* identifier if the version is lower than the new task version. |
| 62 |
*/ |
| 63 |
public function upgradeTask(TaskInterface $task, bool $save = true): void |
| 64 |
{ |
| 65 |
$existingTask = $this->getTask($task->getId()); |
| 66 |
$taskExists = !empty($existingTask); |
| 67 |
|
| 68 |
$taskIsUpdatable = ( |
| 69 |
!$taskExists |
| 70 |
|| (version_compare($existingTask->getVersion(), $task->getVersion(), '<')) |
| 71 |
); |
| 72 |
|
| 73 |
if ($taskIsUpdatable === false) { |
| 74 |
return; |
| 75 |
} |
| 76 |
|
| 77 |
// Keep current status if new task does not want to reactivate on |
| 78 |
// upgrade and the existing task has a status |
| 79 |
if ($task->isReactivateOnUpgrade() === false) { |
| 80 |
$task->setStatusFromTask($existingTask); |
| 81 |
} |
| 82 |
|
| 83 |
// Upgrades existing tasks and add new tasks |
| 84 |
$this->addTask($task, $save); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Remove a task from the repository |
| 89 |
*/ |
| 90 |
public function removeTask(TaskInterface $task, bool $save = true): void |
| 91 |
{ |
| 92 |
unset($this->tasks[$task->getId()]); |
| 93 |
|
| 94 |
if ($save) { |
| 95 |
$this->saveTasksToDatabase(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Remove a task by its ID from the repository |
| 101 |
*/ |
| 102 |
public function removeTaskById(string $taskId, bool $save = true): void |
| 103 |
{ |
| 104 |
if (isset($this->tasks[$taskId])) { |
| 105 |
unset($this->tasks[$taskId]); |
| 106 |
} |
| 107 |
|
| 108 |
if ($save) { |
| 109 |
$this->saveTasksToDatabase(); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Load tasks from the WordPress database |
| 115 |
*/ |
| 116 |
private function loadTasksFromDatabase(): void |
| 117 |
{ |
| 118 |
$storedTasks = get_option(self::OPTION_NAME, []); |
| 119 |
$this->tasks = is_array($storedTasks) ? $storedTasks : []; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Save tasks to the WordPress database |
| 124 |
*/ |
| 125 |
public function saveTasksToDatabase(): void |
| 126 |
{ |
| 127 |
update_option(self::OPTION_NAME, $this->tasks, false); |
| 128 |
} |
| 129 |
} |
| 130 |
|