Completed
Pull Request — master (#2127)
by Christian
02:36
created

ExceptionController::getAndCleanOutputBuffering()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 5
cp 0.6
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.2559
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
final class ExceptionController
26
{
27
    private $viewHandler;
28
    private $exceptionCodes;
29
    private $showException;
30
31 9
    public function __construct(
32
        ViewHandlerInterface $viewHandler,
33
        ExceptionValueMap $exceptionCodes,
34
        bool $showException
35
    ) {
36 9
        $this->viewHandler = $viewHandler;
37 9
        $this->exceptionCodes = $exceptionCodes;
38 9
        $this->showException = $showException;
39 9
    }
40
41
    /**
42
     * @param \Throwable $exception
43
     */
44 9
    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...
45
    {
46 9
        $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...
47
48 9
        $view = new View($exception, $this->getStatusCode($exception), $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []);
49
50 9
        $response = $this->viewHandler->handle($view);
51
52 9
        return $response;
53
    }
54
55
    /**
56
     * Gets and cleans any content that was already outputted.
57
     *
58
     * This code comes from Symfony and should be synchronized on a regular basis
59
     * see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
60
     */
61 9
    private function getAndCleanOutputBuffering($startObLevel): string
62
    {
63 9
        if (ob_get_level() <= $startObLevel) {
64 9
            return '';
65
        }
66
        Response::closeOutputBuffers($startObLevel + 1, true);
67
68
        return ob_get_clean();
69
    }
70
71 9
    private function getStatusCode(\Throwable $exception): int
72
    {
73
        // If matched
74 9
        if (null !== $statusCode = $this->exceptionCodes->resolveException($exception)) {
75
            return $statusCode;
76
        }
77
78
        // Otherwise, default
79 9
        if ($exception instanceof HttpExceptionInterface) {
80 1
            return $exception->getStatusCode();
81
        }
82
83 8
        return 500;
84
    }
85
}
86