<?php
namespace App\Event\EventSubscriber;
use App\Event\SaveExceptionEvent;
use App\Api\Telegram\BaseBot;
use App\Api\Telegram\ExceptionNotificationBot;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class LogSubscriber implements EventSubscriberInterface
{
private LoggerInterface $errorsLogger;
private ExceptionNotificationBot $telegramBot;
/**
* @param LoggerInterface $errorsLogger
* @param ExceptionNotificationBot $telegramBot
*/
public function __construct(LoggerInterface $errorsLogger, ExceptionNotificationBot $telegramBot)
{
$this->errorsLogger = $errorsLogger;
$this->telegramBot = $telegramBot;
}
/**
* @inheritDoc
*/
public static function getSubscribedEvents(): array
{
return [
SaveExceptionEvent::class => 'onSaveExceptionLog',
];
}
/**
* @param SaveExceptionEvent $event
*
* @return void
*/
public function onSaveExceptionLog(SaveExceptionEvent $event): void
{
$this->errorsLogger->error(sprintf('Type: %s. %s. Data: %s.',
$event->getType(),
$event->getMessage(),
$event->getData()
));
$msg = sprintf("Type: %s\nMessage: %s\nData: %s",
$event->getType(),
$event->getMessage(),
$event->getData()
);
$chunks = str_split($msg, BaseBot::MESSAGE_LENGTH);
$totalChunks = count($chunks);
foreach($chunks as $position => $chunk) {
$this->telegramBot->notify($chunk);
if ($totalChunks > 1 && $position !== $totalChunks - 1) {
sleep(12);
}
}
}
}