From d9c266f7ac6bf061486cbb8448c90a521610179b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 6 Jan 2025 16:21:39 +0100 Subject: [PATCH] Using the response factory for the validation handler --- ...xceptionToProblemDetailsKernelListener.php | 21 +++++++++---------- ...tionToProblemDetailsKernelListenerTest.php | 9 ++++++-- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/HttpExceptionToProblemDetailsKernelListener.php b/src/HttpExceptionToProblemDetailsKernelListener.php index f19d9da..75f2b75 100644 --- a/src/HttpExceptionToProblemDetailsKernelListener.php +++ b/src/HttpExceptionToProblemDetailsKernelListener.php @@ -4,7 +4,7 @@ namespace Phauthentic\Symfony\ProblemDetails; -use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; @@ -26,6 +26,11 @@ */ class HttpExceptionToProblemDetailsKernelListener { + public function __construct( + protected ProblemDetailsFactoryInterface $problemDetailsFactory + ) { + } + public function onKernelException(ExceptionEvent $event): void { $exception = $event->getThrowable(); @@ -41,18 +46,12 @@ private function isAHttpException(Throwable $exception): bool return $exception instanceof HttpException; } - private function buildResponse(HttpException $httpException): JsonResponse + private function buildResponse(HttpException $httpException): Response { - return new JsonResponse( - data: [ - 'type' => 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/' . $httpException->getStatusCode(), - 'title' => $httpException->getMessage(), - 'status' => $httpException->getStatusCode(), - ], + return $this->problemDetailsFactory->createResponse( status: $httpException->getStatusCode(), - headers: [ - 'Content-Type' => 'application/problem+json', - ] + type: 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/' . $httpException->getStatusCode(), + title: $httpException->getMessage() ); } } diff --git a/tests/Unit/HttpExceptionToProblemDetailsKernelListenerTest.php b/tests/Unit/HttpExceptionToProblemDetailsKernelListenerTest.php index 406f58d..3c534ab 100644 --- a/tests/Unit/HttpExceptionToProblemDetailsKernelListenerTest.php +++ b/tests/Unit/HttpExceptionToProblemDetailsKernelListenerTest.php @@ -5,6 +5,7 @@ namespace Phauthentic\Symfony\ProblemDetails\Tests\Unit; use Exception; +use Phauthentic\Symfony\ProblemDetails\ProblemDetailsFactory; use PHPUnit\Framework\TestCase; use Phauthentic\Symfony\ProblemDetails\HttpExceptionToProblemDetailsKernelListener; use Symfony\Component\HttpFoundation\JsonResponse; @@ -27,7 +28,9 @@ public function testOnKernelExceptionWithHttpException(): void server: ['HTTP_ACCEPT' => 'application/json'] ); $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); - $listener = new HttpExceptionToProblemDetailsKernelListener(); + $listener = new HttpExceptionToProblemDetailsKernelListener( + new ProblemDetailsFactory() + ); // Act $listener->onKernelException($event); @@ -55,7 +58,9 @@ public function testOnKernelExceptionWithNonHttpException(): void server: ['HTTP_ACCEPT' => 'application/json'] ); $event = new ExceptionEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $exception); - $listener = new HttpExceptionToProblemDetailsKernelListener(); + $listener = new HttpExceptionToProblemDetailsKernelListener( + new ProblemDetailsFactory() + ); // Act $listener->onKernelException($event);