1. Go to this page and download the library: Download lingoda/domain-events library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lingoda / domain-events example snippets
use Lingoda\DomainEventsBundle\Domain\Model\DomainEvent;
use Lingoda\DomainEventsBundle\Domain\Model\Traits\DomainEventTrait;
/**
* Sample domain event
*/
class UserCreatedEvent implements DomainEvent
{
use DomainEventTrait;
private string $username;
public function __construct(string $entityId, string $username)
{
$this->username = $username;
$this->init($entityId);
}
public function getUsername(): string
{
return $this->username;
}
}
use Lingoda\DomainEventsBundle\Domain\Model\DomainEventAware;
use Lingoda\DomainEventsBundle\Domain\Model\Traits\EventRecorderTrait;
use Symfony\Component\Uid\Ulid;
// DomainEventAware interface is a helper that brings RecordsEvents and ContainsEvents together
class User implements DomainEventAware
{
// helper trait for event recording
use EventRecorderTrait;
private Ulid $id;
private string $username;
public function __construct(string $username)
{
$this->id = new Ulid();
$this->username = $username;
$this->recordEvent(new UserCreatedEvent(
$this->id->toRfc4122(),
$username
));
}
public function getId(): Ulid
{
return $this->id;
}
public function setId(Ulid $id): void
{
$this->id = $id;
}
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): void
{
$this->username = $username;
}
}
// create the entity will record the domain event
$user = new User('john-doe');
$entityManager->persist($user);
/**
* When we flush the changes PersistDomainEventsSubscriber will kick in and create a OutboxRecord entity containing
* the domain event in it that will be stored within the same transaction together with the User entity
*/
$entityManager->flush();
// Later on the PublishDomainEventsSubscriber will publish via Messenger all unpublished Domain Event from OutboxRecord
// database on the following events KernelEvents::TERMINATE, ConsoleEvents::TERMINATE or WorkerMessageHandledEvent
// let's say we have AskForUserFeedbackEvent the following event that should be triggered 2 weeks after user registration
// and send a followup email to the user
// we could schedule this like follow
$askForUserFeedbackEvent = new AskForUserFeedbackEvent($user->getId());
$askForUserFeedbackEvent->setOccuredAt(
new CarbonImmutable('+2 weeks')
);
$user->recordEvent($askForUserFeedbackEvent);
// this will be stored in OutboxRecord table and unpublished until the due date
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.