Completed
Push — master ( f5a3d9...b343f1 )
by Dmitry
13:14
created

AuthAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * OAuth2 client for yii2 to login with HIAM server
5
 *
6
 * @link      https://github.com/hiqdev/yii2-hiam-authclient
7
 * @package   yii2-hiam-authclient
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hiam\authclient;
13
14
use Exception;
15
use Psr\Log\LoggerInterface;
16
use Yii;
17
use yii\helpers\Html;
18
19
class AuthAction extends \yii\authclient\AuthAction
20
{
21
    private LoggerInterface $log;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
22
23
    public function __construct($id, $controller, LoggerInterface $log, $config = [])
24
    {
25
        parent::__construct($id, $controller, $config);
26
        $this->log = $log;
27
    }
28
29
    public function run()
30
    {
31
        try {
32
            return parent::run();
33
        } catch (Exception $e) {
34
            if ($e->getMessage() !== 'Invalid auth state parameter.') {
35
                Yii::$app->getResponse()->setStatusCode(500);
36
                return Html::encode($e->getMessage());
37
            }
38
39
            return $this->handleInvalidAuthState($e);
40
        }
41
    }
42
43
    private function handleInvalidAuthState(Exception $exception)
44
    {
45
        if (!empty(Yii::$app->user->id)) {
46
            return $this->redirectSuccess();
47
        }
48
49
        $errorMessage = sprintf('Failed to exchange the auth code to the access token: %s', $exception->getMessage());
50
        $this->log->error($errorMessage, ['exception' => $exception]);
51
52
        if (YII_ENV !== 'prod') {
53
            echo $errorMessage;
54
        }
55
56
        return '';
57
    }
58
}
59