Completed
Pull Request — master (#2089)
by Christian
13:35 queued 03:39
created

ExceptionController::getTemplateData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 7
cts 7
cp 1
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 4
crap 3
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\RestBundle\Controller;
13
14
use FOS\RestBundle\Util\ExceptionValueMap;
15
use FOS\RestBundle\View\View;
16
use FOS\RestBundle\View\ViewHandlerInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
use Symfony\Component\HttpFoundation\Response;
19
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
20
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
21
22
/**
23
 * Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
24
 */
25
class ExceptionController
26
{
27
    /**
28
     * @var ViewHandlerInterface
29
     */
30
    private $viewHandler;
31
32
    /**
33
     * @var ExceptionValueMap
34
     */
35
    private $exceptionCodes;
36
37
    /**
38
     * @var bool
39
     */
40
    private $showException;
41
42
    public function __construct(
43 9
        ViewHandlerInterface $viewHandler,
44
        ExceptionValueMap $exceptionCodes,
45
        $showException
46
    ) {
47
        $this->viewHandler = $viewHandler;
48 9
        $this->exceptionCodes = $exceptionCodes;
49 9
        $this->showException = $showException;
50 9
    }
51 9
52
    /**
53
     * Converts an Exception to a Response.
54
     *
55
     * @param Request                   $request
56
     * @param \Exception|\Throwable     $exception
57
     * @param DebugLoggerInterface|null $logger
58
     *
59
     * @throws \InvalidArgumentException
60
     *
61
     * @return Response
62
     */
63
    public function showAction(Request $request, $exception, DebugLoggerInterface $logger = null)
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64 9
    {
65
        $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
0 ignored issues
show
Unused Code introduced by
$currentContent is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
66 9
        $code = $this->getStatusCode($exception);
0 ignored issues
show
Compatibility introduced by
$exception of type object<Throwable> is not a sub-type of object<Exception>. It seems like you assume a concrete implementation of the interface Throwable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
67 9
68 9
        $view = $this->createView($exception, $code, $request, $this->showException);
0 ignored issues
show
Compatibility introduced by
$exception of type object<Throwable> is not a sub-type of object<Exception>. It seems like you assume a concrete implementation of the interface Throwable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
69
        $response = $this->viewHandler->handle($view);
70 9
71 9
        return $response;
72
    }
73 9
74
    /**
75
     * @param \Exception $exception
76
     * @param int        $code
77
     * @param Request    $request
78
     * @param bool       $showException
79
     *
80
     * @return View
81
     */
82
    protected function createView(\Exception $exception, $code, Request $request, $showException)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $showException is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
    {
84
        $view = new View($exception, $code, $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []);
85 9
86
        return $view;
87 9
    }
88 9
89 9
    /**
90
     * Determines the status code to use for the response.
91 9
     *
92
     * @param \Exception $exception
93
     *
94
     * @return int
95
     */
96
    protected function getStatusCode(\Exception $exception)
97
    {
98
        // If matched
99
        if ($statusCode = $this->exceptionCodes->resolveException($exception)) {
100
            return $statusCode;
101 9
        }
102
103
        // Otherwise, default
104 9
        if ($exception instanceof HttpExceptionInterface) {
105
            return $exception->getStatusCode();
106
        }
107
108
        return 500;
109 9
    }
110 1
111
    /**
112
     * Gets and cleans any content that was already outputted.
113 8
     *
114
     * This code comes from Symfony and should be synchronized on a regular basis
115
     * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
116
     *
117
     * @return string
118
     */
119
    private function getAndCleanOutputBuffering($startObLevel)
120
    {
121
        if (ob_get_level() <= $startObLevel) {
122
            return '';
123
        }
124
        Response::closeOutputBuffers($startObLevel + 1, true);
125
126 9
        return ob_get_clean();
127
    }
128
}
129