Passed
Push — master ( 92d87b...b3073b )
by Julito
08:24
created

ExceptionController::errorAction()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 33
rs 9.7
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Controller;
6
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\ErrorHandler\Exception\FlattenException;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Exception\HttpException;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class ExceptionController extends AbstractController
14
{
15
    public function showAction(FlattenException $exception)
16
    {
17
        if ('dev' === $this->getParameter('app_env')) {
18
            throw new HttpException($exception->getCode(), $exception->getMessage());
19
        }
20
21
        $showException = true;
22
        //$name = $showException ? 'exception' : 'error';
23
        $name = 'exception';
24
        $code = $exception->getCode();
25
        $format = 'html';
26
        $loader = $this->container->get('twig')->getLoader();
27
28
        // when not in debug, try to find a template for the specific HTTP status code and format
29
        $template = sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
30
        if ($loader->exists($template)) {
31
            return $template;
32
        }
33
34
        // try to find a template for the given format
35
        $template = sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
36
        if ($loader->exists($template)) {
37
            return $template;
38
        }
39
40
        // default to a generic HTML exception
41
        //$request->setRequestFormat('html');
42
        //$template = sprintf('@ChamiloCore/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
43
        $template = sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
44
45
        return $this->render($template, ['exception' => $exception]);
46
    }
47
48
    /**
49
     * @Route("/error")
50
     */
51
    public function errorAction(Request $request)
52
    {
53
        $message = $request->getSession()->get('error_message', '');
54
        $exception = new FlattenException();
55
        $exception->setCode(500);
56
57
        $exception->setMessage($message);
58
59
        $showException = true;
60
        //$name = $showException ? 'exception' : 'error';
61
        $name = 'exception';
62
        $code = $exception->getCode();
63
        $format = 'html';
64
        $loader = $this->container->get('twig')->getLoader();
65
        // when not in debug, try to find a template for the specific HTTP status code and format
66
        //if (!$showException) {
67
        $template = sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
68
        if ($loader->exists($template)) {
69
            return $template;
70
        }
71
        //}
72
73
        // try to find a template for the given format
74
        $template = sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
75
        if ($loader->exists($template)) {
76
            return $template;
77
        }
78
79
        // default to a generic HTML exception
80
        //$request->setRequestFormat('html');
81
        $template = sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
82
83
        return $this->render($template, ['exception' => $exception]);
84
    }
85
}
86