Skip to content

Commit dac44e9

Browse files
committed
Extract task status generation to a new method
1 parent c8d5a79 commit dac44e9

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/AsyncTask.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class AsyncTask
3232
* If null, the task will generate an unsaved random ID when it is started.
3333
* @var string|null
3434
*/
35-
protected string|null $taskID;
35+
private string|null $taskID;
3636

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

134+
/**
135+
* Returns a status object for the started AsyncTask.
136+
*
137+
* If this task does not have an explicit task ID, a new one will be generated on-the-fly.
138+
* @return AsyncTaskStatus The status object for the started AsyncTask.
139+
*/
140+
protected function getTaskStatusObject(): AsyncTaskStatus
141+
{
142+
$taskID = $this->taskID ?? Str::ulid()->toString();
143+
return new AsyncTaskStatus($taskID);
144+
}
145+
134146
/**
135147
* Inside an available PHP process, runs this AsyncTask instance.
136148
*
@@ -192,8 +204,7 @@ public function run(): void
192204
public function start(): AsyncTaskStatus
193205
{
194206
// prepare the task details
195-
$taskID = $this->taskID ?? Str::ulid()->toString();
196-
$taskStatus = new AsyncTaskStatus($taskID);
207+
$taskStatus = $this->getTaskStatusObject();
197208

198209
// prepare the runner command
199210
$serializedTask = $this->toBase64Serial();

src/FakeAsyncTask.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function run(): void
4949
public function start(): AsyncTaskStatus
5050
{
5151
// todo fake version
52-
$taskID = $this->taskID ?? Str::ulid()->toString();
53-
return new AsyncTaskStatus($taskID);
52+
return $this->getTaskStatusObject();
5453
}
5554
}

0 commit comments

Comments
 (0)