Skip to content

Release 2.1.1

Compare
Choose a tag to compare
@peter-gribanov peter-gribanov released this 03 Oct 09:21
· 68 commits to master since this release

Changelog (since 2.1.0...2.1.1)

  • Added ability to subscribe to events.

    use GpsLab\Domain\Event\Event;
    use GpsLab\Domain\Event\Listener\Subscriber;
    
    class SendEmailOnPurchaseOrderCreated implements Subscriber
    {
        private $mailer;
    
        public function __construct(\Swift_Mailer $mailer)
        {
            $this->mailer = $mailer;
        }
    
        public static function subscribedEvents()
        {
            return [
                PurchaseOrderCreatedEvent::class => ['onPurchaseOrderCreated'],
            ];
        }
    
        public function onPurchaseOrderCreated(PurchaseOrderCreatedEvent $event)
        {
            $message = $this->mailer
                ->createMessage()
                ->setTo('recipient@example.com')
                ->setBody(sprintf(
                    'Purchase order created at %s for customer #%s',
                    $event->getCreateAt()->format('Y-m-d'),
                    $event->getCustomer()->getId()
                ));
    
            $this->mailer->send($message);
        }
    }

    Register event subscriber

    services:
        acme.domain.purchase_order.event.created.send_email_subscriber:
            class: SendEmailOnPurchaseOrderCreated
            arguments: [ '@mailer' ]
            tags:
                - { name: domain_event.subscriber }