| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Scheduler\Common; |
| 4 |
|
| 5 |
use ArrayObject; |
| 6 |
use InvalidArgumentException; |
| 7 |
use Ramsey\Uuid\UuidInterface; |
| 8 |
use React\Promise\PromiseInterface; |
| 9 |
use SplObjectStorage; |
| 10 |
|
| 11 |
/** |
| 12 |
* Tracks pending {@see PromiseInterface promises} per UUID |
| 13 |
* |
| 14 |
* Provides helpers to add, remove, and detach promises keyed by a {@see UuidInterface}. |
| 15 |
*/ |
| 16 |
trait Promises |
| 17 |
{ |
| 18 |
/** @var SplObjectStorage<UuidInterface, ArrayObject<int, PromiseInterface<mixed>>> */ |
| 19 |
protected SplObjectStorage $promises; |
| 20 |
|
| 21 |
/** |
| 22 |
* Add the given promise for the specified UUID |
| 23 |
* |
| 24 |
* **Example Usage:** |
| 25 |
* |
| 26 |
* $promise = work(); |
| 27 |
* $promises->addPromise($uuid, $promise); |
| 28 |
* |
| 29 |
* @param UuidInterface $uuid |
| 30 |
* @param PromiseInterface<mixed> $promise |
| 31 |
* |
| 32 |
* @return $this |
| 33 |
*/ |
| 34 |
protected function addPromise(UuidInterface $uuid, PromiseInterface $promise): static |
| 35 |
{ |
| 36 |
if (! $this->promises->offsetExists($uuid)) { |
| 37 |
$this->promises->offsetSet($uuid, new ArrayObject()); |
| 38 |
} |
| 39 |
|
| 40 |
$this->promises[$uuid][] = $promise; |
| 41 |
|
| 42 |
return $this; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Remove the given promise for the specified UUID |
| 47 |
* |
| 48 |
* **Example Usage:** |
| 49 |
* |
| 50 |
* $promise->finally(function () use ($uuid, $promise) { |
| 51 |
* $promises->removePromise($uuid, $promise); |
| 52 |
* }) |
| 53 |
* |
| 54 |
* @param UuidInterface $uuid |
| 55 |
* @param PromiseInterface<mixed> $promise |
| 56 |
* |
| 57 |
* @return $this |
| 58 |
* |
| 59 |
* @throws InvalidArgumentException If the given UUID doesn't have any registered promises or when the specified |
| 60 |
* UUID promises doesn't contain the provided promise |
| 61 |
*/ |
| 62 |
protected function removePromise(UuidInterface $uuid, PromiseInterface $promise): static |
| 63 |
{ |
| 64 |
if (! $this->promises->offsetExists($uuid)) { |
| 65 |
throw new InvalidArgumentException( |
| 66 |
sprintf('There are no registered promises for UUID %s', $uuid->toString()) |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
foreach ($this->promises[$uuid] as $k => $v) { |
| 71 |
if ($v === $promise) { |
| 72 |
unset($this->promises[$uuid][$k]); |
| 73 |
|
| 74 |
return $this; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
throw new InvalidArgumentException( |
| 79 |
sprintf('There is no such promise for UUID %s', $uuid->toString()) |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Detach and return promises for the given UUID, if any |
| 85 |
* |
| 86 |
* **Example Usage:** |
| 87 |
* |
| 88 |
* foreach ($promises->detachPromises($uuid) as $promise) { |
| 89 |
* $promise->cancel(); |
| 90 |
* } |
| 91 |
* |
| 92 |
* @param UuidInterface $uuid |
| 93 |
* |
| 94 |
* @return array<int, PromiseInterface<mixed>> |
| 95 |
*/ |
| 96 |
protected function detachPromises(UuidInterface $uuid): array |
| 97 |
{ |
| 98 |
if (! $this->promises->offsetExists($uuid)) { |
| 99 |
return []; |
| 100 |
} |
| 101 |
|
| 102 |
$promises = $this->promises[$uuid]; |
| 103 |
$this->promises->offsetUnset($uuid); |
| 104 |
|
| 105 |
return $promises->getArrayCopy(); |
| 106 |
} |
| 107 |
} |