PHP code example of headsnet / domain-events-bundle
1. Go to this page and download the library: Download headsnet/domain-events-bundle 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/ */
headsnet / domain-events-bundle example snippets
use Headsnet\DomainEventsBundle\Domain\Model\DomainEvent;
use Headsnet\DomainEventsBundle\Domain\Model\Traits\DomainEventTrait;
final class DiscountWasApplied implements DomainEvent
{
use DomainEventTrait;
public function __construct(string $aggregateRootId)
{
$this->aggregateRootId = $aggregateRootId;
$this->occurredOn = (new \DateTimeImmutable)->format(DateTime::ATOM);
}
}
use Headsnet\DomainEventsBundle\Domain\Model\ContainsEvents;
use Headsnet\DomainEventsBundle\Domain\Model\RecordsEvents;
use Headsnet\DomainEventsBundle\Domain\Model\Traits\EventRecorderTrait;
class MyEntity implements ContainsEvents, RecordsEvents
{
use EventRecorderTrait;
public function __construct(PropertyId $uuid)
{
$this->uuid = $uuid;
// Record a domain event
$this->record(
new DiscountWasApplied($uuid->asString())
);
}
}
use App\Entity\User;
use Headsnet\DomainEventsBundle\Doctrine\Event\PreAppendEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Security;
final class AssignDomainEventUser implements EventSubscriberInterface
{
private Security $security;
public function __construct(Security $security)
{
$this->security = $security;
}
public static function getSubscribedEvents(): array
{
return [
PreAppendEvent::class => 'onPreAppend'
];
}
public function onPreAppend(PreAppendEvent $event): void
{
$domainEvent = $event->getDomainEvent();
if (null === $domainEvent->getActorId()) {
$user = $this->security->getUser();
if ($user instanceof User) {
$domainEvent->setActorId($user->getId());
}
}
}
}
class PersonCreated implements DomainEvent, AuditableEvent
{
…
}
class DomainEventDispatcher implements \Headsnet\DomainEventsBundle\EventSubscriber\DomainEventDispatcher
{
private MessageBusInterface $eventBus;
public function __construct(MessageBusInterface $eventBus)
{
$this->eventBus = $eventBus;
}
public function dispatch(DomainEvent $event): void
{
if ($event instanceof AuditableEvent) {
$this->eventBus->dispatch(
new Envelope($event, [new AuditStamp()])
);
} else {
$this->eventBus->dispatch($event);
}
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.