diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index dcf8c680d..8d4101dc7 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -15,11 +15,6 @@ parameters: count: 1 path: src/Projection/Projection/Store/DoctrineStore.php - - - message: "#^Method Patchlevel\\\\EventSourcing\\\\Projection\\\\Projector\\\\InMemoryProjectorRepository\\:\\:projectors\\(\\) should return array\\ but returns array\\\\.$#" - count: 1 - path: src/Projection/Projector/InMemoryProjectorRepository.php - - message: "#^Parameter \\#2 \\$data of method Patchlevel\\\\Hydrator\\\\Hydrator\\:\\:hydrate\\(\\) expects array\\, mixed given\\.$#" count: 1 diff --git a/src/Projection/Projectionist/DefaultProjectionist.php b/src/Projection/Projectionist/DefaultProjectionist.php index d4b294b07..1367bb7d5 100644 --- a/src/Projection/Projectionist/DefaultProjectionist.php +++ b/src/Projection/Projectionist/DefaultProjectionist.php @@ -15,7 +15,6 @@ use Patchlevel\EventSourcing\Projection\Projection\ProjectionError; use Patchlevel\EventSourcing\Projection\Projection\ProjectionStatus; use Patchlevel\EventSourcing\Projection\Projection\Store\ProjectionStore; -use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository; use Patchlevel\EventSourcing\Store\Criteria; use Patchlevel\EventSourcing\Store\Store; use Psr\Log\LoggerInterface; @@ -30,12 +29,13 @@ final class DefaultProjectionist implements Projectionist private const RETRY_LIMIT = 5; /** @var array|null */ - private array|null $projectors = null; + private array|null $projectorIndex = null; + /** @param iterable $projectors */ public function __construct( private readonly Store $streamableMessageStore, private readonly ProjectionStore $projectionStore, - private readonly ProjectorRepository $projectorRepository, + private readonly iterable $projectors, private readonly ProjectorMetadataFactory $metadataFactory = new AttributeProjectorMetadataFactory(), private readonly LoggerInterface|null $logger = null, ) { @@ -442,9 +442,8 @@ public function reactivate(ProjectionCriteria $criteria = new ProjectionCriteria public function projections(): ProjectionCollection { $projections = $this->projectionStore->all(); - $projectors = $this->projectors(); - foreach ($projectors as $projector) { + foreach ($this->projectors as $projector) { $projectorId = $this->projectorId($projector); if ($projections->has($projectorId)) { @@ -529,25 +528,17 @@ private function handleMessage(int $index, Message $message, Projection $project private function projector(string $projectorId): object|null { - $projectors = $this->projectors(); + if ($this->projectorIndex === null) { + $this->projectorIndex = []; - return $projectors[$projectorId] ?? null; - } - - /** @return array */ - private function projectors(): array - { - if ($this->projectors === null) { - $this->projectors = []; - - foreach ($this->projectorRepository->projectors() as $projector) { + foreach ($this->projectors as $projector) { $projectorId = $this->projectorId($projector); - $this->projectors[$projectorId] = $projector; + $this->projectorIndex[$projectorId] = $projector; } } - return $this->projectors; + return $this->projectorIndex[$projectorId] ?? null; } private function handleOutdatedProjections(ProjectionCollection $projections): void diff --git a/src/Projection/Projector/InMemoryProjectorRepository.php b/src/Projection/Projector/InMemoryProjectorRepository.php deleted file mode 100644 index ef2aac7df..000000000 --- a/src/Projection/Projector/InMemoryProjectorRepository.php +++ /dev/null @@ -1,20 +0,0 @@ - $projectors */ - public function __construct( - private readonly iterable $projectors = [], - ) { - } - - /** @return list */ - public function projectors(): array - { - return [...$this->projectors]; - } -} diff --git a/src/Projection/Projector/ProjectorRepository.php b/src/Projection/Projector/ProjectorRepository.php deleted file mode 100644 index f6c4c4e45..000000000 --- a/src/Projection/Projector/ProjectorRepository.php +++ /dev/null @@ -1,11 +0,0 @@ - */ - public function projectors(): array; -} diff --git a/tests/Benchmark/SyncProjectionistBench.php b/tests/Benchmark/SyncProjectionistBench.php index 7e60da4ad..ea0986e6d 100644 --- a/tests/Benchmark/SyncProjectionistBench.php +++ b/tests/Benchmark/SyncProjectionistBench.php @@ -13,7 +13,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBus; -use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Repository\Repository; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; @@ -62,14 +61,11 @@ public function setUp(): void ); $profileProjection = new ProfileProjector($connection); - $projectionRepository = new InMemoryProjectorRepository( - [$profileProjection], - ); $projectionist = new DefaultProjectionist( $this->store, new InMemoryStore(), - $projectionRepository, + [$profileProjection], ); $lockStorage = new LockDoctrineDbalStore($connection); diff --git a/tests/Integration/BankAccountSplitStream/IntegrationTest.php b/tests/Integration/BankAccountSplitStream/IntegrationTest.php index 6e1034b35..af8a9a8cc 100644 --- a/tests/Integration/BankAccountSplitStream/IntegrationTest.php +++ b/tests/Integration/BankAccountSplitStream/IntegrationTest.php @@ -15,7 +15,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBus; -use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer; @@ -56,12 +55,11 @@ public function testSuccessful(): void ); $bankAccountProjector = new BankAccountProjector($this->connection); - $projectionRepository = new InMemoryProjectorRepository([$bankAccountProjector]); $projectionist = new DefaultProjectionist( $store, new InMemoryStore(), - $projectionRepository, + [$bankAccountProjector], ); $eventBus = new ChainEventBus([ diff --git a/tests/Integration/BasicImplementation/BasicIntegrationTest.php b/tests/Integration/BasicImplementation/BasicIntegrationTest.php index b05ef8d03..1175dbb99 100644 --- a/tests/Integration/BasicImplementation/BasicIntegrationTest.php +++ b/tests/Integration/BasicImplementation/BasicIntegrationTest.php @@ -12,7 +12,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBus; -use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; use Patchlevel\EventSourcing\Serializer\DefaultEventSerializer; @@ -53,14 +52,11 @@ public function testSuccessful(): void ); $profileProjector = new ProfileProjector($this->connection); - $projectorRepository = new InMemoryProjectorRepository( - [$profileProjector], - ); $projectionist = new DefaultProjectionist( $store, new InMemoryStore(), - $projectorRepository, + [$profileProjector], ); $eventBus = new ChainEventBus([ @@ -127,14 +123,11 @@ public function testSnapshot(): void ); $profileProjection = new ProfileProjector($this->connection); - $projectorRepository = new InMemoryProjectorRepository( - [$profileProjection], - ); $projectionist = new DefaultProjectionist( $store, new InMemoryStore(), - $projectorRepository, + [$profileProjection], ); $eventBus = new ChainEventBus([ diff --git a/tests/Integration/Outbox/OutboxTest.php b/tests/Integration/Outbox/OutboxTest.php index 97f412c66..2c85472d5 100644 --- a/tests/Integration/Outbox/OutboxTest.php +++ b/tests/Integration/Outbox/OutboxTest.php @@ -16,7 +16,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBus; -use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepository; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -66,14 +65,11 @@ public function testSuccessful(): void $outboxEventBus = new OutboxEventBus($outboxStore); $profileProjector = new ProfileProjector($this->connection); - $projectorRepository = new InMemoryProjectorRepository( - [$profileProjector], - ); $projectionist = new DefaultProjectionist( $store, new InMemoryStore(), - $projectorRepository, + [$profileProjector], ); $eventBusConsumer = DefaultConsumer::create([new SendEmailProcessor()]); diff --git a/tests/Integration/Projectionist/ProjectionistTest.php b/tests/Integration/Projectionist/ProjectionistTest.php index 6dee3e073..002b7eb3f 100644 --- a/tests/Integration/Projectionist/ProjectionistTest.php +++ b/tests/Integration/Projectionist/ProjectionistTest.php @@ -14,7 +14,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Store\DoctrineStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; use Patchlevel\EventSourcing\Projection\Projectionist\ProjectionistEventBus; -use Patchlevel\EventSourcing\Projection\Projector\InMemoryProjectorRepository; use Patchlevel\EventSourcing\Repository\DefaultRepositoryManager; use Patchlevel\EventSourcing\Schema\ChainSchemaConfigurator; use Patchlevel\EventSourcing\Schema\DoctrineSchemaDirector; @@ -76,9 +75,7 @@ public function testAsync(): void $projectionist = new DefaultProjectionist( $store, $projectionStore, - new InMemoryProjectorRepository( - [new ProfileProjector($this->connection)], - ), + [new ProfileProjector($this->connection)], ); $projectionist->boot(); @@ -110,9 +107,7 @@ public function testSync(): void $projectionist = new DefaultProjectionist( $store, $projectionStore, - new InMemoryProjectorRepository( - [new ProfileProjector($this->connection)], - ), + [new ProfileProjector($this->connection)], ); $manager = new DefaultRepositoryManager( diff --git a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php index 0b0ce9c14..62753a933 100644 --- a/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php +++ b/tests/Unit/Projection/Projectionist/DefaultProjectionistTest.php @@ -9,7 +9,6 @@ use Patchlevel\EventSourcing\Attribute\Subscribe; use Patchlevel\EventSourcing\Attribute\Teardown; use Patchlevel\EventSourcing\EventBus\Message; -use Patchlevel\EventSourcing\Metadata\Projector\ProjectorMetadataFactory; use Patchlevel\EventSourcing\Projection\Projection\Projection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCollection; use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria; @@ -18,7 +17,6 @@ use Patchlevel\EventSourcing\Projection\Projection\Store\ErrorContext; use Patchlevel\EventSourcing\Projection\Projection\Store\ProjectionStore; use Patchlevel\EventSourcing\Projection\Projectionist\DefaultProjectionist; -use Patchlevel\EventSourcing\Projection\Projector\ProjectorRepository; use Patchlevel\EventSourcing\Store\ArrayStream; use Patchlevel\EventSourcing\Store\Criteria; use Patchlevel\EventSourcing\Store\Store; @@ -44,16 +42,10 @@ public function testNothingToBoot(): void $projectionStore = $this->prophesize(ProjectionStore::class); $projectionStore->all()->willReturn($projectionCollection)->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([])->shouldBeCalledOnce(); - - $metadataFactory = $this->prophesize(ProjectorMetadataFactory::class); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore->reveal(), - $projectorRepository->reveal(), - $metadataFactory->reveal(), + [], ); $projectionist->boot(); @@ -75,13 +67,10 @@ class { $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->boot(); @@ -121,13 +110,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->boot(); @@ -170,13 +156,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->boot(new ProjectionCriteria(), 1); @@ -226,13 +209,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector1, $projector2])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector1, $projector2], ); $projectionist->boot(); @@ -271,13 +251,10 @@ public function create(): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->shouldNotBeCalled(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->boot(); @@ -323,13 +300,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([1 => $message1, 3 => $message2]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->boot(); @@ -364,13 +338,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->run(); @@ -407,13 +378,10 @@ public function handle(Message $message): void ->willReturn(new ArrayStream([$message1, $message2])) ->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->run(new ProjectionCriteria(), 1); @@ -461,13 +429,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector1, $projector2])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector1, $projector2], ); $projectionist->run(); @@ -504,13 +469,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([$message]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->run(); @@ -538,13 +500,10 @@ public function testRunningMarkOutdated(): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->shouldNotBeCalled(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [], ); $projectionist->run(); @@ -563,13 +522,10 @@ public function testRunningWithoutActiveProjectors(): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->shouldNotBeCalled(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [], ); $projectionist->run(); @@ -600,13 +556,10 @@ public function handle(Message $message): void $streamableStore = $this->prophesize(Store::class); $streamableStore->load($this->criteria())->willReturn(new ArrayStream([1 => $message1, 3 => $message2]))->shouldBeCalledOnce(); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->run(); @@ -638,13 +591,10 @@ public function drop(): void $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->teardown(); @@ -673,13 +623,10 @@ public function drop(): void $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->teardown(); @@ -696,13 +643,10 @@ public function testTeardownWithoutProjector(): void $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [], ); $projectionist->teardown(); @@ -729,13 +673,10 @@ public function drop(): void $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->remove(); @@ -756,13 +697,10 @@ class { $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->remove(); @@ -789,13 +727,10 @@ public function drop(): void $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->remove(); @@ -812,13 +747,10 @@ public function testRemoveWithoutProjector(): void $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [], ); $projectionist->remove(); @@ -838,13 +770,10 @@ class { $streamableStore = $this->prophesize(Store::class); - $projectorRepository = $this->prophesize(ProjectorRepository::class); - $projectorRepository->projectors()->willReturn([$projector])->shouldBeCalledOnce(); - $projectionist = new DefaultProjectionist( $streamableStore->reveal(), $projectionStore, - $projectorRepository->reveal(), + [$projector], ); $projectionist->reactivate(); diff --git a/tests/Unit/Projection/Projector/InMemoryProjectorRepositoryTest.php b/tests/Unit/Projection/Projector/InMemoryProjectorRepositoryTest.php deleted file mode 100644 index 746408b9b..000000000 --- a/tests/Unit/Projection/Projector/InMemoryProjectorRepositoryTest.php +++ /dev/null @@ -1,27 +0,0 @@ -projectors()); - } - - public function testGetAllProjectors(): void - { - $projector = new class { - }; - $repository = new InMemoryProjectorRepository([$projector]); - - self::assertEquals([$projector], $repository->projectors()); - } -}