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

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

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