Completed
Push — master ( 5ca9ad...782d0f )
by Song
03:18
created

CreateUserCommand   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 1
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 25 1
1
<?php
2
3
namespace Encore\Admin\Console;
4
5
use Encore\Admin\Auth\Database\Administrator;
6
use Encore\Admin\Auth\Database\Role;
7
use Illuminate\Console\Command;
8
9
class CreateUserCommand extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'admin:create-user';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Create a admin user';
24
25
    /**
26
     * Execute the console command.
27
     */
28
    public function handle()
29
    {
30
        $username = $this->ask('Please enter a username to login');
31
32
        $password = bcrypt($this->secret('Please enter a password to login'));
33
34
        $name = $this->ask('Please enter a name to display');
35
36
        $roles = Role::all();
37
38
        /** @var array $selected */
39
        $selected = $this->choice('Please choose a role for the user', $roles->pluck('name')->toArray(), null, null, true);
40
41
        $roles = $roles->filter(function ($role) use ($selected) {
42
            return in_array($role->name, $selected);
43
        });
44
45
        $user = new Administrator(compact('username', 'password', 'name'));
46
47
        $user->save();
48
49
        $user->roles()->attach($roles);
50
51
        $this->info("User [$name] created successfully.");
52
    }
53
}