Completed
Push — master ( 957717...7a0390 )
by Christian
02:31 queued 11s
created

ExceptionController::getStatusCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.3332

Importance

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