Completed
Push — master ( 2595f8...be4718 )
by Andrii
03:55
created

SiteController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 18.99 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 15
loc 79
ccs 0
cts 67
cp 0
rs 10
wmc 9
lcom 0
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 15 1
A actions() 0 20 1
A onAuthSuccess() 0 12 3
A actionLogin() 0 8 2
A actionLogout() 8 8 1
A actionSignup() 7 7 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
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace hipanel\controllers;
13
14
use hipanel\models\User;
15
use hisite\actions\RenderAction;
16
use hisite\actions\RedirectAction;
17
use Yii;
18
use yii\filters\AccessControl;
19
use yii\authclient\AuthAction;
20
use yii\web\BadRequestHttpException;
21
22
/**
23
 * Site controller.
24
 */
25
class SiteController extends \hisite\controllers\SiteController
26
{
27
    public function behaviors()
28
    {
29
        return array_merge(parent::behaviors(), [
30
            'loginRequired' => [
31
                'class' => AccessControl::class,
32
                'only' => ['profile'],
33
                'rules' => [
34
                    [
35
                        'allow' => true,
36
                        'roles' => ['@'],
37
                    ],
38
                ],
39
            ],
40
        ]);
41
    }
42
43
    public function actions()
44
    {
45
        return array_merge(parent::actions(), [
46
            'auth' => [
47
                'class' => AuthAction::class,
48
                'successCallback' => [$this, 'onAuthSuccess'],
49
            ],
50
            'index' => [
51
                'class' => RedirectAction::class,
52
                'url'   => ['/dashboard/dashboard'],
53
            ],
54
            'profile' => [
55
                'class' => RedirectAction::class,
56
                'url' => ['@client/view', 'id' => Yii::$app->user->identity->id],
57
            ],
58
            'lockscreen' => [
59
                'class' => RenderAction::class,
60
            ],
61
        ]);
62
    }
63
64
    public function onAuthSuccess($client)
65
    {
66
        $attributes = $client->getUserAttributes();
67
        $user = new User();
68
        foreach ($user->attributes() as $k) {
69
            if (isset($attributes[$k])) {
70
                $user->{$k} = $attributes[$k];
71
            }
72
        }
73
        $user->save();
74
        Yii::$app->user->login($user, 3600 * 24 * 30);
75
    }
76
77
    public function actionLogin()
78
    {
79
        if (!Yii::$app->user->isGuest) {
80
            return $this->redirect(['/site/index']);
81
        }
82
83
        return $this->redirect(['/site/auth', 'authclient' => 'hiam']);
84
    }
85
86 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...
87
    {
88
        $back = Yii::$app->request->getHostInfo();
89
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back'));
90
        Yii::$app->user->logout();
91
92
        return Yii::$app->response->redirect($url);
93
    }
94
95 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...
96
    {
97
        $back = Yii::$app->request->getHostInfo();
98
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back'));
99
100
        return Yii::$app->response->redirect($url);
101
    }
102
103
}
104