-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskPool.php
More file actions
53 lines (40 loc) · 1.11 KB
/
TaskPool.php
File metadata and controls
53 lines (40 loc) · 1.11 KB
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
<?php
declare(strict_types=1);
namespace Netlogix\DependencyResolver;
use ArrayIterator;
use InvalidArgumentException;
use Traversable;
class TaskPool implements ResettableTaskPoolInterface
{
private array $tasks = [];
public function __construct(iterable $tasks = [])
{
foreach ($tasks as $task) {
$this->addTask($task);
}
}
public function addTask(TaskInterface $task): self
{
if (isset($this->tasks[$task->getName()])) {
throw new InvalidArgumentException('Task already exists');
}
$this->tasks[$task->getName()] = $task;
return $this;
}
public function getTask(string $name): TaskInterface
{
if (!isset($this->tasks[$name])) {
throw new InvalidArgumentException(sprintf('Task "%s" does not exist', $name));
}
return $this->tasks[$name];
}
public function getIterator(): Traversable
{
return new ArrayIterator($this->tasks);
}
public function reset(): self
{
array_map(fn ($t) => $t->reset(), $this->tasks);
return $this;
}
}