Completed
Push — master ( 412d43...17d3d4 )
by Christian
06:13 queued 28s
created

ExceptionController::createViewFromThrowable()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 3
cp 0.6667
rs 10
c 0
b 0
f 0
cc 2
nc 1
nop 3
crap 2.1481

1 Method

Rating   Name   Duplication   Size   Complexity  
A ExceptionController::getStatusCode() 0 4 1
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
67 9
        if ($exception instanceof \Exception) {
68 9
            $code = $this->getStatusCode($exception);
69
        } else {
70 9
            $code = $this->getStatusCodeFromThrowable($exception);
71 9
        }
72
        if ($exception instanceof \Exception) {
73 9
            $view = $this->createView($exception, $code, $request, $this->showException);
74
        } else {
75
            $view = new View($exception, $code, $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []);
76
        }
77
78
        $response = $this->viewHandler->handle($view);
79
80
        return $response;
81
    }
82
83
    /**
84
     * @param \Exception $exception
85 9
     * @param int        $code
86
     * @param Request    $request
87 9
     * @param bool       $showException
88 9
     *
89 9
     * @return View
90
     */
91 9
    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...
92
    {
93
        return new View($exception, $code, $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []);
94
    }
95
96
    /**
97
     * Determines the status code to use for the response.
98
     *
99
     * @param \Exception $exception
100
     *
101 9
     * @return int
102
     */
103
    protected function getStatusCode(\Exception $exception)
104 9
    {
105
        return $this->getStatusCodeFromThrowable($exception);
106
    }
107
108
    /**
109 9
     * Gets and cleans any content that was already outputted.
110 1
     *
111
     * This code comes from Symfony and should be synchronized on a regular basis
112
     * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
113 8
     *
114
     * @return string
115
     */
116
    private function getAndCleanOutputBuffering($startObLevel)
117
    {
118
        if (ob_get_level() <= $startObLevel) {
119
            return '';
120
        }
121
        Response::closeOutputBuffers($startObLevel + 1, true);
122
123
        return ob_get_clean();
124
    }
125
126 9
    /**
127
     * Determines the status code to use for the response.
128
     *
129 9
     * @param \Throwable $exception
130 9
     *
131 9
     * @return int
132 9
     */
133 9
    private function getStatusCodeFromThrowable(\Throwable $exception)
134 9
    {
135 9
        // If matched
136
        if ($statusCode = $this->exceptionCodes->resolveThrowable($exception)) {
137
            return $statusCode;
138
        }
139
140
        // Otherwise, default
141
        if ($exception instanceof HttpExceptionInterface) {
142
            return $exception->getStatusCode();
143
        }
144
145
        return 500;
146 9
    }
147
}
148