ClaimedTaskRunner.php
1 month ago
Cli.php
1 month ago
CronCommand.php
1 month ago
DaemonRunner.php
1 month ago
ExecutionLimitOverride.php
1 month ago
ScheduledTaskResolver.php
1 month ago
ScheduledTasksLister.php
1 month ago
TaskAdder.php
1 month ago
TaskCanceller.php
1 month ago
TaskRunner.php
1 month ago
TaskTrigger.php
1 month ago
WorkerTypesCatalog.php
1 month ago
index.php
1 month ago
ScheduledTaskResolver.php
46 lines
| 1 | <?php declare(strict_types = 1); |
| 2 | |
| 3 | namespace MailPoet\Cron\CliCommands; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | use MailPoet\Entities\ScheduledTaskEntity; |
| 10 | use MailPoet\Newsletter\Sending\ScheduledTasksRepository; |
| 11 | |
| 12 | class ScheduledTaskResolver { |
| 13 | private ScheduledTasksRepository $scheduledTasksRepository; |
| 14 | |
| 15 | public function __construct( |
| 16 | ScheduledTasksRepository $scheduledTasksRepository |
| 17 | ) { |
| 18 | $this->scheduledTasksRepository = $scheduledTasksRepository; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Finds a task by ID, treating soft-deleted rows as missing. When $expectedType is given the row |
| 23 | * must match it. The status whitelist stays with each caller. |
| 24 | */ |
| 25 | public function resolveById(int $taskId, ?string $expectedType = null): ScheduledTaskEntity { |
| 26 | $task = $this->scheduledTasksRepository->findOneById($taskId); |
| 27 | if (!$task instanceof ScheduledTaskEntity || $task->getDeletedAt() !== null) { |
| 28 | throw new InvalidArgumentException("No task with ID {$taskId} found."); |
| 29 | } |
| 30 | |
| 31 | if ($expectedType !== null && $task->getType() !== $expectedType) { |
| 32 | $actualType = (string)$task->getType(); |
| 33 | throw new InvalidArgumentException("Task {$taskId} is of type '{$actualType}', not '{$expectedType}'."); |
| 34 | } |
| 35 | |
| 36 | return $task; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * The displayable status, mapping the NULL "running" placeholder to its virtual name. |
| 41 | */ |
| 42 | public function nameStatus(ScheduledTaskEntity $task): string { |
| 43 | return $task->getStatus() ?? ScheduledTaskEntity::VIRTUAL_STATUS_RUNNING; |
| 44 | } |
| 45 | } |
| 46 |