Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
37.50% |
6 / 16 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SendKernelExceptionOnCaughtExceptionListener | |
37.50% |
6 / 16 |
|
66.67% |
2 / 3 |
14.79 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSubscribedEvents | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| onApieResponseCreated | |
16.67% |
2 / 12 |
|
0.00% |
0 / 1 |
13.26 | |||
| 1 | <?php |
| 2 | namespace Apie\ApieBundle\EventListeners; |
| 3 | |
| 4 | use Apie\Common\Events\ApieResponseCreated; |
| 5 | use Apie\Core\Actions\ActionResponse; |
| 6 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 7 | use Symfony\Component\HttpFoundation\RequestStack; |
| 8 | use Symfony\Component\HttpKernel\Event\ExceptionEvent; |
| 9 | use Symfony\Component\HttpKernel\EventListener\ProfilerListener; |
| 10 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 11 | |
| 12 | class SendKernelExceptionOnCaughtExceptionListener implements EventSubscriberInterface |
| 13 | { |
| 14 | public function __construct( |
| 15 | private readonly HttpKernelInterface $kernel, |
| 16 | private readonly RequestStack $requestStack, |
| 17 | private readonly ?ProfilerListener $profilerListener = null |
| 18 | ) { |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @return array<string, array<int, string>> |
| 23 | */ |
| 24 | public static function getSubscribedEvents(): array |
| 25 | { |
| 26 | return [ |
| 27 | ApieResponseCreated::class => ['onApieResponseCreated'], |
| 28 | ]; |
| 29 | } |
| 30 | |
| 31 | public function onApieResponseCreated(ApieResponseCreated $event): void |
| 32 | { |
| 33 | if (!$this->profilerListener) { |
| 34 | return; |
| 35 | } |
| 36 | $actionResponse = $event->context->getContext(ActionResponse::class, false); |
| 37 | if ($actionResponse instanceof ActionResponse && isset($actionResponse->error)) { |
| 38 | $this->profilerListener->onKernelException( |
| 39 | new ExceptionEvent( |
| 40 | $this->kernel, |
| 41 | $this->requestStack->getMainRequest(), |
| 42 | HttpKernelInterface::MAIN_REQUEST, |
| 43 | $actionResponse->error |
| 44 | ) |
| 45 | ); |
| 46 | } |
| 47 | } |
| 48 | } |