Skip to content

CatchAllEventListenerInterface #259

@bwaidelich

Description

@bwaidelich

Currently it's cumbersome to create a listener/projector that is invoked for all event types:

<?php
final class SomeLogProjector implements ProjectorInterface
{
    public function whenSomeEventType1(SomeEventType1 $event, RawEvent $rawEvent): void
    {
        $this->addToEventLog($rawEvent)
    }

    public function whenSomeEventType2(SomeEventType2 $event, RawEvent $rawEvent): void
    {
        $this->addToEventLog($rawEvent)
    }

    // ...
}

With the BeforeInvokeInterface things can be centralized, but the corresponding when*() methods still have to be implemented in order for the listener to be invoked:

<?php
final class SomeLogProjector implements ProjectorInterface, BeforeInvokeInterface
{

    public function beforeInvoke(EventEnvelope $eventEnvelope): void
    {
        $this->addToEventLog($eventEnvelope->getRawEvent())
    }

    public function whenSomeEventType1(SomeEventType1 $_): void
    {
        // this method is required
    }

    public function whenSomeEventType2(SomeEventType2 $_): void
    {
        // this method is required
    }

    // ...
}

To make these cases easier we should provide an additional (marker) interface that informs the framework that this listener should be invoked for all events:

<?php
final class SomeLogProjector implements ProjectorInterface, CatchAllEventListener, BeforeInvokeInterface
{

    public function beforeInvoke(EventEnvelope $eventEnvelope): void
    {
        $this->addToEventLog($eventEnvelope->getRawEvent())
    }

    public function whenSomeEventType2(SomeEventType2 $event): void
    {
        // this method CAN still be implemented and will be invoked for the corresponding event type
    }

    // ...
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions