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.

ExceptionController::getStatusCode()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 9.0111
cc 6
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
/**
41
 * @package Surfnet\StepupBundle\Controller
42
 *
43
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects) Contains extensive mapping for exceptions
44
 */
45
class ExceptionController extends FrameworkController
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
46
{
47
    public function showAction(Request $request, Exception $exception)
48
    {
49
        $statusCode = $this->getStatusCode($exception);
50
51
        if ($statusCode == 404) {
52
            $template = 'SurfnetStepupBundle:Exception:error404.html.twig';
53
        } else {
54
            $template = 'SurfnetStepupBundle:Exception:error.html.twig';
55
        }
56
57
        $response = new Response('', $statusCode);
58
59
        $timestamp = (new DateTime)->format(DateTime::ISO8601);
60
        $hostname  = $request->getHost();
61
        $requestId = $this->get('surfnet_stepup.request.request_id');
62
        $errorCode = Art::forException($exception);
63
        $userAgent = $request->headers->get('User-Agent');
64
        $ipAddress = $request->getClientIp();
65
66
        return $this->render(
67
            $template,
68
            [
69
                'timestamp'   => $timestamp,
70
                'hostname'    => $hostname,
71
                'request_id'  => $requestId->get(),
72
                'error_code'  => $errorCode,
73
                'user_agent'  => $userAgent,
74
                'ip_address'  => $ipAddress,
75
            ] + $this->getPageTitleAndDescription($exception),
76
            $response
77
        );
78
    }
79
80
    /**
81
     * @param Exception $exception
82
     * @return int HTTP status code
83
     */
84
    protected function getStatusCode(Exception $exception)
85
    {
86
        if ($exception instanceof AuthenticationException ||
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Securi...AuthenticationException 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...
87
            $exception instanceof InvalidResponseException) {
88
            return Response::HTTP_UNAUTHORIZED;
89
        }
90
91
        if ($exception instanceof AccessDeniedException ||
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Securi...n\AccessDeniedException 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...
92
            $exception instanceof PreconditionNotMetException) {
93
            return Response::HTTP_FORBIDDEN;
94
        }
95
96
        if ($exception instanceof HttpExceptionInterface) {
97
            return $exception->getStatusCode();
98
        }
99
100
        // Unknown exceptions are server errors!
101
        return 500;
102
    }
103
104
    /**
105
     * @param Exception $exception
106
     * @return array View parameters 'title' and 'description'
107
     */
108
    protected function getPageTitleAndDescription(Exception $exception)
109
    {
110
        $translator = $this->getTranslator();
111
112
        if ($exception instanceof SignatureValidationFailedException) {
113
            $title = $translator->trans('stepup.error.signature_validation_failed.title');
114
            $description = $translator->trans('stepup.error.signature_validation_failed.description');
115
116
        } elseif ($exception instanceof UnsignedRequestException) {
117
            $title = $translator->trans('stepup.error.unsigned_request.title');
118
            $description = $translator->trans('stepup.error.unsigned_request.description');
119
120
        } elseif ($exception instanceof UnsupportedSignatureException) {
121
            $title = $translator->trans('stepup.error.unsupported_signature.title');
122
            $description = $translator->trans('stepup.error.unsupported_signature.description');
123
124
        } elseif ($exception instanceof UnknownServiceProviderException) {
125
            $title = $translator->trans('stepup.error.unknown_service_provider.title');
126
            $description = $exception->getMessage();
127
128
        } elseif ($exception instanceof AuthnFailedSamlResponseException) {
129
            $title = $translator->trans('stepup.error.authn_failed.title');
130
            $description = $translator->trans('stepup.error.authn_failed.description');
131
132
        } elseif ($exception instanceof PreconditionNotMetException) {
133
            $title = $translator->trans('stepup.error.precondition_not_met.title');
134
            $description = $translator->trans('stepup.error.precondition_not_met.description');
135
136
        } elseif ($exception instanceof InvalidResponseException) {
137
            $title = $translator->trans('stepup.error.authentication_error.title');
138
            $description = $translator->trans('stepup.error.authentication_error.description');
139
        } elseif ($exception instanceof AuthenticationException) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Securi...AuthenticationException 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...
140
            $title = $translator->trans('stepup.error.authentication_error.title');
141
            $description = $translator->trans('stepup.error.authentication_error.description');
142
        } else {
143
            $title = $translator->trans('stepup.error.generic_error.title');
144
            $description = $translator->trans('stepup.error.generic_error.description');
145
        }
146
147
        return [
148
            'title' => $title,
149
            'description' => $description,
150
        ];
151
    }
152
153
    /**
154
     * @return TranslatorInterface
155
     */
156
    protected function getTranslator()
157
    {
158
        return $this->get('translator');
159
    }
160
}
161