src/EventSubscriber/LogSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Logs\EventLog;
  4. use App\Event\LogEvent;
  5. use App\Repository\Logs\EventLogRepository;
  6. use App\Service\NotificationService;
  7. use Pusher\PusherException;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Serializer\SerializerInterface;
  10. class LogSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var SerializerInterface
  14.      */
  15.     private $serializer;
  16.     /**
  17.      * @var EventLogRepository
  18.      */
  19.     private $eventLogRepository;
  20.     /**
  21.      * @var NotificationService
  22.      */
  23.     private $notificationService;
  24.     public function __construct(
  25.         SerializerInterface $serializer,
  26.         EventLogRepository $eventLogRepository,
  27.         NotificationService $notificationService
  28.     )
  29.     {
  30.         $this->serializer $serializer;
  31.         $this->eventLogRepository $eventLogRepository;
  32.         $this->notificationService $notificationService;
  33.     }
  34.     /**
  35.      * @inheritDoc
  36.      */
  37.     public static function getSubscribedEvents(): array
  38.     {
  39.         return [
  40.             LogEvent::class => 'onLogEvent',
  41.         ];
  42.     }
  43.     /**
  44.      * @param LogEvent $event
  45.      */
  46.     public function onLogEvent(LogEvent $event): void
  47.     {
  48.         $data $this->serializer->serialize($event->getObject(), 'json', ['groups' => 'event:log']);
  49.         $eventLog = (new EventLog())
  50.             ->setObjectId($event->getObject()->getId())
  51.             ->setObjectFqcn(get_class($event->getObject()))
  52.             ->setBlameObjectId($event->getBlameObject()->getId())
  53.             ->setBlameObjectFqcn(get_class($event->getBlameObject()))
  54.             ->setLogType($event->getLogType())
  55.             ->setData($data);
  56.         $this->eventLogRepository->save($eventLog);
  57.         try {
  58.             $this->notificationService->sendEventLogNotification($eventLog);
  59.         } catch (PusherException $exception) {
  60.             //
  61.         }
  62.     }
  63. }