-
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.
introduce message loader in subscription engine
- Loading branch information
1 parent
95c56c8
commit 4ed8b77
Showing
6 changed files
with
163 additions
and
86 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Store\Criteria\Criteria; | ||
use Patchlevel\EventSourcing\Store\Criteria\FromIndexCriterion; | ||
use Patchlevel\EventSourcing\Store\Store; | ||
use Patchlevel\EventSourcing\Store\Stream; | ||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
|
||
final class DefaultMessageLoader implements MessageLoader | ||
{ | ||
public function __construct( | ||
private readonly Store $store, | ||
) { | ||
} | ||
|
||
/** @param list<Subscription> $subscriptions */ | ||
public function load(int $startIndex, array $subscriptions): Stream | ||
{ | ||
return $this->store->load(new Criteria(new FromIndexCriterion($startIndex))); | ||
} | ||
|
||
public function lastIndex(): int | ||
{ | ||
$stream = $this->store->load(null, 1, null, true); | ||
|
||
return $stream->index() ?: 0; | ||
} | ||
} |
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,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 EventFilteredMessageLoader implements MessageLoader | ||
{ | ||
public function __construct( | ||
private readonly Store $store, | ||
private readonly EventMetadataFactory $eventMetadataFactory, | ||
Check failure on line 23 in src/Subscription/Engine/EventFilteredMessageLoader.php GitHub Actions / Static Analysis by Deptrac (locked, 8.3, ubuntu-latest)
|
||
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; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Subscription\Engine; | ||
|
||
use Patchlevel\EventSourcing\Store\Stream; | ||
use Patchlevel\EventSourcing\Subscription\Subscription; | ||
|
||
interface MessageLoader | ||
{ | ||
/** @param list<Subscription> $subscriptions */ | ||
public function load(int $startIndex, array $subscriptions): Stream; | ||
|
||
public function lastIndex(): int; | ||
} |
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