Completed
Push — master ( 8e87c2...78ddf5 )
by Andrii
08:54
created

SiteController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 70
Duplicated Lines 21.43 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
B actions() 0 24 2
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\authclient\AuthAction;
19
use yii\web\BadRequestHttpException;
20
21
/**
22
 * Site controller.
23
 */
24
class SiteController extends \hisite\controllers\SiteController
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function actions()
30
    {
31
        return array_merge(parent::actions(), [
32
            'auth' => [
33
                'class' => AuthAction::class,
34
                'successCallback' => [$this, 'onAuthSuccess'],
35
            ],
36
            'index' => [
37
                'class' => RedirectAction::class,
38
                'url'   => ['/dashboard/dashboard'],
39
            ],
40
            'profile' => [
41
                'class' => RedirectAction::class,
42
                'url'   => function () {
43
                    $user = Yii::$app->user;
44
45
                    return $user->isGuest ? ['/site/login'] : ['@client/view', 'id' => $user->identity->id];
46
                },
47
            ],
48
            'lockscreen' => [
49
                'class' => RenderAction::class,
50
            ],
51
        ]);
52
    }
53
54
    public function onAuthSuccess($client)
55
    {
56
        $attributes = $client->getUserAttributes();
57
        $user = new User();
58
        foreach ($user->attributes() as $k) {
59
            if (isset($attributes[$k])) {
60
                $user->{$k} = $attributes[$k];
61
            }
62
        }
63
        $user->save();
64
        Yii::$app->user->login($user, 3600 * 24 * 30);
65
    }
66
67
    public function actionLogin()
68
    {
69
        if (!Yii::$app->user->isGuest) {
70
            return $this->redirect(['/site/index']);
71
        }
72
73
        return $this->redirect(['/site/auth', 'authclient' => 'hiam']);
74
    }
75
76 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...
77
    {
78
        $back = Yii::$app->request->getHostInfo();
79
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/logout', compact('back'));
80
        Yii::$app->user->logout();
81
82
        return Yii::$app->response->redirect($url);
83
    }
84
85 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...
86
    {
87
        $back = Yii::$app->request->getHostInfo();
88
        $url = Yii::$app->authClientCollection->getClient()->buildUrl('site/signup', compact('back'));
89
90
        return Yii::$app->response->redirect($url);
91
    }
92
93
}
94