Skip to content

Commit 98c75e1

Browse files
mpaden3paknahad
authored andcommitted
use symfony dependency injection
1 parent 871bb8d commit 98c75e1

3 files changed

Lines changed: 54 additions & 30 deletions

File tree

src/EventSubscriber/JsonApiErrorHandlerEvent.php

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,50 @@
44

55
use Paknahad\JsonApiBundle\Document\ValidationErrorDocument;
66
use Paknahad\JsonApiBundle\Exception\ValidationException;
7-
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
7+
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
88
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
99
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
1010
use Symfony\Component\HttpKernel\Exception\HttpException;
1111
use Symfony\Component\HttpKernel\KernelEvents;
1212
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1313
use Symfony\Component\Validator\Exception\ValidatorException;
1414
use Throwable;
15-
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;
1615
use WoohooLabs\Yin\JsonApi\Exception\JsonApiExceptionInterface;
1716
use WoohooLabs\Yin\JsonApi\JsonApi;
18-
use WoohooLabs\Yin\JsonApi\Response\Responder;
1917
use WoohooLabs\Yin\JsonApi\Schema\Document\ErrorDocument;
18+
use WoohooLabs\Yin\JsonApi\Schema\Document\ErrorDocumentInterface;
2019
use WoohooLabs\Yin\JsonApi\Schema\Error\Error;
2120
use WoohooLabs\Yin\JsonApi\Schema\Link\DocumentLinks;
2221
use WoohooLabs\Yin\JsonApi\Schema\Link\Link;
23-
use WoohooLabs\Yin\JsonApi\Serializer\JsonSerializer;
2422

2523
class 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();

src/Factory/JsonApiFactory.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,46 @@
55
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
66
use Symfony\Component\HttpFoundation\RequestStack;
77
use Symfony\Component\HttpFoundation\Response;
8-
use WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory;
8+
use WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface;
99
use WoohooLabs\Yin\JsonApi\JsonApi;
1010
use WoohooLabs\Yin\JsonApi\Request\JsonApiRequest;
11+
use WoohooLabs\Yin\JsonApi\Serializer\SerializerInterface;
1112

1213
class JsonApiFactory
1314
{
15+
/**
16+
* @var PsrHttpFactory
17+
*/
1418
private $psrFactory;
1519

20+
/**
21+
* @var RequestStack
22+
*/
1623
private $requestStack;
24+
/**
25+
* @var ExceptionFactoryInterface
26+
*/
27+
private $exceptionFactory;
28+
/**
29+
* @var SerializerInterface
30+
*/
31+
private $serializer;
1732

18-
public function __construct(PsrHttpFactory $psrFactory, RequestStack $requestStack)
33+
public function __construct(PsrHttpFactory $psrFactory, RequestStack $requestStack, ExceptionFactoryInterface $exceptionFactory, SerializerInterface $serializer)
1934
{
2035
$this->psrFactory = $psrFactory;
2136
$this->requestStack = $requestStack;
37+
$this->exceptionFactory = $exceptionFactory;
38+
$this->serializer = $serializer;
2239
}
2340

24-
public function create()
41+
public function create(): JsonApi
2542
{
2643
$jsonApiRequest = new JsonApiRequest(
2744
$this->psrFactory->createRequest($this->requestStack->getCurrentRequest()),
28-
new DefaultExceptionFactory()
45+
$this->exceptionFactory
2946
);
3047

31-
return new JsonApi($jsonApiRequest, $this->psrFactory->createResponse(new Response()));
48+
return new JsonApi($jsonApiRequest, $this->psrFactory->createResponse(new Response()), $this->exceptionFactory, $this->serializer);
3249
}
3350
}

src/Resources/config/services.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99

1010
<service id="jsonapi.error.handler.event" class="Paknahad\JsonApiBundle\EventSubscriber\JsonApiErrorHandlerEvent" lazy="true">
1111
<tag name="event_subscriber" />
12-
<argument type="service" id="WoohooLabs\Yin\JsonApi\JsonApi" />
12+
<argument key="$jsonApi" type="service" id="WoohooLabs\Yin\JsonApi\JsonApi" />
1313
<argument key="$environment">%kernel.environment%</argument>
1414
<argument key="$debug">%kernel.debug%</argument>
15+
<argument key="$httpFoundationFactory" type="service"
16+
id="sensio_framework_extra.psr7.http_foundation_factory"/>
1517
</service>
1618

17-
<service id="jsonapi.jsonApi.factory" class="Paknahad\JsonApiBundle\Factory\JsonApiFactory">
19+
<service id="paknahad_json_api.factory.json_api_factory" class="Paknahad\JsonApiBundle\Factory\JsonApiFactory">
1820
<argument type="service" id="sensio_framework_extra.psr7.http_message_factory" />
1921
<argument type="service" id="request_stack" />
22+
<argument type="service" id="WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface"/>
23+
<argument type="service" id="woohoo_labs.yin.json_api.serializer.json_serializer"/>
2024
</service>
2125

2226
<service id="WoohooLabs\Yin\JsonApi\JsonApi" class="WoohooLabs\Yin\JsonApi\JsonApi">
23-
<factory service="jsonapi.jsonApi.factory" method="create" />
27+
<factory service="paknahad_json_api.factory.json_api_factory" method="create" />
2428
</service>
2529

2630
<service id="jsonapi.postman_collection_generator" class="Paknahad\JsonApiBundle\Collection\PostmanCollectionGenerator">
@@ -66,9 +70,13 @@
6670

6771
<service id="Paknahad\JsonApiBundle\Helper\Sorter" />
6872
<service id="Paknahad\JsonApiBundle\Helper\FieldManager" />
73+
6974
<service id="WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory" />
7075
<service id="WoohooLabs\Yin\JsonApi\Exception\ExceptionFactoryInterface" alias="WoohooLabs\Yin\JsonApi\Exception\DefaultExceptionFactory"/>
7176

77+
<service id="woohoo_labs.yin.json_api.serializer.json_serializer" class="WoohooLabs\Yin\JsonApi\Serializer\JsonSerializer"/>
78+
<service id="WoohooLabs\Yin\JsonApi\Serializer\SerializerInterface" alias="woohoo_labs.yin.json_api.serializer.json_serializer"/>
79+
7280
<service id="Paknahad\JsonApiBundle\Helper\ResourceCollection" class="Paknahad\JsonApiBundle\Helper\ResourceCollection">
7381
<argument type="service" id="request_stack"/>
7482
<argument type="service" id="Paknahad\JsonApiBundle\Helper\Filter\FinderCollection" />

0 commit comments

Comments
 (0)