Completed
Push — master ( 11d850...163a0b )
by Cristian
01:58 queued 01:55
created

CreateUser::handle()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 32
nop 0
dl 0
loc 32
rs 8.7857
c 0
b 0
f 0
1
<?php
2
3
namespace Backpack\Base\app\Console\Commands;
4
5
use Illuminate\Console\Command;
6
7
class CreateUser extends Command
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'backpack:base:user
15
                            {--N|name= : The name of the new user}
16
                            {--E|email= : The user\'s email address}
17
                            {--P|password= : User\'s password}
18
                            {--encrypt=true : Is the user\'s password already encrypted}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new user';
26
27
    /**
28
     * Execute the console command.
29
     *
30
     * @return mixed
31
     */
32
    public function handle()
33
    {
34
        $this->info('Creating a new user');
35
36
        if (!$name = $this->option('name')) {
37
            $name = $this->ask('Name');
38
        }
39
40
        if (!$email = $this->option('email')) {
41
            $email = $this->ask('Email');
42
        }
43
44
        if (!$password = $this->option('password')) {
45
            $password = $this->secret('Password');
46
        }
47
48
        if ($this->option('encrypt')) {
49
            $password = bcrypt($password);
0 ignored issues
show
Bug introduced by
It seems like $password can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
50
        }
51
52
        $auth = config('backpack.base.user_model_fqn', 'App\User');
53
        $user = new $auth();
54
        $user->name = $name;
55
        $user->email = $email;
56
        $user->password = $password;
57
58
        if ($user->save()) {
59
            $this->info('Successfully created new user');
60
        } else {
61
            $this->error('Something went wrong trying to save your user');
62
        }
63
    }
64
}
65