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:08 queued 05:30
created

ExceptionController::getStatusCode()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 6
eloc 10
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 SAML2\Response\Exception\InvalidResponseException;
24
use SAML2\Response\Exception\PreconditionNotMetException;
25
use Surfnet\SamlBundle\Http\Exception\AuthnFailedSamlResponseException;
26
use Surfnet\SamlBundle\Http\Exception\SignatureValidationFailedException;
27
use Surfnet\SamlBundle\Http\Exception\UnknownServiceProviderException;
28
use Surfnet\SamlBundle\Http\Exception\UnsignedRequestException;
29
use Surfnet\SamlBundle\Http\Exception\UnsupportedSignatureException;
30
use Surfnet\StepupBundle\EventListener\RequestIdRequestResponseListener;
31
use Surfnet\StepupBundle\Exception\Art;
32
use Symfony\Bundle\FrameworkBundle\Controller\Controller as FrameworkController;
33
use Symfony\Component\HttpFoundation\Request;
34
use Symfony\Component\HttpFoundation\Response;
35
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
36
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
37
use Symfony\Component\Security\Core\Exception\AuthenticationException;
38
use Symfony\Component\Translation\TranslatorInterface;
39
40
class ExceptionController extends FrameworkController
41
{
42
    public function showAction(Request $request, Exception $exception)
43
    {
44
        $statusCode = $this->getStatusCode($exception);
45
46
        if ($statusCode == 404) {
47
            $template = 'SurfnetStepupBundle:Exception:error404.html.twig';
48
        } else {
49
            $template = 'SurfnetStepupBundle:Exception:error.html.twig';
50
        }
51
52
        $response = new Response('', $statusCode);
53
54
        $timestamp = (new DateTime)->format(DateTime::ISO8601);
55
        $hostname  = $request->getHost();
56
        $requestId = $request->headers->get(RequestIdRequestResponseListener::HEADER_NAME, '-');
57
        $errorCode = Art::forException($exception);
58
        $userAgent = $request->headers->get('User-Agent');
59
        $ipAddress = $request->getClientIp();
60
61
        return $this->render(
62
            $template,
63
            [
64
                'timestamp'   => $timestamp,
65
                'hostname'    => $hostname,
66
                'request_id'  => $requestId,
67
                'error_code'  => $errorCode,
68
                'user_agent'  => $userAgent,
69
                'ip_address'  => $ipAddress,
70
            ] + $this->getPageTitleAndDescription($exception),
71
            $response
72
        );
73
    }
74
75
    /**
76
     * @param Exception $exception
77
     * @return int HTTP status code
78
     */
79
    protected function getStatusCode(Exception $exception)
80
    {
81
        if ($exception instanceof AuthenticationException ||
82
            $exception instanceof InvalidResponseException) {
83
            return Response::HTTP_UNAUTHORIZED;
84
        }
85
86
        if ($exception instanceof AccessDeniedException ||
87
            $exception instanceof PreconditionNotMetException) {
88
            return Response::HTTP_FORBIDDEN;
89
        }
90
91
        if ($exception instanceof HttpExceptionInterface) {
92
            return $exception->getStatusCode();
93
        }
94
95
        // Unknown exceptions are server errors!
96
        return 500;
97
    }
98
99
    /**
100
     * @param Exception $exception
101
     * @return array View parameters 'title' and 'description'
102
     */
103
    protected function getPageTitleAndDescription(Exception $exception)
104
    {
105
        $translator = $this->getTranslator();
106
        $parameters = [];
0 ignored issues
show
Unused Code introduced by
$parameters is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
107
108
        if ($exception instanceof SignatureValidationFailedException) {
109
            $title = $translator->trans('stepup.error.signature_validation_failed.title');
110
            $description = $translator->trans('stepup.error.signature_validation_failed.description');
111
112
        } elseif ($exception instanceof UnsignedRequestException) {
113
            $title = $translator->trans('stepup.error.unsigned_request.title');
114
            $description = $translator->trans('stepup.error.unsigned_request.description');
115
116
        } elseif ($exception instanceof UnsupportedSignatureException) {
117
            $title = $translator->trans('stepup.error.unsupported_signature.title');
118
            $description = $translator->trans('stepup.error.unsupported_signature.description');
119
120
        } elseif ($exception instanceof UnknownServiceProviderException) {
121
            $title = $translator->trans('stepup.error.unknown_service_provider.title');
122
            $description = $translator->trans('stepup.error.unknown_service_provider.description');
123
124
        } elseif ($exception instanceof AuthnFailedSamlResponseException) {
125
            $title = $translator->trans('stepup.error.authn_failed.title');
126
            $description = $translator->trans('stepup.error.authn_failed.description');
127
128
        } elseif ($exception instanceof PreconditionNotMetException) {
129
            $title = $translator->trans('stepup.error.precondition_not_met.title');
130
            $description = $translator->trans('stepup.error.precondition_not_met.description');
131
132
        } elseif ($exception instanceof InvalidResponseException) {
133
            $title = $translator->trans('stepup.error.authentication_error.title');
134
            $description = $translator->trans('stepup.error.authentication_error.description');
135
        } elseif ($exception instanceof AuthenticationException) {
136
            $title = $translator->trans('stepup.error.authentication_error.title');
137
            $description = $translator->trans('stepup.error.authentication_error.description');
138
        } else {
139
            $title = $translator->trans('stepup.error.generic_error.title');
140
            $description = $translator->trans('stepup.error.generic_error.description');
141
        }
142
143
        return [
144
            'title' => $title,
145
            'description' => $description,
146
        ];
147
    }
148
149
    /**
150
     * @return TranslatorInterface
151
     */
152
    protected function getTranslator()
153
    {
154
        return $this->get('translator');
155
    }
156
}
157