Example #1
0
    }
}
class Booking implements AggregateRoot
{
    use TracksChanges;
    private $id;
    public function __construct($id)
    {
        $this->id = $id;
    }
    public function confirm()
    {
        $this->recordChange(new BookingConfirmed($this->id));
    }
}
class SymfonySendConfirmationListener
{
    public function onBookingConfirmed(BookingConfirmed $event)
    {
        echo sprintf('[Symfony] booking#%s has been confirmed. We need to send a confirmation email.' . PHP_EOL, $event->getBookingId());
    }
}
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('booking.confirmed', [new SymfonySendConfirmationListener(), 'onBookingConfirmed']);
$redis = new Redis();
$redis->connect('127.0.0.1');
$changeTracker = new ChangeTracker(new LoggerEventBus(new EchoLogger(), new CompositeEventBus([new SymfonyEventBus($eventDispatcher), new RedisEventBus($redis, 'booking_engine')])));
$booking = new Booking('1234');
$booking->confirm();
$changeTracker->track($booking);
echo 'Run php examples/redis-worker.php to catch event in an asynchronous way' . PHP_EOL;
 public function track(AggregateRoot $aggregate)
 {
     $this->changeTracker->track($aggregate);
 }