<?php
namespace App\Floa\Exception;
use Crea\FloaBundle\Event\FloaExceptionEvent;
use App\Helper\EmailSenderHelper;
use Crea\FloaBundle\Normalizer\FloaExceptionNormalizer;
use Crea\FloaBundle\Exception\AbstractFloaException;
use Crea\FloaBundle\FloaEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Serializer;
use Throwable;
class FloaExceptionEventSubscriber implements EventSubscriberInterface
{
protected EmailSenderHelper $emailSenderHelper;
protected string $mailTo;
public function __construct(EmailSenderHelper $emailSenderHelper, string $mailTo)
{
$this->emailSenderHelper = $emailSenderHelper;
$this->mailTo = $mailTo;
}
public static function getSubscribedEvents(): array
{
return [
FloaEvents::MISSING_ADAPTER_EXCEPTION => [['handleException', 256]],
FloaEvents::MISSING_ELIGIBILITY_EXCEPTION => [['handleException', 256]],
FloaEvents::CLIENT_TOKEN_EXCEPTION => [['handleException', 256]],
FloaEvents::CLIENT_PRE_ELIGIBILITY_EXCEPTION => [['handleException', 256]],
FloaEvents::CLIENT_ELIGIBILITY_EXCEPTION => [['handleException', 256]],
FloaEvents::CLIENT_PAYMENT_SESSION_EXCEPTION => [['handleException', 256]],
];
}
/**
* @throws ExceptionInterface
*/
public function handleException(FloaExceptionEvent $event)
{
if ($this->supportException($exception = $event->getException())) {
$serializer = new Serializer([new FloaExceptionNormalizer()]);
$this->emailSenderHelper->sendTextEmail(
[$this->mailTo],
"Floa exception",
print_r($serializer->normalize($exception), true)
);
}
}
private function supportException(Throwable $exception): bool
{
return $exception instanceof AbstractFloaException;
}
}