Completed
Push — master ( 42fdff...4bba11 )
by vistart
02:58
created

UserController::getUser()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 6
nop 1
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
    protected function checkUserClass()
32
    {
33
        $userClass = $this->userClass;
34
        if (!class_exists($userClass)) {
35
            throw new Exception('User Class Invalid.');
36
        }
37
        if (!((new $userClass()) instanceof User)) {
38
            throw new Exception('User Class(' . $userClass . ') does not inherited from `\rhosocial\user\User`.');
39
        }
40
        return $userClass;
41
    }
42
    
43
    /**
44
     * Get user from database.
45
     * @param User|string|integer $user
46
     * @return User
47
     */
48
    protected function getUser($user)
49
    {
50
        $userClass = $this->checkUserClass();
51
        if (is_numeric($user)) {
52
            $user = $userClass::find()->id($user)->one();
53
        } elseif (is_string($user) && strlen($user)) {
54
            $user = $userClass::find()->guid($user)->one();
55
        }
56
        if (!$user || $user->getIsNewRecord()) {
57
            throw new Exception('User Not Registered.');
58
        }
59
        return $user;
60
    }
61
    
62
    /**
63
     * Register new User.
64
     * @param string $password Password.
65
     * @param string $nickname If profile contains this property, this parameter is required.
66
     * @param string $first_name If profile contains this property, this parameter is required.
67
     * @param string $last_name If profile contains this propery, this parameter is required.
68
     */
69
    public function actionRegister($password, $nickname = null, $first_name = null, $last_name = null)
70
    {
71
        $userClass = $this->checkUserClass();
72
        
73
        $user = new $userClass(['password' => $password]);
74
        /* @var $user User */
75
        $profile = $user->createProfile(['nickname' => $nickname, 'first_name' => $first_name, 'last_name' => $last_name]);
76
        /* @var $profile Profile */
77
        try {
78
            is_null($profile) ? $user->register(): $user->register([$profile]);
79
        } catch (\Exception $ex) {
80
            throw new Exception($ex->getMessage());
81
        }
82
        echo "User Registered:\n";
83
        return $this->actionShow($user);
84
    }
85
    
86
    /**
87
     * Deregister user.
88
     * @param User|string|integer $user The user to be deregistered.
89
     * @return boolean
90
     */
91
    public function actionDeregister($user)
92
    {
93
        $user = $this->getUser($user);
94
        if ($user->deregister()) {
95
            echo "User (" . $user->getID() . ") Deregistered.\n";
96
            return true;
97
        }
98
        return false;
99
    }
100
    
101
    /**
102
     * Show User Information.
103
     * @param User|string|integer $user
104
     */
105
    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...
106
    {
107
        $user = $this->getUser($user);
108
        echo Yii::t('app', 'User') . " (" . $user->getID() . "), " . Yii::t('app', 'registered at') . " (" . $user->getCreatedAt() . ")"
109
                . ($user->getCreatedAt() == $user->getUpdatedAt() ? "" : ", " . Yii::t('app', 'last updated at') . " (" . $user->getUpdatedAt() . ")") .".\n";
110
        return true;
111
    }
112
}