← All changes
|
app/Features/TaskManagement/TaskManagementController.php
+98
-0
2.0.0
→
2.1.1
View file →
| @@ -1,0 +1,98 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +declare(strict_types=1); | |
| 4 | + | |
| 5 | +namespace Metricool\Features\TaskManagement; | |
| 6 | + | |
| 7 | +if (!defined('ABSPATH')) { | |
| 8 | + exit; | |
| 9 | +} | |
| 10 | + | |
| 11 | +use Metricool\Interfaces\FeatureInterface; | |
| 12 | +use Metricool\Interfaces\TaskInterface; | |
| 13 | +use Metricool\Services\DashboardService; | |
| 14 | +use Metricool\Traits\HasAllowlistControl; | |
| 15 | + | |
| 16 | +class TaskManagementController implements FeatureInterface | |
| 17 | +{ | |
| 18 | + use HasAllowlistControl; | |
| 19 | + | |
| 20 | + private TaskManagementEndpoints $endpoints; | |
| 21 | + private TaskManagementService $service; | |
| 22 | + private TaskManagementListener $listener; | |
| 23 | + private DashboardService $dashboard; | |
| 24 | + | |
| 25 | + public function __construct(TaskManagementService $service, TaskManagementEndpoints $endpoints, TaskManagementListener $listener, DashboardService $dashboard) | |
| 26 | + { | |
| 27 | + $this->service = $service; | |
| 28 | + $this->endpoints = $endpoints; | |
| 29 | + $this->listener = $listener; | |
| 30 | + $this->dashboard = $dashboard; | |
| 31 | + } | |
| 32 | + | |
| 33 | + public function register(): void | |
| 34 | + { | |
| 35 | + $this->endpoints->register(); | |
| 36 | + | |
| 37 | + if ($this->userCanManage() && $this->dashboard->isOnboardingCompleted()) { | |
| 38 | + $this->listener->listen(); | |
| 39 | + $this->initiateTasks(); | |
| 40 | + } | |
| 41 | + | |
| 42 | + add_action('metricool_plugin_version_upgrade', [$this, 'upgradeTasks']); | |
| 43 | + } | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * This method returns an array of Task class-strings that should be added | |
| 47 | + * to the database. | |
| 48 | + * | |
| 49 | + * @internal New tasks should be added here. Upgrade the task version if the | |
| 50 | + * task should be updated. If a task should be removed, remove the task from | |
| 51 | + * this list. | |
| 52 | + * | |
| 53 | + * @return array<int,class-string<TaskInterface>> Array of Task class-strings | |
| 54 | + */ | |
| 55 | + private function getTaskObjects(): array | |
| 56 | + { | |
| 57 | + return [ | |
| 58 | + Tasks\TwitterTask::class, | |
| 59 | + Tasks\LinkedInTask::class, | |
| 60 | + Tasks\HistoricalDataTask::class, | |
| 61 | + Tasks\FirstConnectionTask::class, | |
| 62 | + Tasks\SchedulePostTask::class, | |
| 63 | + ]; | |
| 64 | + } | |
| 65 | + | |
| 66 | + /** | |
| 67 | + * This method adds the initial tasks to the database if they are not | |
| 68 | + * already present. | |
| 69 | + * @throws \Exception If task class cannot be instantiated | |
| 70 | + */ | |
| 71 | + private function initiateTasks(): void | |
| 72 | + { | |
| 73 | + if ($this->service->hasTasks()) { | |
| 74 | + return; | |
| 75 | + } | |
| 76 | + | |
| 77 | + $this->service->addTasks( | |
| 78 | + $this->getTaskObjects() | |
| 79 | + ); | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * This method makes sure that if new tasks are added in the update that | |
| 84 | + * these tasks are added in the database. Existing tasks will be updated | |
| 85 | + * if the version is higher than the current existing task with the same id. | |
| 86 | + * @throws \Exception If task class cannot be instantiated | |
| 87 | + */ | |
| 88 | + public function upgradeTasks(): void | |
| 89 | + { | |
| 90 | + if ($this->service->hasTasks() === false) { | |
| 91 | + return; // Tasks will be added by initiateTasks() | |
| 92 | + } | |
| 93 | + | |
| 94 | + $this->service->upgradeTasks( | |
| 95 | + $this->getTaskObjects() | |
| 96 | + ); | |
| 97 | + } | |
| 98 | +} | |