Browse project documentation

Events

Ersalv0.2.0-beta.2View sourceEnglish / Persian

Ersal dispatches lifecycle events through the optional EventDispatcher. Pass a dispatcher to the Ersal constructor to receive them:

$ersal = new Ersal(
    eventDispatcher: new MyDispatcher(),
);

Event catalog

All events are simple data classes with public readonly fields.

EventFieldsDispatched when
ShipmentQuotedproviderName, request, quotesquote() returns successfully
ShipmentCreatedproviderName, shipmentcreateShipment() succeeds
ShipmentTrackedproviderName, shipmenttrack() returns successfully
ShipmentCancelledproviderName, shipmentcancel() succeeds
ShipmentFailedproviderName, operation, reason, errorCodeAny operation fails

operation on ShipmentFailed is one of: 'quote', 'book', 'track', 'cancel', 'label', 'pickup'.

Minimal dispatcher

use Eram\Ersal\Http\EventDispatcher;
use Eram\Ersal\Event\ShipmentCreated;
use Eram\Ersal\Event\ShipmentFailed;

final class AppDispatcher implements EventDispatcher
{
    public function dispatch(object $event): object
    {
        match (true) {
            $event instanceof ShipmentCreated => $this->onCreated($event),
            $event instanceof ShipmentFailed  => $this->onFailed($event),
            default => null,
        };

        return $event;
    }

    private function onCreated(ShipmentCreated $e): void
    {
        $this->logger->info('Shipment booked', [
            'provider' => $e->providerName,
            'shipment_id' => $e->shipment->getId()->value(),
            'tracking_code' => $e->shipment->getTrackingCode(),
        ]);
    }

    private function onFailed(ShipmentFailed $e): void
    {
        $this->logger->error('Shipment operation failed', [
            'provider' => $e->providerName,
            'operation' => $e->operation,
            'reason' => $e->reason,
            'code' => $e->errorCode,
        ]);
    }
}

Using a PSR-14 dispatcher

EventDispatcher::dispatch() deliberately mirrors PSR-14’s signature (object → object). To use an existing PSR-14 dispatcher, write a thin adapter:

final class Psr14Adapter implements EventDispatcher
{
    public function __construct(private \Psr\EventDispatcher\EventDispatcherInterface $inner) {}

    public function dispatch(object $event): object
    {
        return $this->inner->dispatch($event);
    }
}

Ersal stays PSR-14-neutral — no direct psr/event-dispatcher dependency, but the contract is a drop-in match.

Search documentation

Search across all projects. Close this window to return to your guide.

Tab to navigate · Enter to openEsc to close