Passed
Pull Request — master (#34)
by Paweł
03:02
created

ApiExceptionSubscriber::onExceptionEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
eloc 7
c 2
b 0
f 2
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
6
use Symfony\Component\HttpFoundation\JsonResponse;
7
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
8
9
class ApiExceptionSubscriber implements EventSubscriberInterface
10
{
11
    public function onExceptionEvent(ExceptionEvent $event)
12
    {
13
        $request = $event->getRequest();
14
        if ('application/json' === $request->headers->get('Content-Type')) {
15
            $event->allowCustomResponseCode();
16
            $event->setResponse(new JsonResponse([
17
                'success' => false,
18
                'message' => $event->getException()->getMessage(),
19
            ], $event->getException()->getStatusCode()));
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Exception. It seems like you code against a sub-type of Exception such as SWP\Component\Common\Exception\HttpException or Aws\Exception\AwsException or Symfony\Component\HttpKe...Exception\HttpException. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
            ], $event->getException()->/** @scrutinizer ignore-call */ getStatusCode()));
Loading history...
20
        }
21
    }
22
23
    public static function getSubscribedEvents()
24
    {
25
        return [
26
            ExceptionEvent::class => 'onExceptionEvent',
27
        ];
28
    }
29
}
30