Completed
Push — master ( 239c91...59afdf )
by Dmitry
02:55
created

SiteController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 121
Duplicated Lines 17.36 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 4
dl 21
loc 121
ccs 0
cts 67
cp 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A behaviors() 0 15 1
B actions() 0 24 1
A actionUnimpersonate() 0 8 2
A onAuthSuccess() 5 12 4
A actionLogin() 0 8 2
A actionLogout() 9 9 1
A actionSignup() 7 7 1
A actionImpersonate() 0 9 2
A actionHealthcheck() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * HiPanel core package.
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\controllers;
12
13
use hipanel\logic\Impersonator;
14
use hipanel\models\User;
15
use hisite\actions\RedirectAction;
16
use hisite\actions\RenderAction;
17
use Yii;
18
use yii\authclient\AuthAction;
19
use yii\base\Module;
20
use yii\filters\AccessControl;
21
22
/**
23
 * Site controller.
24
 */
25
class SiteController extends \hisite\controllers\SiteController
26
{
27
    /** @var string */
28
    protected $defaultAuthClient = 'hiam';
29
    /**
30
     * @var Impersonator
31
     */
32
    private $impersonator;
33
34
    public function __construct(string $id, Module $module, Impersonator $impersonator, array $config = [])
35
    {
36
        parent::__construct($id, $module, $config);
37
        $this->impersonator = $impersonator;
38
    }
39
40
    public function behaviors()
41
    {
42
        return array_merge(parent::behaviors(), [
43
            'loginRequired' => [
44
                'class' => AccessControl::class,
45
                'only' => ['profile'],
46
                'rules' => [
47
                    [
48
                        'allow' => true,
49
                        'roles' => ['@'],
50
                    ],
51
                ],
52
            ],
53
        ]);
54
    }
55
56
    public function actions()
57
    {
58
        return array_merge(parent::actions(), [
59
            'auth' => [
60
                'class' => AuthAction::class,
61
                'successCallback' => [$this, 'onAuthSuccess'],
62
            ],
63
            'impersonate-auth' => [
64
                'class' => AuthAction::class,
65
                'successCallback' => [$this->impersonator, 'impersonateUser'],
66
            ],
67
            'index' => [
68
                'class' => RedirectAction::class,
69
                'url'   => ['/dashboard/dashboard'],
70
            ],
71
            'profile' => [
72
                'class' => RedirectAction::class,
73
                'url' => ['@client/view', 'id' => Yii::$app->user->identity->id],
74
            ],
75
            'lockscreen' => [
76
                'class' => RenderAction::class,
77
            ],
78
        ]);
79
    }
80
81
82
    public function actionUnimpersonate()
83
    {
84
        if ($this->impersonator->isUserImpersonated()) {
85
            $this->impersonator->unimpersonateUser();
86
        }
87
88
        return $this->redirect('/');
89
    }
90
91
    public function onAuthSuccess($client)
92
    {
93
        $attributes = $client->getUserAttributes();
94
        $user = new User();
95 View Code Duplication
        foreach ($user->attributes() as $k) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
            if (isset($attributes[$k])) {
97
                $user->{$k} = $attributes[$k];
98
            }
99
        }
100
        $user->save();
101
        Yii::$app->user->login($user, Yii::$app->params['login_duration'] ?: 3600 * 24 * 30);
102
    }
103
104
    public function actionLogin()
105
    {
106
        if (!Yii::$app->user->isGuest) {
107
            return $this->redirect(['/site/index']);
108
        }
109
110
        return $this->redirect(['/site/auth', 'authclient' => $this->defaultAuthClient]);
111
    }
112
113 View Code Duplication
    public function actionLogout()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115
        Yii::$app->user->logout();
116
117
        $back = Yii::$app->request->getHostInfo();
118
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back'));
119
120
        return $this->redirect($url);
121
    }
122
123 View Code Duplication
    public function actionSignup()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $back = Yii::$app->request->getHostInfo();
126
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back'));
127
128
        return $this->redirect($url);
129
    }
130
131
    public function actionImpersonate($user_id)
132
    {
133
        if ($this->impersonator->isUserImpersonated()) {
134
            $this->impersonator->unimpersonateUser();
135
        }
136
137
        $this->impersonator->backupCurrentToken();
138
        return $this->redirect($this->impersonator->buildAuthUrl($user_id));
139
    }
140
141
    public function actionHealthcheck()
142
    {
143
        return "Up and running.";
144
    }
145
}
146