Completed
Push — master ( 7d275f...5e5f47 )
by Kévin
10s
created

ExceptionAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 17 3
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ApiPlatform\Core\Action;
13
14
use ApiPlatform\Core\Exception\InvalidArgumentException;
15
use ApiPlatform\Core\Util\ErrorFormatGuesser;
16
use Symfony\Component\Debug\Exception\FlattenException;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\Serializer\Exception\ExceptionInterface;
20
use Symfony\Component\Serializer\SerializerInterface;
21
22
/**
23
 * Renders a normalized exception for a given {@see \Symfony\Component\Debug\Exception\FlattenException}.
24
 *
25
 * @author Baptiste Meyer <[email protected]>
26
 * @author Kévin Dunglas <[email protected]>
27
 */
28
final class ExceptionAction
29
{
30
    const DEFAULT_EXCEPTION_TO_STATUS = [
31
        ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
32
        InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
33
    ];
34
35
    private $serializer;
36
    private $errorFormats;
37
    private $exceptionToStatus;
38
39
    public function __construct(SerializerInterface $serializer, array $errorFormats, $exceptionToStatus = [])
40
    {
41
        $this->serializer = $serializer;
42
        $this->errorFormats = $errorFormats;
43
        $this->exceptionToStatus = array_merge(self::DEFAULT_EXCEPTION_TO_STATUS, $exceptionToStatus);
44
    }
45
46
    /**
47
     * Converts a an exception to a JSON response.
48
     *
49
     * @param \Exception|FlattenException $exception
50
     * @param Request                     $request
51
     *
52
     * @return Response
53
     */
54
    public function __invoke($exception, Request $request) : Response
55
    {
56
        $exceptionClass = $exception->getClass();
0 ignored issues
show
Bug introduced by
The method getClass does only exist in Symfony\Component\Debug\Exception\FlattenException, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
57
        foreach ($this->exceptionToStatus as $class => $status) {
58
            if (is_a($exceptionClass, $class, true)) {
59
                $exception->setStatusCode($status);
0 ignored issues
show
Bug introduced by
The method setStatusCode does only exist in Symfony\Component\Debug\Exception\FlattenException, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
60
61
                break;
62
            }
63
        }
64
65
        $headers = $exception->getHeaders();
0 ignored issues
show
Bug introduced by
The method getHeaders does only exist in Symfony\Component\Debug\Exception\FlattenException, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66
        $format = ErrorFormatGuesser::guessErrorFormat($request, $this->errorFormats);
67
        $headers['Content-Type'] = $format['value'][0];
68
69
        return new Response($this->serializer->serialize($exception, $format['key']), $exception->getStatusCode(), $headers);
0 ignored issues
show
Bug introduced by
The method getStatusCode does only exist in Symfony\Component\Debug\Exception\FlattenException, but not in Exception.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
    }
71
}
72