Completed
Push — master ( 8927df...f70445 )
by Abdelrahman
02:05
created

UserAssignRoleCommand::handle()   C

Complexity

Conditions 8
Paths 54

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 19
nc 54
nop 0
dl 0
loc 34
rs 5.3846
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Console\Commands;
17
18
use Illuminate\Console\Command;
19
use Illuminate\Support\Facades\Lang;
20
21
class UserAssignRoleCommand extends Command
22
{
23
    /**
24
     * The name and signature of the console command.
25
     *
26
     * @var string
27
     */
28
    protected $signature = 'fort:user:assignrole
29
                            {user? : The user identifier}
30
                            {role? : The role identifier}';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Assign a role to a user.';
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return void
43
     */
44
    public function handle()
45
    {
46
        $userField = $this->argument('user') ?: $this->ask(Lang::get('rinvex.fort::artisan.user.identifier'));
47
48
        if (intval($userField)) {
49
            $user = $this->laravel['rinvex.fort.user']->find($userField);
50
        } else if (filter_var($userField, FILTER_VALIDATE_EMAIL)) {
51
            $user = $this->laravel['rinvex.fort.user']->findWhere(['email' => $userField])->first();
52
        } else {
53
            $user = $this->laravel['rinvex.fort.user']->findWhere(['username' => $userField])->first();
54
        }
55
56
        if (! $user) {
57
            return $this->error(Lang::get('rinvex.fort::artisan.user.invalid', ['field' => $userField]));
58
        }
59
60
61
        $roleField = $this->argument('role') ?: $this->anticipate(Lang::get('rinvex.fort::artisan.user.role'), $this->laravel['rinvex.fort.role']->findAll()->lists('slug', 'id')->toArray());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 190 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
62
63
        if (intval($roleField)) {
64
            $role = $this->laravel['rinvex.fort.role']->find($roleField);
65
        } else {
66
            $role = $this->laravel['rinvex.fort.role']->findWhere(['slug' => $roleField])->first();
67
        }
68
69
        if (! $role) {
70
            return $this->error(Lang::get('rinvex.fort::artisan.role.invalid', ['field' => $roleField]));
71
        }
72
73
        // Assign role to user
74
        $user->assignRole($role);
75
76
        $this->info(Lang::get('rinvex.fort::artisan.user.roleassigned', ['user' => $user->id, 'role' => $role->id]));
77
    }
78
}
79