instances added god may help
This commit is contained in:
@@ -4,39 +4,42 @@ namespace MintyPHP\Service\Scheduler;
|
||||
|
||||
use MintyPHP\Service\Scheduler\Handler\ScheduledJobHandlerInterface;
|
||||
use MintyPHP\Service\Scheduler\Handler\UserLifecycleJobHandler;
|
||||
use MintyPHP\Service\User\UserLifecycleService;
|
||||
|
||||
class ScheduledJobRegistry
|
||||
{
|
||||
public const USER_LIFECYCLE_RUN = 'user_lifecycle_run';
|
||||
|
||||
/**
|
||||
* Maps job_key => handler class name (must implement ScheduledJobHandlerInterface).
|
||||
* 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.
|
||||
* 3. Add one line here: self::YOUR_JOB_KEY => YourJobHandler::class,
|
||||
* 3. Add one line in __construct(): self::YOUR_JOB_KEY => new YourJobHandler(...),
|
||||
*
|
||||
* No other file needs to be changed.
|
||||
*
|
||||
* @return array<string, class-string<ScheduledJobHandlerInterface>>
|
||||
* @var array<string, ScheduledJobHandlerInterface>
|
||||
*/
|
||||
private static function handlers(): array
|
||||
private array $handlers;
|
||||
|
||||
public function __construct(UserLifecycleService $userLifecycleService)
|
||||
{
|
||||
return [
|
||||
self::USER_LIFECYCLE_RUN => UserLifecycleJobHandler::class,
|
||||
$this->handlers = [
|
||||
self::USER_LIFECYCLE_RUN => new UserLifecycleJobHandler($userLifecycleService),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all job definitions keyed by job_key.
|
||||
* Consumed by ScheduledJobService::ensureSystemJobs() to sync registry with the database.
|
||||
* Consumed by ScheduledJobService->ensureSystemJobs() to sync registry with the database.
|
||||
*/
|
||||
public static function definitions(): array
|
||||
public function definitions(): array
|
||||
{
|
||||
$result = [];
|
||||
foreach (self::handlers() as $jobKey => $handlerClass) {
|
||||
$result[$jobKey] = $handlerClass::definition();
|
||||
foreach ($this->handlers as $jobKey => $handler) {
|
||||
$result[$jobKey] = $handler->definition();
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
@@ -44,14 +47,13 @@ class ScheduledJobRegistry
|
||||
/**
|
||||
* Returns the definition for a single job key, or null if not registered.
|
||||
*/
|
||||
public static function get(string $jobKey): ?array
|
||||
public function get(string $jobKey): ?array
|
||||
{
|
||||
$jobKey = trim($jobKey);
|
||||
if ($jobKey === '') {
|
||||
return null;
|
||||
}
|
||||
$handlers = self::handlers();
|
||||
return isset($handlers[$jobKey]) ? $handlers[$jobKey]::definition() : null;
|
||||
return isset($this->handlers[$jobKey]) ? $this->handlers[$jobKey]->definition() : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,10 +65,9 @@ class ScheduledJobRegistry
|
||||
* @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}
|
||||
*/
|
||||
public static function execute(string $jobKey, ?int $actorUserId): array
|
||||
public function execute(string $jobKey, ?int $actorUserId): array
|
||||
{
|
||||
$handlers = self::handlers();
|
||||
if (!isset($handlers[$jobKey])) {
|
||||
if (!isset($this->handlers[$jobKey])) {
|
||||
return [
|
||||
'status' => 'failed',
|
||||
'error_code' => 'job_not_supported',
|
||||
@@ -74,6 +75,6 @@ class ScheduledJobRegistry
|
||||
'result' => [],
|
||||
];
|
||||
}
|
||||
return $handlers[$jobKey]::execute($actorUserId);
|
||||
return $this->handlers[$jobKey]->execute($actorUserId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user