Skip to content
Prev Previous commit
Next Next commit
Extract task status generation to a new method
  • Loading branch information
Vectorial1024 committed Apr 3, 2025
commit dac44e97d9d1a743475635d4164e11ff32f8b95f
17 changes: 14 additions & 3 deletions src/AsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AsyncTask
* If null, the task will generate an unsaved random ID when it is started.
* @var string|null
*/
protected string|null $taskID;
private string|null $taskID;

/**
* The process that is actually running this task. Tasks that are not started will have null here.
Expand Down Expand Up @@ -131,6 +131,18 @@ public function __unserialize($data): void
] = $data;
}

/**
* Returns a status object for the started AsyncTask.
*
* If this task does not have an explicit task ID, a new one will be generated on-the-fly.
* @return AsyncTaskStatus The status object for the started AsyncTask.
*/
protected function getTaskStatusObject(): AsyncTaskStatus
{
$taskID = $this->taskID ?? Str::ulid()->toString();
return new AsyncTaskStatus($taskID);
}

/**
* Inside an available PHP process, runs this AsyncTask instance.
*
Expand Down Expand Up @@ -192,8 +204,7 @@ public function run(): void
public function start(): AsyncTaskStatus
{
// prepare the task details
$taskID = $this->taskID ?? Str::ulid()->toString();
$taskStatus = new AsyncTaskStatus($taskID);
$taskStatus = $this->getTaskStatusObject();

// prepare the runner command
$serializedTask = $this->toBase64Serial();
Expand Down
3 changes: 1 addition & 2 deletions src/FakeAsyncTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function run(): void
public function start(): AsyncTaskStatus
{
// todo fake version
$taskID = $this->taskID ?? Str::ulid()->toString();
return new AsyncTaskStatus($taskID);
return $this->getTaskStatusObject();
}
}