Passed
Pull Request — master (#7192)
by Angel Fernando Quiroz
21:00 queued 11:43
created

ErrorController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 3
dl 0
loc 7
rs 10
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Symfony\Component\DependencyInjection\Attribute\Autowire;
10
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
14
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
15
use Twig\Environment;
16
use Twig\Error\LoaderError;
17
use Twig\Error\RuntimeError;
18
use Twig\Error\SyntaxError;
19
20
class ErrorController
21
{
22
    public function __construct(
23
        private readonly Environment $twig,
24
        #[Autowire(env: 'APP_ENV')]
25
        private readonly string $environment,
26
        #[Autowire(service: 'error_renderer')]
27
        private readonly ErrorRendererInterface $errorRenderer,
28
    ) {}
29
30
    /**
31
     * @throws SyntaxError
32
     * @throws RuntimeError
33
     * @throws LoaderError
34
     */
35
    public function show(
36
        Request $request,
37
        \Throwable $exception,
38
        ?DebugLoggerInterface $logger = null,
39
    ): Response {
40
        $statusCode = 500;
41
42
        if ($exception instanceof HttpExceptionInterface) {
43
            $statusCode = $exception->getStatusCode();
0 ignored issues
show
Bug introduced by
The method getStatusCode() does not exist on Throwable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
            /** @scrutinizer ignore-call */ 
44
            $statusCode = $exception->getStatusCode();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        }
45
46
        if (in_array($this->environment, ['dev', 'test'])) {
47
            $exception = $this->errorRenderer->render($exception);
48
49
            $content = $exception->getAsString();
50
            $headers = $exception->getHeaders();
51
        } else {
52
            $format = $request->getPreferredFormat();
53
54
            if ('html' === $format) {
55
                $content = $this->twig->render(
56
                    '@ChamiloCore/Layout/no_layout.html.twig',
57
                    [
58
                        'exception' => $exception,
59
                        'content' => '',
60
                    ]
61
                );
62
            } else {
63
                $exception = $this->errorRenderer->render($exception);
64
65
                $content = $exception->getAsString();
66
            }
67
68
            $headers = [];
69
        }
70
71
        return new Response($content, $statusCode, $headers);
72
    }
73
}