GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — develop (#47)
by
unknown
07:57 queued 02:00
created

ExceptionController::getStatusCode()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 8
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright 2014 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\StepupBundle\Controller;
20
21
use DateTime;
22
use Exception;
23
use Surfnet\StepupBundle\EventListener\RequestIdRequestResponseListener;
24
use Surfnet\StepupBundle\Exception\Art;
25
use Surfnet\StepupBundle\Exception\UserMessageException;
26
use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
29
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
30
use Symfony\Component\Security\Core\Exception\AuthenticationException;
31
use Symfony\Component\Translation\TranslatorInterface;
32
33
class ExceptionController extends FrameworkController
34
{
35
    public function showAction(Exception $exception)
36
    {
37
        $statusCode = $this->getStatusCode($exception);
38
39
        if ($statusCode == 404) {
40
            $template = 'SurfnetStepupBundle:Exception:error404.html.twig';
41
        } else {
42
            $template = 'SurfnetStepupBundle:Exception:error.html.twig';
43
        }
44
45
        $request = $this->getRequest();
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Bundle\Framework...ontroller::getRequest() has been deprecated with message: since version 2.4, to be removed in 3.0. Ask Symfony to inject the Request object into your controller method instead by type hinting it in the method's signature.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
46
        $response = new Response('', $statusCode);
47
48
        $timestamp = (new DateTime)->format(DateTime::ISO8601);
49
        $hostname  = $request->getHost();
50
        $requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-');
51
        $errorCode = Art::forException($exception);
52
        $userAgent = $request->headers->get('User-Agent');
53
        $ipAddress = $request->getClientIp();
54
55
        return $this->render(
56
            $template,
57
            [
58
                'timestamp'   => $timestamp,
59
                'hostname'    => $hostname,
60
                'request_id'  => $requestId,
61
                'error_code'  => $errorCode,
62
                'user_agent'  => $userAgent,
63
                'ip_address'  => $ipAddress,
64
            ] + $this->getPageTitleAndDescription($exception),
65
            $response
66
        );
67
    }
68
69
    /**
70
     * @param Exception $exception
71
     * @return int HTTP status code
72
     */
73
    private function getStatusCode(Exception $exception)
74
    {
75
        if ($exception instanceof AuthenticationException) {
76
            return Response::HTTP_UNAUTHORIZED;
77
        }
78
79
        if ($exception instanceof AccessDeniedException) {
80
            return Response::HTTP_FORBIDDEN;
81
        }
82
83
        if ($exception instanceof HttpExceptionInterface) {
84
            return $exception->getStatusCode();
85
        }
86
87
        // Unknown exceptions are server errors!
88
        return 500;
89
    }
90
91
    /**
92
     * @param Exception $exception
93
     * @return array View parameters 'title' and 'description'
94
     */
95
    private function getPageTitleAndDescription(Exception $exception)
96
    {
97
        $translator = $this->getTranslator();
98
        $parameters = [];
99
100
        if ($exception instanceof UserErrorProviderInterface) {
0 ignored issues
show
Bug introduced by
The class Surfnet\StepupBundle\Con...rErrorProviderInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
101
            $parameters['title'] = $translator->trans(
102
                $exception->getErrorPageTitleMessageId()
103
            );
104
105
            $parameters['description'] = $translator->trans(
106
                $exception->getErrorPageDescriptionMessageId(),
107
                $exception->getErrorPageDescriptionArguments()
108
            );
109
        } elseif ($exception instanceof AuthenticationException) {
110
            $parameters['title'] = $translator->trans('stepup.error.authentication_error_title');
111
            $parameters['description'] = $translator->trans('stepup.error.authentication_error_description');
112
        } else {
113
            $parameters['title'] = $translator->trans('stepup.error.generic_error_title');
114
            $parameters['description'] = $translator->trans('stepup.error.generic_error_description');
115
        }
116
117
        return $parameters;
118
    }
119
120
    /**
121
     * @return TranslatorInterface
122
     */
123
    private function getTranslator()
124
    {
125
        return $this->get('translator');
126
    }
127
}
128