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

CreateUserCommand::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
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
}