src/Event/EventSubscriber/LogSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Event\EventSubscriber;
  3. use App\Event\SaveExceptionEvent;
  4. use App\Api\Telegram\BaseBot;
  5. use App\Api\Telegram\ExceptionNotificationBot;
  6. use Psr\Log\LoggerInterface;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. final class LogSubscriber implements EventSubscriberInterface
  9. {
  10.     private LoggerInterface $errorsLogger;
  11.     private ExceptionNotificationBot $telegramBot;
  12.     /**
  13.      * @param LoggerInterface $errorsLogger
  14.      * @param ExceptionNotificationBot $telegramBot
  15.      */
  16.     public function __construct(LoggerInterface $errorsLoggerExceptionNotificationBot $telegramBot)
  17.     {
  18.         $this->errorsLogger $errorsLogger;
  19.         $this->telegramBot  $telegramBot;
  20.     }
  21.     /**
  22.      * @inheritDoc
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             SaveExceptionEvent::class => 'onSaveExceptionLog',
  28.         ];
  29.     }
  30.     /**
  31.      * @param SaveExceptionEvent $event
  32.      *
  33.      * @return void
  34.      */
  35.     public function onSaveExceptionLog(SaveExceptionEvent $event): void
  36.     {
  37.         $this->errorsLogger->error(sprintf('Type: %s. %s. Data: %s.',
  38.             $event->getType(),
  39.             $event->getMessage(),
  40.             $event->getData()
  41.         ));
  42.         $msg sprintf("Type: %s\nMessage: %s\nData: %s",
  43.             $event->getType(),
  44.             $event->getMessage(),
  45.             $event->getData()
  46.         );
  47.         $chunks str_split($msgBaseBot::MESSAGE_LENGTH);
  48.         $totalChunks count($chunks);
  49.         foreach($chunks as $position => $chunk) {
  50.             $this->telegramBot->notify($chunk);
  51.             if ($totalChunks && $position !== $totalChunks 1) {
  52.                 sleep(12);
  53.             }
  54.         }
  55.     }
  56. }