<?php
namespace CodersLab\Lms\SharedKernel\Application;
use CodersLab\Lms\SharedKernel\Application\Response\ExceptionMetadata;
use CodersLab\Lms\SharedKernel\Application\Response\LmsApiResponse;
use CodersLab\Lms\SharedKernel\Common\Exception\LmsApiException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
final class LmsExceptionListener
{
public function onKernelException(ExceptionEvent $event): void
{
$exception = $event->getThrowable();
if (!$exception instanceof LmsApiException) {
return;
}
$metadata = new ExceptionMetadata(
$exception->getMessage(),
$exception->getCode()
);
$event->setResponse(
new LmsApiResponse(
[],
$metadata,
$metadata->getCode() === 0 ? Response::HTTP_INTERNAL_SERVER_ERROR : $metadata->getCode()
)
);
}
}