CreateController::actionIndex()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 22
cp 0
rs 9.2088
c 0
b 0
f 0
cc 5
nc 5
nop 4
crap 30
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Command;
13
14
use Da\User\Factory\MailFactory;
15
use Da\User\Model\User;
16
use Da\User\Service\UserCreateService;
17
use Da\User\Traits\ContainerAwareTrait;
18
use Yii;
19
use yii\console\Controller;
20
use yii\helpers\Console;
21
22
class CreateController extends Controller
23
{
24
    use ContainerAwareTrait;
25
26
    /**
27
     * This command creates a new user account. If no password is not set, an 8-char password will be generated. After
28
     * saving user to database, this command uses mailer component to send credentials (username and password) to user
29
     * via email. A role can be also assigned but it must exists previously on the database.
30
     *
31
     * @param string      $email    Email
32
     * @param string      $username Username
33
     * @param string|null $password The password. If null it will be generated automatically
34
     * @param string|null $role     Role to assign (must already exist)
35
     *
36
     * @throws \yii\base\InvalidConfigException
37
     */
38
    public function actionIndex($email, $username, $password = null, $role = null)
39
    {
40
        /** @var User $user */
41
        $user = $this->make(
42
            User::class,
43
            [],
44
            ['scenario' => 'create', 'email' => $email, 'username' => $username, 'password' => $password]
45
        );
46
        $mailService = MailFactory::makeWelcomeMailerService($user);
47
48
        if ($this->make(UserCreateService::class, [$user, $mailService])->run()) {
49
            $this->stdout(Yii::t('usuario', 'User has been created') . "!\n", Console::FG_GREEN);
50
51
            if (null !== $role) {
52
                $this->assignRole($user, $role);
53
            }
54
        } else {
55
            $this->stdout(Yii::t('usuario', 'Please fix following errors:') . "\n", Console::FG_RED);
56
            foreach ($user->errors as $errors) {
57
                foreach ($errors as $error) {
58
                    $this->stdout(' - ' . $error . "\n", Console::FG_RED);
59
                }
60
            }
61
        }
62
    }
63
64
    protected function assignRole(User $user, $role)
65
    {
66
        $auth = Yii::$app->getAuthManager();
67
        if (false === $auth) {
68
            $this->stdout(
69
                Yii::t(
70
                    'usuario',
71
                    'Cannot assign role "{0}" as the AuthManager is not configured on your console application.',
72
                    $role
73
                ) . "\n",
74
                Console::FG_RED
75
            );
76
        } else {
77
            $userRole = $auth->getRole($role);
78
            if (null === $userRole) {
79
                $this->stdout(Yii::t('usuario', 'Role "{0}" not found. Creating it.', [$role]) . "!\n", Console::FG_GREEN);
80
                $userRole = $auth->createRole($role);
81
                $auth->add($userRole);
82
            }
83
            $auth->assign($userRole, $user->id);
84
        }
85
    }
86
}
87