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

UserGiveAbilityCommand::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 UserGiveAbilityCommand extends Command
22
{
23
    /**
24
     * The name and signature of the console command.
25
     *
26
     * @var string
27
     */
28
    protected $signature = 'fort:user:giveability
29
                            {user? : The user identifier}
30
                            {ability? : The ability identifier}';
31
32
    /**
33
     * The console command description.
34
     *
35
     * @var string
36
     */
37
    protected $description = 'Give a ability from 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
        $abilityField = $this->argument('ability') ?: $this->anticipate(Lang::get('rinvex.fort::artisan.user.ability'), $this->laravel['rinvex.fort.ability']->findAll()->lists('slug', 'id')->toArray());
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 202 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($abilityField)) {
64
            $ability = $this->laravel['rinvex.fort.ability']->find($abilityField);
65
        } else {
66
            $ability = $this->laravel['rinvex.fort.ability']->findWhere(['slug' => $abilityField])->first();
67
        }
68
69
        if (! $ability) {
70
            return $this->error(Lang::get('rinvex.fort::artisan.ability.invalid', ['field' => $abilityField]));
71
        }
72
73
        // Give user ability to..
74
        $user->giveAbilityTo($ability);
75
76
        $this->info(Lang::get('rinvex.fort::artisan.user.abilitygived', ['user' => $user->id, 'ability' => $ability->id]));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 123 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...
77
    }
78
}
79