src/Event/Subscriber/LoggerSubscriberBase.php line 35

  1. <?php
  2. namespace App\Event\Subscriber;
  3. use App\Entity\LogEntryInterface;
  4. use App\Event\CommonEventInterface;
  5. use App\Utility\LogUtility;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. abstract class LoggerSubscriberBase implements EventSubscriberInterface
  10. {
  11.     public function __construct(
  12.         protected readonly RequestStack $requestStack,
  13.         protected readonly EntityManagerInterface $entityManager,
  14.         protected readonly LogUtility $logUtility
  15.     ) {
  16.     }
  17.     abstract protected function getBaseLogEntry(): LogEntryInterface;
  18.     final protected function addCommonDetailsToLogEntry(LogEntryInterface $entryCommonEventInterface $event): LogEntryInterface
  19.     {
  20.         return $entry->setDetails($this->logUtility->encodeEventDetails($event->getEventDetails()))
  21.             ->setTagGroups($event->getEventTags())
  22.             ->setEventType($event::class);
  23.     }
  24.     public function addAdditionalDetailsToLogEntry(LogEntryInterface $entryCommonEventInterface $event): LogEntryInterface
  25.     {
  26.         return $entry;
  27.     }
  28.     final public function onEvent(CommonEventInterface $event): void
  29.     {
  30.         $entry $this->getBaseLogEntry();
  31.         $entry $this->addCommonDetailsToLogEntry($entry$event);
  32.         $entry $this->addAdditionalDetailsToLogEntry($entry$event);
  33.         $this->entityManager->persist($entry);
  34.         $this->entityManager->flush();
  35.     }
  36. }