Completed
Push — master ( 36858c...6c23f8 )
by Abdelrahman
07:21
created

RoleFindCommand::handle()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 4
nop 0
dl 0
loc 23
rs 8.5906
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 RoleFindCommand extends Command
22
{
23
    /**
24
     * The name and signature of the console command.
25
     *
26
     * @var string
27
     */
28
    protected $signature = 'fort:role:find {field? : Get specific role by field}';
29
30
    /**
31
     * The console command description.
32
     *
33
     * @var string
34
     */
35
    protected $description = 'List matched roles.';
36
37
    /**
38
     * Execute the console command.
39
     *
40
     * @return void
41
     */
42
    public function handle()
43
    {
44
        $columns = ['id', 'name', 'slug', 'created_at', 'updated_at'];
45
46
        // Find single role
47
        if ($field = $this->argument('field')) {
48
            if (intval($field) && $role = $this->laravel['rinvex.fort.role']->find($field, $columns)) {
49
                return $this->table($columns, [$role->toArray()]);
50
            } else if ($role = $this->laravel['rinvex.fort.role']->findWhere(['slug' => $field], $columns)->first()) {
51
                return $this->table($columns, $role->toArray());
52
            }
53
54
            return $this->error(Lang::get('rinvex.fort::artisan.role.invalid', ['field' => $field]));
55
        }
56
57
        // Find multiple roles
58
        $field    = $this->anticipate(Lang::get('rinvex.fort::artisan.role.field'), ['id', 'slug'], 'id');
59
        $operator = $this->anticipate(Lang::get('rinvex.fort::artisan.role.operator'), ['=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'like binary', 'not like', 'between', 'ilike', '&', '|', '^', '<<', '>>', 'rlike', 'regexp', 'not regexp', '~', '~*', '!~', '!~*', 'similar to', 'not similar to'], '=');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 304 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...
60
        $value    = $this->ask(Lang::get('rinvex.fort::artisan.role.value'));
61
        $results  = $this->laravel['rinvex.fort.role']->where($field, $operator, $value)->get($columns);
62
63
        return $this->table($columns, $results->toArray());
64
    }
65
    
66
    /**
67
     * Register the related console commands.
68
     *
69
     * @return void
70
     */
71
    protected function registerCommands()
72
    {
73
        foreach ($this->commands as $commandKey => $commandClass) {
0 ignored issues
show
Bug introduced by
The property commands does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
74
            $this->app->singleton($commandKey, function ($app) use ($commandClass) {
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
75
                return new $commandClass();
76
            });
77
        }
78
79
        $this->commands(array_keys($this->commands));
0 ignored issues
show
Bug introduced by
The method commands() does not exist on Rinvex\Fort\Console\Commands\RoleFindCommand. Did you maybe mean registerCommands()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
    }
81
}
82