Skip to content

Commit

Permalink
change listener provider api
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Jan 29, 2024
1 parent 7899776 commit 82cc5c4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 57 deletions.
16 changes: 10 additions & 6 deletions src/EventBus/AttributeListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ public function __construct(
) {
}

/** @return iterable<ListenerDescriptor> */
public function listenersForEvent(object $event): iterable
/**
* @param class-string $eventClass
*
* @return iterable<ListenerDescriptor>
*/
public function listenersForEvent(string $eventClass): iterable
{
if ($this->subscribeMethods !== null) {
return array_merge(
$this->subscribeMethods[$event::class] ?? [],
$this->subscribeMethods[$eventClass] ?? [],
$this->subscribeMethods[Subscribe::ALL] ?? [],
);
}
Expand All @@ -41,17 +45,17 @@ 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()}(...),
);
}
}
}

return array_merge(
$this->subscribeMethods[$event::class] ?? [],
$this->subscribeMethods[$eventClass] ?? [],
$this->subscribeMethods[Subscribe::ALL] ?? [],
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/EventBus/DefaultEventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
));

Expand Down
8 changes: 6 additions & 2 deletions src/EventBus/ListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

interface ListenerProvider
{
/** @return iterable<ListenerDescriptor> */
public function listenersForEvent(object $event): iterable;
/**
* @param class-string $eventClass
*
* @return iterable<ListenerDescriptor>
*/
public function listenersForEvent(string $eventClass): iterable;
}
44 changes: 6 additions & 38 deletions tests/Unit/EventBus/AttributeListenerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,13 +17,8 @@ final class AttributeListenerProviderTest extends TestCase
{
public function testProvideNothing(): void
{
$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$eventBus = new AttributeListenerProvider([]);
$listeners = $eventBus->listenersForEvent($event);
$listeners = $eventBus->listenersForEvent(ProfileCreated::class);

self::assertSame([], $listeners);
}
Expand All @@ -44,13 +37,8 @@ public function bar(Message $message): void
}
};

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$eventBus = new AttributeListenerProvider([$listener]);
$listeners = $eventBus->listenersForEvent($event);
$listeners = $eventBus->listenersForEvent(ProfileCreated::class);

self::assertEquals([new ListenerDescriptor($listener->foo(...))], $listeners);
}
Expand All @@ -69,13 +57,8 @@ public function bar(Message $message): void
}
};

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$eventBus = new AttributeListenerProvider([$listener]);
$listeners = $eventBus->listenersForEvent($event);
$listeners = $eventBus->listenersForEvent(ProfileCreated::class);

self::assertEquals([
new ListenerDescriptor($listener->foo(...)),
Expand All @@ -99,13 +82,8 @@ public function __invoke(Message $message): void
}
};

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$eventBus = new AttributeListenerProvider([$listener1, $listener2]);
$listeners = $eventBus->listenersForEvent($event);
$listeners = $eventBus->listenersForEvent(ProfileCreated::class);

self::assertEquals([
new ListenerDescriptor($listener1->__invoke(...)),
Expand All @@ -122,13 +100,8 @@ public function __invoke(Message $message): void
}
};

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$eventBus = new AttributeListenerProvider([$listener]);
$listeners = $eventBus->listenersForEvent($event);
$listeners = $eventBus->listenersForEvent(ProfileCreated::class);

self::assertEquals([
new ListenerDescriptor($listener->__invoke(...)),
Expand All @@ -149,13 +122,8 @@ public function bar(Message $message): void
}
};

$event = new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
);

$eventBus = new AttributeListenerProvider([$listener]);
$listeners = $eventBus->listenersForEvent($event);
$listeners = $eventBus->listenersForEvent(ProfileCreated::class);

self::assertEquals([
new ListenerDescriptor($listener->bar(...)),
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/EventBus/DefaultEventBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 82cc5c4

Please sign in to comment.