src/App/Logger/AppEventLogger.php line 61

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SpringerNature\CPS\AMEDReviewTracker\App\Logger;
  4. use Psr\Log\LoggerInterface;
  5. use SpringerNature\CPS\AMEDReviewTracker\App\Event\AppEvent;
  6. use SpringerNature\CPS\AMEDReviewTracker\App\Event\HasApplicationException;
  7. use Symfony\Component\EventDispatcher\Event;
  8. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class AppEventLogger implements EventDispatcherInterface
  11. {
  12.     /**
  13.      * @var EventDispatcherInterface
  14.      */
  15.     private $dispatcher;
  16.     /**
  17.      * @var LoggerInterface
  18.      */
  19.     private $logger;
  20.     /**
  21.      * @var LoggerInterface
  22.      */
  23.     private $auditLogger;
  24.     public function __construct(
  25.         EventDispatcherInterface $dispatcher,
  26.         LoggerInterface $logger,
  27.         LoggerInterface $auditLogger
  28.     ) {
  29.         $this->dispatcher $dispatcher;
  30.         $this->logger $logger;
  31.         $this->auditLogger $auditLogger;
  32.     }
  33.     /**
  34.      * {@inheritdoc}
  35.      */
  36.     public function dispatch($event/*, string $eventName = null*/)
  37.     {
  38.         $eventName \func_num_args() ? func_get_arg(1) : null;
  39.         if (\is_object($event)) {
  40.             $eventName $eventName ?? \get_class($event);
  41.         } else {
  42.             $swap $event;
  43.             $event $eventName ?? new Event();
  44.             $eventName $swap;
  45.             if ( ! $event instanceof Event) {
  46.                 throw new \TypeError(sprintf('Argument 1 passed to "%s::dispatch()" must be an instance of "%s", "%s" given.'EventDispatcherInterface::class, Event::class, \is_object($event) ? \get_class($event) : \gettype($event)));
  47.             }
  48.         }
  49.         $this->logAppEvent($eventName$event);
  50.         return $this->dispatcher->dispatch($event$eventName);
  51.     }
  52.     /**
  53.      * {@inheritdoc}
  54.      */
  55.     public function addListener($eventName$listener$priority 0): void
  56.     {
  57.         $this->dispatcher->addListener($eventName$listener$priority);
  58.     }
  59.     /**
  60.      * {@inheritdoc}
  61.      */
  62.     public function addSubscriber(EventSubscriberInterface $subscriber): void
  63.     {
  64.         $this->dispatcher->addSubscriber($subscriber);
  65.     }
  66.     /**
  67.      * {@inheritdoc}
  68.      */
  69.     public function removeListener($eventName$listener): void
  70.     {
  71.         $this->dispatcher->removeListener($eventName$listener);
  72.     }
  73.     public function removeSubscriber(EventSubscriberInterface $subscriber): void
  74.     {
  75.         $this->dispatcher->removeSubscriber($subscriber);
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function getListeners($eventName null)
  81.     {
  82.         return $this->dispatcher->getListeners($eventName);
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function getListenerPriority($eventName$listener)
  88.     {
  89.         return $this->dispatcher->getListenerPriority($eventName$listener);
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      */
  94.     public function hasListeners($eventName null)
  95.     {
  96.         return $this->dispatcher->hasListeners($eventName);
  97.     }
  98.     /**
  99.      * @param $eventName
  100.      * @param \Symfony\Contracts\EventDispatcher\Event|Event|null $event
  101.      */
  102.     private function logAppEvent($eventName$event null): void
  103.     {
  104.         if ( ! $event instanceof AppEvent) {
  105.             return;
  106.         }
  107.         if ($event instanceof HasApplicationException && $event->getException()) {
  108.             $this->logger->info($event->getException());
  109.         }
  110.         $this->auditLogger->info($eventName, ['app_event' => $event]);
  111.     }
  112. }