From 82cc5c4a97785b2269cfa0c5baf595f6c58efaa4 Mon Sep 17 00:00:00 2001 From: David Badura Date: Mon, 29 Jan 2024 17:48:33 +0100 Subject: [PATCH] change listener provider api --- src/EventBus/AttributeListenerProvider.php | 16 ++++--- src/EventBus/DefaultEventBus.php | 8 ++-- src/EventBus/ListenerProvider.php | 8 +++- .../AttributeListenerProviderTest.php | 44 +++---------------- tests/Unit/EventBus/DefaultEventBusTest.php | 16 +++---- 5 files changed, 35 insertions(+), 57 deletions(-) diff --git a/src/EventBus/AttributeListenerProvider.php b/src/EventBus/AttributeListenerProvider.php index 0e9a2c526..cfa865199 100644 --- a/src/EventBus/AttributeListenerProvider.php +++ b/src/EventBus/AttributeListenerProvider.php @@ -20,12 +20,16 @@ public function __construct( ) { } - /** @return iterable */ - public function listenersForEvent(object $event): iterable + /** + * @param class-string $eventClass + * + * @return iterable + */ + public function listenersForEvent(string $eventClass): iterable { if ($this->subscribeMethods !== null) { return array_merge( - $this->subscribeMethods[$event::class] ?? [], + $this->subscribeMethods[$eventClass] ?? [], $this->subscribeMethods[Subscribe::ALL] ?? [], ); } @@ -41,9 +45,9 @@ public function listenersForEvent(object $event): iterable foreach ($attributes as $attribute) { $instance = $attribute->newInstance(); - $eventClass = $instance->eventClass; + $subscribedEventClass = $instance->eventClass; - $this->subscribeMethods[$eventClass][] = new ListenerDescriptor( + $this->subscribeMethods[$subscribedEventClass][] = new ListenerDescriptor( $listener->{$method->getName()}(...), ); } @@ -51,7 +55,7 @@ public function listenersForEvent(object $event): iterable } return array_merge( - $this->subscribeMethods[$event::class] ?? [], + $this->subscribeMethods[$eventClass] ?? [], $this->subscribeMethods[Subscribe::ALL] ?? [], ); } diff --git a/src/EventBus/DefaultEventBus.php b/src/EventBus/DefaultEventBus.php index 4ece0ab2a..9c0d39f48 100644 --- a/src/EventBus/DefaultEventBus.php +++ b/src/EventBus/DefaultEventBus.php @@ -46,17 +46,19 @@ public function dispatch(Message ...$messages): void $this->logger?->debug('EventBus: Start processing queue.'); while ($message = array_shift($this->queue)) { + $eventClass = $message->event()::class; + $this->logger?->debug(sprintf( 'EventBus: Dispatch message "%s" to listeners.', - $message->event()::class, + $eventClass, )); - $listeners = $this->listenerProvider->listenersForEvent($message->event()); + $listeners = $this->listenerProvider->listenersForEvent($eventClass); foreach ($listeners as $listener) { $this->logger?->info(sprintf( 'EventBus: Dispatch message with event "%s" to listener "%s".', - $message->event()::class, + $eventClass, $listener->name(), )); diff --git a/src/EventBus/ListenerProvider.php b/src/EventBus/ListenerProvider.php index 2bf4155ab..0918a634e 100644 --- a/src/EventBus/ListenerProvider.php +++ b/src/EventBus/ListenerProvider.php @@ -6,6 +6,10 @@ interface ListenerProvider { - /** @return iterable */ - public function listenersForEvent(object $event): iterable; + /** + * @param class-string $eventClass + * + * @return iterable + */ + public function listenersForEvent(string $eventClass): iterable; } diff --git a/tests/Unit/EventBus/AttributeListenerProviderTest.php b/tests/Unit/EventBus/AttributeListenerProviderTest.php index 6ae4bfe1d..08844fd9d 100644 --- a/tests/Unit/EventBus/AttributeListenerProviderTest.php +++ b/tests/Unit/EventBus/AttributeListenerProviderTest.php @@ -8,9 +8,7 @@ use Patchlevel\EventSourcing\EventBus\AttributeListenerProvider; use Patchlevel\EventSourcing\EventBus\ListenerDescriptor; use Patchlevel\EventSourcing\EventBus\Message; -use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; -use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId; use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileVisited; use PHPUnit\Framework\TestCase; @@ -19,13 +17,8 @@ final class AttributeListenerProviderTest extends TestCase { public function testProvideNothing(): void { - $event = new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('info@patchlevel.de'), - ); - $eventBus = new AttributeListenerProvider([]); - $listeners = $eventBus->listenersForEvent($event); + $listeners = $eventBus->listenersForEvent(ProfileCreated::class); self::assertSame([], $listeners); } @@ -44,13 +37,8 @@ public function bar(Message $message): void } }; - $event = new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('info@patchlevel.de'), - ); - $eventBus = new AttributeListenerProvider([$listener]); - $listeners = $eventBus->listenersForEvent($event); + $listeners = $eventBus->listenersForEvent(ProfileCreated::class); self::assertEquals([new ListenerDescriptor($listener->foo(...))], $listeners); } @@ -69,13 +57,8 @@ public function bar(Message $message): void } }; - $event = new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('info@patchlevel.de'), - ); - $eventBus = new AttributeListenerProvider([$listener]); - $listeners = $eventBus->listenersForEvent($event); + $listeners = $eventBus->listenersForEvent(ProfileCreated::class); self::assertEquals([ new ListenerDescriptor($listener->foo(...)), @@ -99,13 +82,8 @@ public function __invoke(Message $message): void } }; - $event = new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('info@patchlevel.de'), - ); - $eventBus = new AttributeListenerProvider([$listener1, $listener2]); - $listeners = $eventBus->listenersForEvent($event); + $listeners = $eventBus->listenersForEvent(ProfileCreated::class); self::assertEquals([ new ListenerDescriptor($listener1->__invoke(...)), @@ -122,13 +100,8 @@ public function __invoke(Message $message): void } }; - $event = new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('info@patchlevel.de'), - ); - $eventBus = new AttributeListenerProvider([$listener]); - $listeners = $eventBus->listenersForEvent($event); + $listeners = $eventBus->listenersForEvent(ProfileCreated::class); self::assertEquals([ new ListenerDescriptor($listener->__invoke(...)), @@ -149,13 +122,8 @@ public function bar(Message $message): void } }; - $event = new ProfileCreated( - ProfileId::fromString('1'), - Email::fromString('info@patchlevel.de'), - ); - $eventBus = new AttributeListenerProvider([$listener]); - $listeners = $eventBus->listenersForEvent($event); + $listeners = $eventBus->listenersForEvent(ProfileCreated::class); self::assertEquals([ new ListenerDescriptor($listener->bar(...)), diff --git a/tests/Unit/EventBus/DefaultEventBusTest.php b/tests/Unit/EventBus/DefaultEventBusTest.php index 51dcf5084..b255ccdbb 100644 --- a/tests/Unit/EventBus/DefaultEventBusTest.php +++ b/tests/Unit/EventBus/DefaultEventBusTest.php @@ -42,7 +42,7 @@ public function __invoke(Message $message): void ); $provider = $this->prophesize(ListenerProvider::class); - $provider->listenersForEvent($message->event())->willReturn([new ListenerDescriptor($listener->__invoke(...))]); + $provider->listenersForEvent(ProfileCreated::class)->willReturn([new ListenerDescriptor($listener->__invoke(...))]); $eventBus = new DefaultEventBus($provider->reveal()); $eventBus->dispatch($message); @@ -77,8 +77,8 @@ public function __invoke(Message $message): void ); $provider = $this->prophesize(ListenerProvider::class); - $provider->listenersForEvent($message1->event())->willReturn([new ListenerDescriptor($listener->__invoke(...))]); - $provider->listenersForEvent($message2->event())->willReturn([new ListenerDescriptor($listener->__invoke(...))]); + $provider->listenersForEvent(ProfileCreated::class)->willReturn([new ListenerDescriptor($listener->__invoke(...))]); + $provider->listenersForEvent(ProfileCreated::class)->willReturn([new ListenerDescriptor($listener->__invoke(...))]); $eventBus = new DefaultEventBus($provider->reveal()); $eventBus->dispatch($message1, $message2); @@ -140,8 +140,8 @@ public function __invoke(Message $message): void } }; - $provider->listenersForEvent($messageA->event())->willReturn([new ListenerDescriptor($listenerA->__invoke(...))]); - $provider->listenersForEvent($messageB->event())->willReturn([new ListenerDescriptor($listenerB->__invoke(...))]); + $provider->listenersForEvent(ProfileCreated::class)->willReturn([new ListenerDescriptor($listenerA->__invoke(...))]); + $provider->listenersForEvent(ProfileVisited::class)->willReturn([new ListenerDescriptor($listenerB->__invoke(...))]); $eventBus->dispatch($messageA); @@ -209,9 +209,9 @@ public function __invoke(Message $message): void } }; - $provider->listenersForEvent($messageA->event())->willReturn([new ListenerDescriptor($listenerA->__invoke(...))]); - $provider->listenersForEvent($messageB->event())->willReturn([]); - $provider->listenersForEvent($messageC->event())->willReturn([new ListenerDescriptor($listenerB->__invoke(...))]); + $provider->listenersForEvent(ProfileCreated::class)->willReturn([new ListenerDescriptor($listenerA->__invoke(...))]); + $provider->listenersForEvent(ProfileVisited::class)->willReturn([]); + $provider->listenersForEvent(NameChanged::class)->willReturn([new ListenerDescriptor($listenerB->__invoke(...))]); $eventBus->dispatch($messageA, $messageB);