Lines
100.00%
8 / 8
Functions and Methods
100.00%
2 / 2
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| StartWithCurrentStateOperator | 100.00% | 8 / 8 | 100.00% | 2 / 2 | 3 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| attach | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Support\Stream; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | use Stewart\Contracts\State\StateChange; | |
| 9 | use Stewart\Contracts\Subscription; | |
| 10 | use Stewart\Support\State\CurrentStateReader; | |
| 11 | ||
| 12 | /** | |
| 13 | * @internal | |
| 14 | * @implements StreamSource<StateChange> | |
| 15 | */ | |
| 16 | final readonly class StartWithCurrentStateOperator implements StreamSource | |
| 17 | { | |
| 18 | /** @param StreamSource<StateChange> $source */ | |
| 19 | public function __construct( | |
| 20 | private StreamSource $source, | |
| 21 | private CurrentStateReader $currentStates, | |
| 22 | ) {} | |
| 23 | ||
| 24 | public function attach(SubscriptionScope $scope, Closure $downstream): Subscription | |
| 25 | { | |
| 26 | $subscription = $this->source->attach($scope, $downstream); | |
| 27 | ||
| 28 | foreach ($this->currentStates->readCurrentStates()->sortedByEntityId() as $state) { | |
| 29 | $seed = StateChange::fromCurrentState($state); | |
| 30 | $scope->emit(static function () use ($downstream, $seed): void { | |
| 31 | $downstream($seed); | |
| 32 | }); | |
| 33 | } | |
| 34 | ||
| 35 | return $subscription; | |
| 36 | } | |
| 37 | } |