AddUser::handle()   C
last analyzed

Complexity

Conditions 8
Paths 65

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8.125

Importance

Changes 0
Metric Value
cc 8
eloc 27
nc 65
nop 0
dl 0
loc 48
ccs 21
cts 24
cp 0.875
crap 8.125
rs 5.9322
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Models\User;
6
use Illuminate\Console\Command;
7
8
class AddUser extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'user:add 
16
        {username : The username to add}
17
        {password? : Optional}
18
        {--realname= : The real name of the user}
19
        {--email= : The email of the user}
20
        {--read : Global Read access}
21
        {--admin : Global Admin access}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Add a new user (if supported by the authentication type)';
29
30
    /**
31
     * Create a new command instance.
32
     */
33 87
    public function __construct()
34
    {
35 87
        parent::__construct();
36 87
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43 3
    public function handle()
44
    {
45 3
        $user = new User();
46
47
        // set username
48 3
        $user->username = $this->argument('username');
49 3
        if (User::where('username', $user->username)->exists()) {
50 1
            $this->error('User '.$user->username.' exists.');
51 1
            return;
52
        }
53
54
        // set realname
55 2
        if ($this->option('realname')) {
56 1
            $user->realname = $this->option('realname');
57
        } else {
58 1
            $user->realname = $this->ask('Real Name');
59
        }
60
61
        // set email
62 2
        if ($this->option('email')) {
63 1
            $user->email = $this->option('email');
64
        } else {
65 1
            $user->email = $this->ask('Email');
66
        }
67
68
        // set user level, start with 1 and upgrade as specified
69 2
        $user->level = 1;
70 2
        if ($this->option('read')) {
71
            $user->level = 5;
72
        }
73 2
        if ($this->option('admin')) {
74
            $user->level = 10;
75
        }
76
77
        // set password
78 2
        if ($this->argument('password')) {
79 1
            $user->password = $this->argument('password');
80
        } else {
81 1
            $user->password = $this->secret('Password');
82
        }
83
84
        // save user
85 2
        if ($user->save()) {
86 2
            $this->info('User '.$user->username.' created.');
87
        } else {
88
            $this->error('Failed to create user '.$user->username);
89
        }
90 2
    }
91
}
92