DefaultController::findUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace inblank\activeuser\commands;
4
5
use inblank\activeuser\components\ConsoleController;
6
use inblank\activeuser\traits\CommonTrait;
7
use Yii;
8
use yii\helpers\Console;
9
10
/**
11
 * Managing user accounts
12
 * @package inblank\activeuser\commands
13
 */
14
class DefaultController extends ConsoleController
15
{
16
    use CommonTrait;
17
18
    /** @var bool whether to send email about action. If not set use module's settings */
19
    public $sendEmail = false;
20
21
    /** @inheritdoc */
22
    public function options($actionID)
23
    {
24
        return ['sendEmail'];
25
    }
26
27
    /**
28
     * Create a new user
29
     * @param string $email user email
30
     * @param string $name user name
31
     * @param string|null $password optional user password. If not set will be generated automatically
32
     * @throws \yii\base\InvalidConfigException
33
     */
34
    public function actionCreate($email = null, $name = null, $password = null)
35
    {
36
        /** @var \inblank\activeuser\models\User $user */
37
        $user = Yii::createObject(self::di('User'));
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
        $this->getModule()->registrationFields = ['name'];
39
        $user->setAttributes(compact('email', 'name', 'password'), false);
40
        if (!$user->validate(['email', 'name'])) {
41
            $this->showUsage('email,name', 'password', $user->getErrors());
42
        }
43
        $nameString = $this->ansiFormat($name, Console::FG_GREEN);
44
        $emailString = $this->ansiFormat($email, Console::FG_GREEN);
45
        $this->confirmAction(Yii::t("activeuser_backend", "Create new user {nameString} with email {emailString}?", [
46
            'emailString' => $emailString,
47
            'nameString' => $nameString,
48
        ]));
49
        // create user
50
        if (!$user->create($this->sendEmail)) {
51
            $this->showErrors($user->getErrors());
52
        }
53
        $passwordString = $this->ansiFormat($user->password, Console::FG_GREEN);
54
        $info = Yii::t("activeuser_backend", 'Created user');
55
        $this->stdout($info . PHP_EOL, Console::BOLD);
56
        $this->stdout(str_pad('', mb_strlen($info), '-') . PHP_EOL, Console::BOLD);
57
        $this->stdout(Yii::t('activeuser_general', 'Name') . ': ' . $nameString . PHP_EOL);
58
        $this->stdout(Yii::t('activeuser_general', 'Email') . ': ' . $emailString . PHP_EOL);
59
        $this->stdout(Yii::t('activeuser_general', 'Password') . ': ' . $passwordString . PHP_EOL);
60
    }
61
62
    /**
63
     * Block user
64
     * @param string $email user email to block
65
     */
66 View Code Duplication
    public function actionBlock($email = null)
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...
67
    {
68
        if (empty($email)) {
69
            $this->showUsage('email');
70
        }
71
        $user = $this->findUser($email);
72
        $nameString = $this->ansiFormat($user->name, Console::FG_GREEN);
73
        $emailString = $this->ansiFormat($user->email, Console::FG_GREEN);
74
        if (!$user->isActive()) {
75
            $this->showErrors([
76
                'error' => [
77
                    Yii::t('activeuser_backend', "Can't block user {name} with email {email}", [
78
                        'name' => $nameString,
79
                        'email' => $emailString,
80
                    ])
81
                ]
82
            ]);
83
        }
84
        $this->confirmAction(Yii::t("activeuser_backend", "Block user {name} with email {email}?", [
85
            'name' => $nameString,
86
            'email' => $emailString,
87
        ]));
88
        $user->block($this->sendEmail);
89
        $this->stdout(
90
            Yii::t('activeuser_backend', 'User {name} with email {email} was blocked', [
91
                'name' => $nameString,
92
                'email' => $emailString,
93
            ]) . PHP_EOL
94
        );
95
    }
96
97
    /**
98
     * Unblock user
99
     * @param string $email user email to unblock
100
     */
101 View Code Duplication
    public function actionUnblock($email = null)
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...
102
    {
103
        if (empty($email)) {
104
            $this->showUsage('email');
105
        }
106
        $user = $this->findUser($email);
107
        $nameString = $this->ansiFormat($user->name, Console::FG_GREEN);
108
        $emailString = $this->ansiFormat($user->email, Console::FG_GREEN);
109
        if (!$user->isBlocked()) {
110
            $this->showErrors([
111
                'error' => [
112
                    Yii::t('activeuser_backend', 'User {name} with email {email} not blocked', [
113
                        'name' => $nameString,
114
                        'email' => $emailString,
115
                    ])
116
                ]
117
            ]);
118
        }
119
        $this->confirmAction(Yii::t("activeuser_backend", 'Unblock user {name} with email {email}?', [
120
            'name' => $nameString,
121
            'email' => $emailString,
122
        ]));
123
        $user->unblock($this->sendEmail);
124
        $this->stdout(
125
            Yii::t('activeuser_backend', 'User {name} with email {email} was unblocked', [
126
                'name' => $nameString,
127
                'email' => $emailString,
128
            ]) . PHP_EOL
129
        );
130
    }
131
132
    /**
133
     * Change user password
134
     * @param string $email user email
135
     * @param string|null $password new password. If not set will be generated automatically
136
     */
137
    public function actionPassword($email = null, $password = null)
138
    {
139
        if (empty($email)) {
140
            $this->showUsage('email', 'password');
141
        }
142
        $user = $this->findUser($email);
143
        $nameString = $this->ansiFormat($user->name, Console::FG_GREEN);
144
        $emailString = $this->ansiFormat($user->email, Console::FG_GREEN);
145
        $this->confirmAction(Yii::t("activeuser_backend", 'Change password user {name} with email {email}?', [
146
            'name' => $nameString,
147
            'email' => $emailString,
148
        ]));
149
        $user->password = $password;
150
        $user->newPassword($this->sendEmail);
151
        $passwordString = $this->ansiFormat($user->password, Console::BOLD);
152
        $info = Yii::t("activeuser_backend", 'New user data');
153
        $this->stdout($info . PHP_EOL, Console::BOLD);
154
        $this->stdout(str_pad('', mb_strlen($info), '-') . PHP_EOL);
155
        $this->stdout(Yii::t('activeuser_general', 'Email') . ': ' . $emailString . PHP_EOL);
156
        $this->stdout(Yii::t('activeuser_general', 'Password') . ': ' . $passwordString . PHP_EOL);
157
    }
158
159
    /**
160
     * Find user
161
     * @param string $email user email for search
162
     * @return \inblank\activeuser\models\User
163
     * @throws \yii\base\ExitException
164
     * @throws \yii\base\InvalidConfigException
165
     */
166
    protected function findUser($email)
167
    {
168
        $user = Yii::createObject(self::di('User'))->findOne(['email' => $email]);
0 ignored issues
show
Documentation introduced by
self::di('User') is of type *, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
169
        if ($user !== null) {
170
            return $user;
171
        }
172
        return $this->showErrors([
173
            'user' => [
174
                Yii::t('activeuser_backend', 'User with email {email} not found', [
175
                    'email' => $this->ansiFormat($email, Console::BOLD)
176
                ]),
177
            ]
178
        ]);
179
    }
180
181
}
182