Completed
Push — fixes ( 6830e4...798510 )
by Tony
03:34
created

AddUser::handle()   C

Complexity

Conditions 8
Paths 65

Size

Total Lines 52
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 52
rs 6.8493
c 1
b 0
f 0
cc 8
eloc 27
nc 65
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $user = new User();
46
47
        // set username
48
        $user->username = $this->argument('username');
49
        if (User::where('username', $user->username)->exists()) {
50
            $this->error('User '.$user->username.' exists.');
51
            return;
52
        }
53
54
        // set realname
55
        if ($this->option('realname')) {
56
            $user->realname = $this->option('realname');
57
        }
58
        else {
59
            $user->realname = $this->ask('Real Name');
60
        }
61
62
        // set email
63
        if ($this->option('email')) {
64
            $user->email = $this->option('email');
65
        }
66
        else {
67
            $user->email = $this->ask('Email');
68
        }
69
70
        // set user level, start with 1 and upgrade as specified
71
        $user->level = 1;
72
        if ($this->option('read')) {
73
            $user->level = 5;
74
        }
75
        if ($this->option('admin')) {
76
            $user->level = 10;
77
        }
78
79
        // set password
80
        if ($this->argument('password')) {
81
            $user->password = $this->argument('password');
82
        }
83
        else {
84
            $user->password = $this->secret('Password');
85
        }
86
87
        // save user
88
        if ($user->save()) {
89
            $this->info('User '.$user->username.' created.');
90
        }
91
        else {
92
            $this->error('Failed to create user '.$user->username);
93
        }
94
    }
95
}
96