Passed
Pull Request — master (#6316)
by
unknown
08:27
created

ExceptionController::undefinedUrlError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 17
nc 1
nop 3
dl 0
loc 26
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper;
10
use Exception;
11
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
12
use Symfony\Component\ErrorHandler\Exception\FlattenException;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpKernel\Exception\HttpException;
16
use Symfony\Component\Routing\Attribute\Route;
17
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
19
class ExceptionController extends AbstractController
20
{
21
    public function show(Exception $exception): Response
22
    {
23
        if ('dev' === (string) $this->getParameter('app_env')) {
24
            throw new HttpException($exception->getCode(), $exception->getMessage());
25
        }
26
27
        $showException = true;
28
        // $name = $showException ? 'exception' : 'error';
29
        $name = 'exception';
30
        $code = $exception->getCode();
31
        $format = 'html';
32
        $loader = $this->container->get('twig')->getLoader();
33
34
        $templateToLoad = \sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
35
36
        // when not in debug, try to find a template for the specific HTTP status code and format
37
        $template = \sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
38
        if ($loader->exists($template)) {
39
            $templateToLoad = $template;
40
        }
41
42
        // try to find a template for the given format
43
        $template = \sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
44
        if ($loader->exists($template)) {
45
            $templateToLoad = $template;
46
        }
47
48
        // default to a generic HTML exception
49
        // $request->setRequestFormat('html');
50
        // $template = sprintf('@ChamiloCore/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
51
52
        return $this->render($templateToLoad, [
53
            'exception' => $exception,
54
        ]);
55
    }
56
57
    #[Route(path: '/error')]
58
    public function error(Request $request): Response
59
    {
60
        $message = $request->getSession()->get('error_message', '');
61
        $exception = new FlattenException();
62
        $exception->setCode(500);
63
64
        $exception->setMessage($message);
65
66
        $showException = true;
67
        // $name = $showException ? 'exception' : 'error';
68
        $name = 'exception';
69
        $code = $exception->getCode();
70
        $format = 'html';
71
        $loader = $this->container->get('twig')->getLoader();
72
73
        $templateToLoad = \sprintf('@ChamiloCore/Exception/%s.html.twig', 'exception_full');
74
75
        // when not in debug, try to find a template for the specific HTTP status code and format
76
        // if (!$showException) {
77
        $template = \sprintf('@ChamiloCore/Exception/%s%s.%s.twig', $name, $code, $format);
78
        if ($loader->exists($template)) {
79
            $templateToLoad = $template;
80
        }
81
        // }
82
83
        // try to find a template for the given format
84
        $template = \sprintf('@ChamiloCore/Exception/%s.%s.twig', $name, $format);
85
        if ($loader->exists($template)) {
86
            $templateToLoad = $template;
87
        }
88
89
        // default to a generic HTML exception
90
        // $request->setRequestFormat('html');
91
92
        return $this->render($templateToLoad, [
93
            'exception' => $exception,
94
        ]);
95
    }
96
97
    #[Route(path: '/error/undefined-url', name: 'undefined_url_error')]
98
    public function undefinedUrlError(
99
        Request $request,
100
        UrlGeneratorInterface $urlGenerator,
101
        AccessUrlHelper $accessUrlHelper
102
    ): Response {
103
        $host = $request->getHost();
104
105
        $accessUrl = $accessUrlHelper->getFirstAccessUrl();
106
        $themeHost = rtrim($accessUrl?->getUrl() ?? '', '/');
107
        $themeName = 'chamilo';
108
109
        $cssUrl = $themeHost . $urlGenerator->generate('theme_asset', [
110
                'name' => $themeName,
111
                'path' => 'colors.css',
112
            ], UrlGeneratorInterface::ABSOLUTE_PATH);
113
114
        $logoUrl = $themeHost . $urlGenerator->generate('theme_asset', [
115
                'name' => $themeName,
116
                'path' => 'images/header-logo.svg',
117
            ], UrlGeneratorInterface::ABSOLUTE_PATH);
118
119
        return $this->render('@ChamiloCore/Exception/undefined_url.html.twig', [
120
            'host' => $host,
121
            'cssUrl' => $cssUrl,
122
            'logoUrl' => $logoUrl,
123
        ]);
124
    }
125
}
126