forked from aternosorg/taskmaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IteratorTaskFactory.php
54 lines (48 loc) · 1.19 KB
/
IteratorTaskFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Aternos\Taskmaster\Task;
use Iterator;
/**
* Class IteratorTaskFactory
*
* A {@link TaskFactory} that creates tasks from an iterator, e.g. a {@link \DirectoryIterator}
*
* @package Aternos\Taskmaster\Task
*/
class IteratorTaskFactory extends TaskFactory
{
/**
* @param Iterator $iterator
* @param string $taskClass
*/
public function __construct(
protected Iterator $iterator,
protected string $taskClass
)
{
}
/**
* @inheritDoc
*/
public function createNextTask(?string $group): ?TaskInterface
{
if (!$this->iterator->valid()) {
return null;
}
$task = $this->createTask($this->iterator->current());
$this->iterator->next();
return $task;
}
/**
* Create a task from the iterator value
*
* This can be overwritten to create a different task from the iterator value
* or pass further arguments to the task constructor.
*
* @param mixed $iteratorValue
* @return TaskInterface
*/
protected function createTask(mixed $iteratorValue): TaskInterface
{
return new $this->taskClass($iteratorValue);
}
}