Passed
Push — feature/symfony6-upgrade ( 89b1cf...fdf01e )
by Michiel
05:53
created

ExceptionController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 53
rs 10
c 2
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageTitleAndDescription() 0 17 5
A show() 0 29 2
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2018 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller;
22
23
use DateTime;
24
use DateTimeInterface;
25
use Surfnet\StepupBundle\Exception\Art;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Throwable;
29
use Surfnet\StepupBundle\Controller\ExceptionController as BaseExceptionController;
30
use Surfnet\StepupSelfService\SelfServiceBundle\Exception\MissingRequiredAttributeException;
31
use Symfony\Component\HttpKernel\Exception\HttpException;
32
33
final class ExceptionController extends BaseExceptionController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ExceptionController
Loading history...
34
{
35
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $exception should have a doc-comment as per coding-style.
Loading history...
36
     * @return array View parameters 'title' and 'description'
37
     */
38
    protected function getPageTitleAndDescription(Throwable $exception): array
39
    {
40
        $translator = $this->getTranslator();
41
42
        if ($exception instanceof HttpException && $exception instanceof MissingRequiredAttributeException) {
0 ignored issues
show
introduced by
$exception is never a sub-type of Surfnet\StepupSelfServic...uiredAttributeException.
Loading history...
43
            $title = $translator->trans('stepup.error.missing_required_attribute.title');
44
            $description = $exception->getMessage();
45
        }
46
47
        if (isset($title) && isset($description)) {
48
            return [
49
                'title' => $title,
50
                'description' => $description,
51
            ];
52
        }
53
54
        return parent::getPageTitleAndDescription($exception);
55
    }
56
57
    public function show(Request $request, Throwable $exception): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function show()
Loading history...
58
    {
59
        $statusCode = $this->getStatusCode($exception);
60
61
        $template = '@default/bundles/TwigBundle/Exception/error.html.twig';
62
        if ($statusCode == 404) {
63
            $template = '@default/bundles/TwigBundle/Exception/error404.html.twig';
64
        }
65
66
        $response = new Response('', $statusCode);
67
68
        $timestamp = (new DateTime)->format(DateTimeInterface::ATOM);
69
        $hostname  = $request->getHost();
70
        $requestId = $this->requestId;
71
        $errorCode = Art::forException($exception);
72
        $userAgent = $request->headers->get('User-Agent');
73
        $ipAddress = $request->getClientIp();
74
75
        return $this->render(
76
            $template,
77
            [
78
                'timestamp'   => $timestamp,
79
                'hostname'    => $hostname,
80
                'request_id'  => $requestId->get(),
81
                'error_code'  => $errorCode,
82
                'user_agent'  => $userAgent,
83
                'ip_address'  => $ipAddress,
84
            ] + $this->getPageTitleAndDescription($exception),
85
            $response
86
        );
87
    }
88
}
89