Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Clivern committed Aug 21, 2021
1 parent 13bc979 commit cc5a70f
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down
40 changes: 40 additions & 0 deletions src/Event/AuditAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Clivern/Walnut project.
* (c) Clivern <hello@clivern.com>
*/

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;
}
}
57 changes: 57 additions & 0 deletions src/EventSubscriber/AuditActionSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Clivern/Walnut project.
* (c) Clivern <hello@clivern.com>
*/

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',
];
}
}
3 changes: 2 additions & 1 deletion src/EventSubscriber/EventCreatedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
));
Expand Down
11 changes: 9 additions & 2 deletions src/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit cc5a70f

Please sign in to comment.