Completed
Push — master ( fb3acb...928588 )
by Christian
02:51 queued 10s
created

ExceptionController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 80.95%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 58
ccs 17
cts 21
cp 0.8095
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getAndCleanOutputBuffering() 0 9 2
A __construct() 0 9 1
A showAction() 0 10 2
A getStatusCode() 0 14 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 1
@trigger_error(sprintf('The %s\ExceptionController class is deprecated since FOSRestBundle 2.8.', __NAMESPACE__), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
15
16
use FOS\RestBundle\Util\ExceptionValueMap;
17
use FOS\RestBundle\View\View;
18
use FOS\RestBundle\View\ViewHandlerInterface;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
22
23
/**
24
 * Custom ExceptionController that uses the view layer and supports HTTP response status code mapping.
25
 *
26
 * @deprecated since 2.8
27
 */
28
final class ExceptionController
29
{
30
    private $viewHandler;
31
    private $exceptionCodes;
32
    private $showException;
33
34 2
    public function __construct(
35
        ViewHandlerInterface $viewHandler,
36
        ExceptionValueMap $exceptionCodes,
37
        bool $showException
38
    ) {
39 2
        $this->viewHandler = $viewHandler;
40 2
        $this->exceptionCodes = $exceptionCodes;
41 2
        $this->showException = $showException;
42 2
    }
43
44 2
    public function showAction(Request $request, \Throwable $exception): Response
45
    {
46 2
        $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
47
48 2
        $view = new View($exception, $this->getStatusCode($exception), $exception instanceof HttpExceptionInterface ? $exception->getHeaders() : []);
49
50 2
        $response = $this->viewHandler->handle($view);
51
52 2
        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 2
    private function getAndCleanOutputBuffering($startObLevel): string
62
    {
63 2
        if (ob_get_level() <= $startObLevel) {
64 2
            return '';
65
        }
66
        Response::closeOutputBuffers($startObLevel + 1, true);
67
68
        return ob_get_clean();
69
    }
70
71 2
    private function getStatusCode(\Throwable $exception): int
72
    {
73
        // If matched
74 2
        if (null !== $statusCode = $this->exceptionCodes->resolveException($exception)) {
75
            return $statusCode;
76
        }
77
78
        // Otherwise, default
79 2
        if ($exception instanceof HttpExceptionInterface) {
80
            return $exception->getStatusCode();
81
        }
82
83 2
        return 500;
84
    }
85
}
86