diff --git a/src/Controller/HealthController.php b/src/Controller/HealthController.php index 772bf86..5e90eb7 100644 --- a/src/Controller/HealthController.php +++ b/src/Controller/HealthController.php @@ -10,6 +10,7 @@ namespace App\Controller; use App\Entity\Event; +use App\Event\AuditAction; use App\Repository\EventRepository; use Clivern\Chunk\Core\Message; use Clivern\Chunk\Core\Sender; @@ -81,7 +82,14 @@ public function index(): Response 'type' => 'healthCheck', 'payload' => ['key' => 'value'], ]); + $this->eventRepository->storeOne($event); + + $event = Event::fromArray([ + 'type' => 'newRequest', + 'payload' => ['key' => 'value'], + ]); + $this->eventRepository->dispatchEvent(new AuditAction($event), AuditAction::NAME); $this->eventRepository->storeOne($event); return $this->json([ diff --git a/src/Event/AuditAction.php b/src/Event/AuditAction.php new file mode 100644 index 0000000..76eea96 --- /dev/null +++ b/src/Event/AuditAction.php @@ -0,0 +1,40 @@ + + */ + +namespace App\Event; + +use App\Entity\Event; +use Symfony\Contracts\EventDispatcher\Event as SymfonyEvent; + +/** + * AuditAction Class. + */ +class AuditAction extends SymfonyEvent +{ + public const NAME = 'audit.action'; + + /** @var Event */ + protected $event; + + /** + * Class Constructor. + */ + public function __construct(Event $event) + { + $this->event = $event; + } + + /** + * Get Event Instance. + */ + public function getEvent(): Event + { + return $this->event; + } +} diff --git a/src/EventSubscriber/AuditActionSubscriber.php b/src/EventSubscriber/AuditActionSubscriber.php new file mode 100644 index 0000000..a035e4a --- /dev/null +++ b/src/EventSubscriber/AuditActionSubscriber.php @@ -0,0 +1,57 @@ + + */ + +namespace App\EventSubscriber; + +use App\Event\AuditAction; +use Psr\Log\LoggerInterface; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +/** + * AuditActionSubscriber Class. + */ +class AuditActionSubscriber implements EventSubscriberInterface +{ + /** @var LoggerInterface */ + private $logger; + + /** + * Class Constructor. + */ + public function __construct( + LoggerInterface $logger + ) { + $this->logger = $logger; + } + + /** + * Event Created Handler. + */ + public function onAuditAction(AuditAction $action) + { + $this->logger->info(sprintf( + 'Event with type %s created with ID %d, UUID %s', + $action->getEvent()->getType(), + $action->getEvent()->getId(), + $action->getEvent()->getUUID() + )); + } + + /** + * Get Subscribed Events. + * + * @return array + */ + public static function getSubscribedEvents() + { + return [ + AuditAction::NAME => 'onAuditAction', + ]; + } +} diff --git a/src/EventSubscriber/EventCreatedSubscriber.php b/src/EventSubscriber/EventCreatedSubscriber.php index d8a919a..669168e 100644 --- a/src/EventSubscriber/EventCreatedSubscriber.php +++ b/src/EventSubscriber/EventCreatedSubscriber.php @@ -36,7 +36,8 @@ public function __construct( public function onEventCreated(EventCreated $event) { $this->logger->info(sprintf( - 'Event created with ID %d, UUID %s', + 'Event with type %s created with ID %d, UUID %s', + $event->getEvent()->getType(), $event->getEvent()->getId(), $event->getEvent()->getUUID() )); diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php index 0d3d5e2..bbd7ffc 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -42,9 +42,16 @@ public function storeOne(Event $event): ?Event $this->getEntityManager()->persist($event); $this->getEntityManager()->flush(); - $eventCreated = new EventCreated($event); - $this->eventDispatcher->dispatch($eventCreated, EventCreated::NAME); + $this->dispatchEvent(new EventCreated($event), EventCreated::NAME); return $event; } + + /** + * Dispatch Event. + */ + public function dispatchEvent(object $event, string $name) + { + $this->eventDispatcher->dispatch($event, $name); + } }