AssignAdmin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initial() 0 13 3
A handle() 0 3 1
A getAdminRole() 0 8 2
A initialRole() 0 8 1
A assignAdmin() 0 4 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Entities\Role;
6
use App\Entities\User;
7
use App\Services\UserService;
8
use Illuminate\Console\Command;
9
10
class AssignAdmin extends Command
11
{
12
    protected $signature = 'assign:admin';
13
14
    public function handle()
15
    {
16
        $this->initial();
17
    }
18
19
    private function initial()
20
    {
21
        while (true) {
22
            $username = $this->ask('You should input administrator username');
23
            /** @var User $user */
24
            $user = app(UserService::class)->findByName($username);
25
            if ($user) {
26
                $this->info('User Id is '.$user->id);
27
                $this->assignAdmin($user);
28
                $this->info('Assign Done');
29
                break;
30
            }
31
            $this->error('User is not found!');
32
        }
33
    }
34
35
    /**
36
     * @param User $user
37
     */
38
    private function assignAdmin($user)
39
    {
40
        $role = $this->getAdminRole();
41
        $user->roles()->attach($role);
42
    }
43
44
    /**
45
     * @return Role|mixed
46
     */
47
    private function getAdminRole()
48
    {
49
        $role = Role::where('name', 'admin')->first();
50
        if (! $role) {
51
            $role = $this->initialRole();
52
        }
53
54
        return $role;
55
    }
56
57
    private function initialRole()
58
    {
59
        $role = new Role();
60
        $role->name = 'admin';
61
        $role->display_name = 'Administrator';
62
        $role->save();
63
64
        return $role;
65
    }
66
}
67