Passed
Push — develop ( d2d0cb...6c46b1 )
by Pieter van der
02:42
created

ExceptionController::show()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nc 2
nop 2
dl 0
loc 29
rs 9.584
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Copyright 2018 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Controller;
20
21
use DateTime;
22
use DateTimeInterface;
23
use Surfnet\StepupBundle\Controller\ExceptionController as BaseExceptionController;
24
use Surfnet\StepupBundle\Exception\Art;
25
use Surfnet\StepupBundle\Request\RequestId;
26
use Surfnet\StepupGateway\SecondFactorOnlyBundle\Adfs\Exception\AcsLocationNotAllowedException;
27
use Symfony\Component\HttpFoundation\Request;
28
use Symfony\Component\HttpFoundation\Response;
29
use Symfony\Contracts\Translation\TranslatorInterface;
30
use Throwable;
31
use Twig\Environment;
32
33
final class ExceptionController extends BaseExceptionController
34
{
35
    public function __construct(
36
        protected readonly TranslatorInterface $translator,
37
        // Service: surfnet_stepup.request.request_id
38
        protected RequestId $requestId,
39
        protected Environment $twig
40
    ) {
41
    }
42
43
    /**
44
     * @return array View parameters 'title' and 'description'
45
     */
46
    protected function getPageTitleAndDescription(\Exception|Throwable $exception): array
47
    {
48
        $translator = $this->getTranslator();
49
50
        if ($exception instanceof AcsLocationNotAllowedException) {
51
            return [
52
                'title' => $translator->trans('gateway.error.acs_location_not_allowed.title'),
53
                'description' => $exception->getMessage(),
54
            ];
55
        }
56
57
        return parent::getPageTitleAndDescription($exception);
58
    }
59
60
    public function show(Request $request, Throwable $exception): Response
61
    {
62
        $statusCode = $this->getStatusCode($exception);
63
64
        $template = '@default/bundles/TwigBundle/Exception/error.html.twig';
65
        if ($statusCode == 404) {
66
            $template = '@default/bundles/TwigBundle/Exception/error404.html.twig';
67
        }
68
69
        $timestamp = (new DateTime)->format(DateTimeInterface::ATOM);
70
        $hostname  = $request->getHost();
71
        $requestId = $this->requestId;
72
        $errorCode = Art::forException($exception);
73
        $userAgent = $request->headers->get('User-Agent');
74
        $ipAddress = $request->getClientIp();
75
76
        return new Response(
77
            $this->twig->render(
78
                $template,
79
                [
80
                    'timestamp'   => $timestamp,
81
                    'hostname'    => $hostname,
82
                    'request_id'  => $requestId->get(),
83
                    'error_code'  => $errorCode,
84
                    'user_agent'  => $userAgent,
85
                    'ip_address'  => $ipAddress,
86
                ] + $this->getPageTitleAndDescription($exception),
87
            ),
88
            $statusCode
89
        );
90
    }
91
}
92