-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from patchlevel/add-auto-setup-listener
add auto setup listener
- Loading branch information
Showing
3 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcingBundle\RequestListener; | ||
|
||
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngine; | ||
use Patchlevel\EventSourcing\Subscription\Engine\SubscriptionEngineCriteria; | ||
use Patchlevel\EventSourcing\Subscription\Status; | ||
use Symfony\Component\Console\Event\ConsoleCommandEvent; | ||
use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
|
||
final class AutoSetupListener | ||
{ | ||
/** | ||
* @param list<string>|null $ids | ||
* @param list<string>|null $groups | ||
*/ | ||
public function __construct( | ||
private readonly SubscriptionEngine $subscriptionEngine, | ||
private readonly array|null $ids, | ||
private readonly array|null $groups, | ||
) { | ||
} | ||
|
||
public function onKernelRequest(RequestEvent $event): void | ||
{ | ||
if (!$event->isMainRequest()) { | ||
return; | ||
} | ||
|
||
$this->run(); | ||
} | ||
|
||
public function onConsoleCommand(ConsoleCommandEvent $event): void | ||
{ | ||
$this->run(); | ||
} | ||
|
||
private function run(): void | ||
{ | ||
$subscriptions = $this->subscriptionEngine->subscriptions( | ||
new SubscriptionEngineCriteria( | ||
$this->ids, | ||
$this->groups, | ||
), | ||
); | ||
|
||
$ids = []; | ||
|
||
foreach ($subscriptions as $subscription) { | ||
if ($subscription->status() !== Status::New) { | ||
continue; | ||
} | ||
|
||
$ids[] = $subscription->id(); | ||
} | ||
|
||
$this->subscriptionEngine->setup( | ||
new SubscriptionEngineCriteria($ids), | ||
true, | ||
); | ||
} | ||
} |