Completed
Push — master ( c8c676...fccfb6 )
by vistart
03:23
created

UserController   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 19
c 1
b 0
f 1
lcom 1
cbo 5
dl 0
loc 110
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A checkUserClass() 0 11 3
B getUser() 0 13 6
A actionRegister() 0 16 3
A actionDeregister() 0 9 2
A actionShow() 0 7 2
A actionStat() 0 16 3
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user\console\controllers;
14
15
use rhosocial\user\User;
16
use rhosocial\user\Profile;
17
use yii\console\Controller;
18
use yii\console\Exception;
19
use Yii;
20
21
/**
22
 * The simple operations associated with User.
23
 *
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class UserController extends Controller
28
{
29
    public $userClass;
30
    
31
    public $defaultAction = 'show';
32
    
33
    protected function checkUserClass()
34
    {
35
        $userClass = $this->userClass;
36
        if (!class_exists($userClass)) {
37
            throw new Exception('User Class Invalid.');
38
        }
39
        if (!((new $userClass()) instanceof User)) {
40
            throw new Exception('User Class(' . $userClass . ') does not inherited from `\rhosocial\user\User`.');
41
        }
42
        return $userClass;
43
    }
44
    
45
    /**
46
     * Get user from database.
47
     * @param User|string|integer $user
48
     * @return User
49
     */
50
    protected function getUser($user)
51
    {
52
        $userClass = $this->checkUserClass();
53
        if (is_numeric($user)) {
54
            $user = $userClass::find()->id($user)->one();
55
        } elseif (is_string($user) && strlen($user)) {
56
            $user = $userClass::find()->guid($user)->one();
57
        }
58
        if (!$user || $user->getIsNewRecord()) {
59
            throw new Exception('User Not Registered.');
60
        }
61
        return $user;
62
    }
63
    
64
    /**
65
     * Register new User.
66
     * @param string $password Password.
67
     * @param string $nickname If profile contains this property, this parameter is required.
68
     * @param string $first_name If profile contains this property, this parameter is required.
69
     * @param string $last_name If profile contains this propery, this parameter is required.
70
     */
71
    public function actionRegister($password, $nickname = null, $first_name = null, $last_name = null)
72
    {
73
        $userClass = $this->checkUserClass();
74
        
75
        $user = new $userClass(['password' => $password]);
76
        /* @var $user User */
77
        $profile = $user->createProfile(['nickname' => $nickname, 'first_name' => $first_name, 'last_name' => $last_name]);
78
        /* @var $profile Profile */
79
        try {
80
            is_null($profile) ? $user->register(): $user->register([$profile]);
81
        } catch (\Exception $ex) {
82
            throw new Exception($ex->getMessage());
83
        }
84
        echo "User Registered:\n";
85
        return $this->actionShow($user);
86
    }
87
    
88
    /**
89
     * Deregister user.
90
     * @param User|string|integer $user The user to be deregistered.
91
     * @return boolean
92
     */
93
    public function actionDeregister($user)
94
    {
95
        $user = $this->getUser($user);
96
        if ($user->deregister()) {
97
            echo "User (" . $user->getID() . ") Deregistered.\n";
98
            return true;
99
        }
100
        return false;
101
    }
102
    
103
    /**
104
     * Show User Information.
105
     * @param User|string|integer $user
106
     */
107
    public function actionShow($user, $guid = false, $pass_hash = false, $access_token = false, $auth_key = false)
0 ignored issues
show
Unused Code introduced by
The parameter $guid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $pass_hash is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $access_token is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $auth_key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
108
    {
109
        $user = $this->getUser($user);
110
        echo Yii::t('app', 'User') . " (" . $user->getID() . "), " . Yii::t('app', 'registered at') . " (" . $user->getCreatedAt() . ")"
111
                . ($user->getCreatedAt() == $user->getUpdatedAt() ? "" : ", " . Yii::t('app', 'last updated at') . " (" . $user->getUpdatedAt() . ")") .".\n";
112
        return true;
113
    }
114
    
115
    /**
116
     * Show statistics.
117
     * @param User|string|integer $user
118
     * @return boolean
119
     */
120
    public function actionStat($user = null)
121
    {
122
        if ($user === null) {
123
            $count = User::find()->count();
124
            echo "Total number of user(s): " . $count . "\n";
125
            if ($count == 0) {
126
                return true;
127
            }
128
            $last = User::find()->orderByCreatedAt(SORT_DESC)->one();
129
            /* @var $last User */
130
            echo "Latest user (" . $last->getID() . ") registered at " . $last->getCreatedAt() . "\n";
131
            return true;
132
        }
133
        $user = $this->getUser($user);
0 ignored issues
show
Unused Code introduced by
$user is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
134
        return true;
135
    }
136
}