Passed
Push — master ( 1437aa...6e82fd )
by Rutger
13:37
created

Oauth2BaseServerAction::processException()   C

Complexity

Conditions 13
Paths 110

Size

Total Lines 64
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 39
CRAP Score 13

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 41
c 1
b 0
f 0
dl 0
loc 64
ccs 39
cts 39
cp 1
rs 6.5333
cc 13
nc 110
nop 2
crap 13

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\controllers\web\server\base;
4
5
use League\OAuth2\Server\Exception\OAuthServerException;
6
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebAction;
7
use rhertogh\Yii2Oauth2Server\controllers\web\base\Oauth2BaseWebController;
8
use rhertogh\Yii2Oauth2Server\helpers\UrlHelper;
9
use Yii;
10
use yii\base\ErrorException;
11
use yii\base\Exception;
12
use yii\base\UserException;
13
use yii\web\HttpException;
14
use yii\web\Response;
15
16
/**
17
 * @property Oauth2BaseWebController $controller
18
 */
19
abstract class Oauth2BaseServerAction extends Oauth2BaseWebAction
20
{
21
    /**
22
     * @param \Exception $exception
23
     * @return Response
24
     * @see https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
25
     */
26 6
    protected function processException($exception, $logCategory)
27
    {
28 6
        $module = $this->controller->module;
29
30 6
        if (Yii::$app->has('response')) {
31 5
            $response = Yii::$app->getResponse();
32
            // reset parameters of response to avoid interference with partially created response data
33
            // in case the error occurred while sending the response.
34 5
            $response->isSent = false;
35 5
            $response->stream = null;
36 5
            $response->data = null;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
37 5
            $response->content = null;
38
        } else {
39 1
            $response = new Response();
40
        }
41
42 6
        if ($exception instanceof OAuthServerException) {
43 2
            if ($exception->hasRedirect()) {
44 1
                return $this->controller->redirect(UrlHelper::addQueryParams($exception->getRedirectUri(), [
45 1
                    'error' => $exception->getErrorType()
46 1
                ]));
47
            }
48
49 1
            $response->setStatusCode($exception->getHttpStatusCode());
50
51 1
            $error = $exception->getErrorType();
52 1
            $hint = $exception->getHint();
53 1
            $description = $exception->getMessage() . ($hint ? ' ' . $hint : '');
54
        } else {
55 4
            $response->setStatusCodeByException($exception);
56
57 4
            $error = ($exception instanceof Exception || $exception instanceof ErrorException)
58 4
                ? $exception->getName()
59 4
                : 'Exception';
60
61 4
            $displayNonHttpExceptionMessages = $module->displayConfidentialExceptionMessages !== null
62 2
                ? $module->displayConfidentialExceptionMessages
63 4
                : YII_DEBUG;
64
65
            if (
66 4
                !$displayNonHttpExceptionMessages
67 4
                && !$exception instanceof UserException
68 4
                && !$exception instanceof HttpException
69
            ) {
70 1
                $description = Yii::t('yii', 'An internal server error occurred.');
71
            } else {
72 3
                $description = (string)$exception;
73
            }
74
        }
75
76 5
        $response->data = [
77 5
            'error' => $error,
78 5
            'error_description' => $description,
79 5
        ];
80
81 5
        if ($response->getIsClientError()) {
82 1
            if ($module->httpClientErrorsLogLevel) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $module->httpClientErrorsLogLevel of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
83 1
                Yii::getLogger()->log((string)$exception, $module->httpClientErrorsLogLevel, $logCategory);
84
            }
85
        } else {
86 4
            Yii::error((string)$exception, $logCategory);
87
        }
88
89 5
        return $response;
90
    }
91
}
92