ExceptionController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageTitleAndDescription() 0 17 4
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
32
final class ExceptionController extends BaseExceptionController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class ExceptionController
Loading history...
33
{
34
    /**
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...
35
     * @return array View parameters 'title' and 'description'
36
     */
37
    protected function getPageTitleAndDescription(Throwable $exception): array
38
    {
39
        $translator = $this->getTranslator();
40
41
        if ($exception instanceof MissingRequiredAttributeException) {
42
            $title = $translator->trans('stepup.error.missing_required_attribute.title');
43
            $description = $exception->getMessage();
44
        }
45
46
        if (isset($title) && isset($description)) {
47
            return [
48
                'title' => $title,
49
                'description' => $description,
50
            ];
51
        }
52
53
        return parent::getPageTitleAndDescription($exception);
54
    }
55
56
    public function show(Request $request, Throwable $exception): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function show()
Loading history...
57
    {
58
        $statusCode = $this->getStatusCode($exception);
59
60
        $template = '@default/bundles/TwigBundle/Exception/error.html.twig';
61
        if ($statusCode == 404) {
62
            $template = '@default/bundles/TwigBundle/Exception/error404.html.twig';
63
        }
64
65
        $response = new Response('', $statusCode);
66
67
        $timestamp = (new DateTime)->format(DateTimeInterface::ATOM);
68
        $hostname  = $request->getHost();
69
        $requestId = $this->requestId;
70
        $errorCode = Art::forException($exception);
71
        $userAgent = $request->headers->get('User-Agent');
72
        $ipAddress = $request->getClientIp();
73
74
        return $this->render(
75
            $template,
76
            [
77
                'timestamp'   => $timestamp,
78
                'hostname'    => $hostname,
79
                'request_id'  => $requestId->get(),
80
                'error_code'  => $errorCode,
81
                'user_agent'  => $userAgent,
82
                'ip_address'  => $ipAddress,
83
            ] + $this->getPageTitleAndDescription($exception),
84
            $response
85
        );
86
    }
87
}
88