Files
breadcrumb-the-shire/lib/Service/Scheduler/ScheduledJobRegistry.php

87 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace MintyPHP\Service\Scheduler;
2026-03-05 11:17:42 +01:00
use MintyPHP\Service\Audit\SystemAuditService;
use MintyPHP\Service\Scheduler\Handler\ScheduledJobHandlerInterface;
2026-03-04 15:56:58 +01:00
use MintyPHP\Service\Scheduler\Handler\SystemAuditPurgeJobHandler;
use MintyPHP\Service\Scheduler\Handler\UserLifecycleJobHandler;
2026-02-23 12:58:19 +01:00
use MintyPHP\Service\User\UserLifecycleService;
class ScheduledJobRegistry
{
public const USER_LIFECYCLE_RUN = 'user_lifecycle_run';
2026-03-04 15:56:58 +01:00
public const SYSTEM_AUDIT_PURGE = 'system_audit_purge';
/**
2026-02-23 12:58:19 +01:00
* Maps job_key => handler instance (must implement ScheduledJobHandlerInterface).
*
* To register a new job:
* 1. Create lib/Service/Scheduler/Handler/YourJobHandler.php implementing the interface.
* 2. Add a public const for the job key above.
2026-02-23 12:58:19 +01:00
* 3. Add one line in __construct(): self::YOUR_JOB_KEY => new YourJobHandler(...),
*
* No other file needs to be changed.
*
2026-02-23 12:58:19 +01:00
* @var array<string, ScheduledJobHandlerInterface>
*/
2026-02-23 12:58:19 +01:00
private array $handlers;
2026-03-04 15:56:58 +01:00
public function __construct(
UserLifecycleService $userLifecycleService,
SystemAuditService $systemAuditService
2026-03-05 11:17:42 +01:00
) {
2026-02-23 12:58:19 +01:00
$this->handlers = [
self::USER_LIFECYCLE_RUN => new UserLifecycleJobHandler($userLifecycleService),
2026-03-04 15:56:58 +01:00
self::SYSTEM_AUDIT_PURGE => new SystemAuditPurgeJobHandler($systemAuditService),
];
}
/**
* Returns all job definitions keyed by job_key.
2026-02-23 12:58:19 +01:00
* Consumed by ScheduledJobService->ensureSystemJobs() to sync registry with the database.
*/
2026-02-23 12:58:19 +01:00
public function definitions(): array
{
$result = [];
2026-02-23 12:58:19 +01:00
foreach ($this->handlers as $jobKey => $handler) {
$result[$jobKey] = $handler->definition();
}
return $result;
}
/**
* Returns the definition for a single job key, or null if not registered.
*/
2026-02-23 12:58:19 +01:00
public function get(string $jobKey): ?array
{
$jobKey = trim($jobKey);
if ($jobKey === '') {
return null;
}
2026-02-23 12:58:19 +01:00
return isset($this->handlers[$jobKey]) ? $this->handlers[$jobKey]->definition() : null;
}
/**
* Dispatches execution to the registered handler for the given job key and returns
* a normalized result envelope. Unknown job keys return status 'failed' with
* error_code 'job_not_supported' rather than throwing an exception.
*
* @param string $jobKey Registered job identifier (see class constants).
* @param int|null $actorUserId Null for scheduler-triggered runs; user ID for manual runs.
* @return array{status:string,error_code:?string,error_message:?string,result:array}
*/
2026-02-23 12:58:19 +01:00
public function execute(string $jobKey, ?int $actorUserId): array
{
2026-02-23 12:58:19 +01:00
if (!isset($this->handlers[$jobKey])) {
return [
'status' => 'failed',
'error_code' => 'job_not_supported',
'error_message' => null,
'result' => [],
];
}
2026-02-23 12:58:19 +01:00
return $this->handlers[$jobKey]->execute($actorUserId);
}
}