Completed
Push — master ( 8748d1...422e48 )
by Benjamin
04:33 queued 02:20
created

ExceptionController::findTemplate()   C

Complexity

Conditions 8
Paths 20

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 26
rs 5.3846
cc 8
eloc 13
nc 20
nop 4
1
<?php
2
3
namespace Controller;
4
5
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseController;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpKernel\Exception\FlattenException;
8
9
class ExceptionController extends BaseController
10
{
11
    /**
12
     * @Route("/erreur", name="error")
13
     */
14
    public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null)
15
    {
16
        $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
17
        $showException = $request->attributes->get('showException', $this->debug); // As opposed to an additional parameter, this maintains BC
18
19
        $code = $exception->getStatusCode();
20
21
        return new Response($this->twig->render(
22
            (string) $this->findTemplate($request, $request->getRequestFormat(), $code, $showException),
23
            array(
24
                'status_code' => $code,
25
                'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
26
                'exception' => $exception,
27
                'logger' => $logger,
28
                'currentContent' => $currentContent,
29
            )
30
        ));
31
    }
32
33
    /**
34
     * @param Request $request
35
     * @param string  $format
36
     * @param int     $code          An HTTP response status code
37
     * @param bool    $showException
38
     *
39
     * @return string
40
     */
41
    protected function findTemplate(Request $request, $format, $code, $showException)
42
    {
43
        $name = $showException ? 'exception' : 'error';
44
        if ($showException && 'html' == $format) {
45
            $name = 'exception_full';
46
        }
47
48
        // For error pages, try to find a template for the specific HTTP status code and format
49
        if (!$showException) {
50
            $template = sprintf('@Twig/Exception/%s%s.%s.twig', $name, $code, $format);
51
            if ($this->templateExists($template)) {
52
                return $template;
53
            }
54
        }
55
56
        // try to find a template for the given format
57
        $template = sprintf('@Twig/Exception/%s.%s.twig', $name, $format);
58
        if ($this->templateExists($template)) {
59
            return $template;
60
        }
61
62
        // default to a generic HTML exception
63
        $request->setRequestFormat('html');
64
65
        return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
66
    }
67
}
68