44
55use Paknahad \JsonApiBundle \Document \ValidationErrorDocument ;
66use Paknahad \JsonApiBundle \Exception \ValidationException ;
7- use Symfony \Bridge \PsrHttpMessage \Factory \ HttpFoundationFactory ;
7+ use Symfony \Bridge \PsrHttpMessage \HttpFoundationFactoryInterface ;
88use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
99use Symfony \Component \HttpKernel \Event \ExceptionEvent ;
1010use Symfony \Component \HttpKernel \Exception \HttpException ;
1111use Symfony \Component \HttpKernel \KernelEvents ;
1212use Symfony \Component \Security \Core \Exception \AuthenticationException ;
1313use Symfony \Component \Validator \Exception \ValidatorException ;
1414use Throwable ;
15- use WoohooLabs \Yin \JsonApi \Exception \DefaultExceptionFactory ;
1615use WoohooLabs \Yin \JsonApi \Exception \JsonApiExceptionInterface ;
1716use WoohooLabs \Yin \JsonApi \JsonApi ;
18- use WoohooLabs \Yin \JsonApi \Response \Responder ;
1917use WoohooLabs \Yin \JsonApi \Schema \Document \ErrorDocument ;
18+ use WoohooLabs \Yin \JsonApi \Schema \Document \ErrorDocumentInterface ;
2019use WoohooLabs \Yin \JsonApi \Schema \Error \Error ;
2120use WoohooLabs \Yin \JsonApi \Schema \Link \DocumentLinks ;
2221use WoohooLabs \Yin \JsonApi \Schema \Link \Link ;
23- use WoohooLabs \Yin \JsonApi \Serializer \JsonSerializer ;
2422
2523class JsonApiErrorHandlerEvent implements EventSubscriberInterface
2624{
25+ /**
26+ * @var string
27+ */
2728 private $ environment ;
28-
29+ /**
30+ * @var JsonApi
31+ */
2932 private $ jsonApi ;
30-
33+ /**
34+ * @var bool
35+ */
3136 private $ debug ;
37+ /**
38+ * @var HttpFoundationFactoryInterface
39+ */
40+ private $ httpFoundationFactory ;
3241
33- public function __construct ($ environment , JsonApi $ jsonApi , $ debug )
42+ public function __construct (string $ environment , bool $ debug , JsonApi $ jsonApi , HttpFoundationFactoryInterface $ httpFoundationFactory )
3443 {
3544 $ this ->environment = $ environment ;
3645 $ this ->jsonApi = $ jsonApi ;
3746 $ this ->debug = $ debug ;
47+ $ this ->httpFoundationFactory = $ httpFoundationFactory ;
3848 }
3949
40- public static function getSubscribedEvents ()
50+ public static function getSubscribedEvents (): array
4151 {
4252 return [
4353 KernelEvents::EXCEPTION => 'onKernelException ' ,
@@ -46,28 +56,17 @@ public static function getSubscribedEvents()
4656
4757 public function onKernelException (ExceptionEvent $ event )
4858 {
49- $ exceptionFactory = new DefaultExceptionFactory ();
50-
5159 $ exception = $ event ->getThrowable ();
5260
53- $ httpFoundationFactory = new HttpFoundationFactory ();
54-
55- $ responder = new Responder (
56- $ this ->jsonApi ->request ,
57- $ this ->jsonApi ->response ,
58- $ exceptionFactory ,
59- new JsonSerializer ()
60- );
61-
6261 $ additionalMeta = $ this ->getAdditionalMeta ($ exception );
6362
64- $ response = $ responder ->genericError (
63+ $ response = $ this -> jsonApi -> respond () ->genericError (
6564 $ this ->toErrorDocument ($ exception , $ event ->getRequest ()->getRequestUri ()),
6665 null ,
6766 $ additionalMeta
6867 );
6968
70- $ event ->setResponse ($ httpFoundationFactory ->createResponse ($ response ));
69+ $ event ->setResponse ($ this -> httpFoundationFactory ->createResponse ($ response ));
7170 }
7271
7372 protected function getExceptionMeta (Throwable $ exception ): array
@@ -90,7 +89,7 @@ protected function getExceptionMeta(Throwable $exception): array
9089 ];
9190 }
9291
93- protected function toErrorDocument (Throwable $ exception , string $ url )
92+ protected function toErrorDocument (Throwable $ exception , string $ url ): ErrorDocumentInterface
9493 {
9594 if ($ exception instanceof JsonApiExceptionInterface) {
9695 return $ exception ->getErrorDocument ();
0 commit comments