Lines
0.00%
0 / 11
Functions and Methods
0.00%
0 / 7
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| QueuedWork | 0.00% | 0 / 11 | 0.00% | 0 / 7 | 90 | 0.00% | 0 / 1 | |||
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| forDroppableEvent | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| forStreamEmission | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| withEvent | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| isDroppable | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| discard | 0.00% | 0 / 2 | 0.00% | 0 / 1 | 6 | |||||
| __invoke | 0.00% | 0 / 4 | 0.00% | 0 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Dispatch; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | ||
| 9 | final readonly class QueuedWork | |
| 10 | { | |
| 11 | /** | |
| 12 | * @param Closure(object): void|Closure(): void $handler | |
| 13 | * @param ?Closure(QueueDiscardReason): void $onDiscarded | |
| 14 | */ | |
| 15 | private function __construct( | |
| 16 | public ?object $event, | |
| 17 | private Closure $handler, | |
| 18 | private ?Closure $onDiscarded = null, | |
| 19 | ) {} | |
| 20 | ||
| 21 | /** | |
| 22 | * @param Closure(object): void $handler | |
| 23 | * @param ?Closure(QueueDiscardReason): void $onDiscarded | |
| 24 | */ | |
| 25 | public static function forDroppableEvent(object $event, Closure $handler, ?Closure $onDiscarded = null): self | |
| 26 | { | |
| 27 | return new self($event, $handler, $onDiscarded); | |
| 28 | } | |
| 29 | ||
| 30 | /** @param Closure(): void $emission */ | |
| 31 | public static function forStreamEmission(Closure $emission): self | |
| 32 | { | |
| 33 | return new self(null, $emission); | |
| 34 | } | |
| 35 | ||
| 36 | public function withEvent(object $event): self | |
| 37 | { | |
| 38 | return new self($event, $this->handler, $this->onDiscarded); | |
| 39 | } | |
| 40 | ||
| 41 | public function isDroppable(): bool | |
| 42 | { | |
| 43 | return $this->event !== null; | |
| 44 | } | |
| 45 | ||
| 46 | public function discard(QueueDiscardReason $reason): void | |
| 47 | { | |
| 48 | if ($this->onDiscarded !== null) { | |
| 49 | ($this->onDiscarded)($reason); | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | public function __invoke(): void | |
| 54 | { | |
| 55 | if ($this->event === null) { | |
| 56 | ($this->handler)(); | |
| 57 | ||
| 58 | return; | |
| 59 | } | |
| 60 | ||
| 61 | ($this->handler)($this->event); | |
| 62 | } | |
| 63 | } |