-
Notifications
You must be signed in to change notification settings - Fork 5
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 #637 from patchlevel/subscription-engine-filter-ev…
…ents filter events in subscription engine
- Loading branch information
Showing
17 changed files
with
271 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,6 +154,7 @@ deptrac: | |
- Attribute | ||
- Clock | ||
- Message | ||
- MetadataEvent | ||
- MetadataSubscriber | ||
- Repository | ||
- Schema | ||
|
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
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Store\Criteria; | ||
|
||
final class EventsCriterion | ||
{ | ||
public function __construct( | ||
/** @var list<string> */ | ||
public readonly array $events, | ||
) { | ||
} | ||
} |
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
84 changes: 84 additions & 0 deletions
84
src/Subscription/Engine/EventFilteredStoreMessageLoader.php
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Metadata\Event\EventMetadataFactory; | ||
use Patchlevel\EventSourcing\Store\Criteria\Criteria; | ||
use Patchlevel\EventSourcing\Store\Criteria\EventsCriterion; | ||
use Patchlevel\EventSourcing\Store\Criteria\FromIndexCriterion; | ||
use Patchlevel\EventSourcing\Store\Store; | ||
use Patchlevel\EventSourcing\Store\Stream; | ||
use Patchlevel\EventSourcing\Subscription\Subscriber\MetadataSubscriberAccessor; | ||
use Patchlevel\EventSourcing\Subscription\Subscriber\SubscriberAccessorRepository; | ||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
|
||
use function array_keys; | ||
|
||
final class EventFilteredStoreMessageLoader implements MessageLoader | ||
{ | ||
public function __construct( | ||
private readonly Store $store, | ||
private readonly EventMetadataFactory $eventMetadataFactory, | ||
private readonly SubscriberAccessorRepository $subscriberRepository, | ||
) { | ||
} | ||
|
||
/** @param list<Subscription> $subscriptions */ | ||
public function load(int $startIndex, array $subscriptions): Stream | ||
{ | ||
$criteria = new Criteria(new FromIndexCriterion($startIndex)); | ||
|
||
$events = $this->events($subscriptions); | ||
|
||
if ($events !== []) { | ||
$criteria = $criteria->add(new EventsCriterion($events)); | ||
} | ||
|
||
return $this->store->load($criteria); | ||
} | ||
|
||
/** | ||
* @param list<Subscription> $subscriptions | ||
* | ||
* @return list<string> | ||
*/ | ||
private function events(array $subscriptions): array | ||
{ | ||
$eventNames = []; | ||
|
||
foreach ($subscriptions as $subscription) { | ||
$subscriber = $this->subscriberRepository->get($subscription->id()); | ||
|
||
if (!$subscriber instanceof MetadataSubscriberAccessor) { | ||
return []; | ||
} | ||
|
||
$events = $subscriber->events(); | ||
|
||
foreach ($events as $event) { | ||
if ($event === '*') { | ||
return []; | ||
} | ||
|
||
$metadata = $this->eventMetadataFactory->metadata($event); | ||
|
||
$eventNames[$metadata->name] = true; | ||
|
||
foreach ($metadata->aliases as $alias) { | ||
$eventNames[$alias] = true; | ||
} | ||
} | ||
} | ||
|
||
return array_keys($eventNames); | ||
} | ||
|
||
public function lastIndex(): int | ||
{ | ||
$stream = $this->store->load(null, 1, null, true); | ||
|
||
return $stream->index() ?: 0; | ||
} | ||
} |
Oops, something went wrong.