UserCommand::handle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 15
c 3
b 0
f 0
dl 0
loc 22
rs 9.7666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Uccello\Core\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Hash;
7
use Uccello\Core\Models\Domain;
8
use Uccello\Core\Models\Role;
9
use Uccello\Core\Models\Privilege;
10
use App\User;
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
class UserCommand extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'uccello:user';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Create a new user';
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $domain = Domain::first();
46
47
        $username = $this->ask(trans('uccello::command.user.username'));
0 ignored issues
show
Bug introduced by
It seems like trans('uccello::command.user.username') can also be of type array; however, parameter $question of Illuminate\Console\Command::ask() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
        $username = $this->ask(/** @scrutinizer ignore-type */ trans('uccello::command.user.username'));
Loading history...
48
        $name = $this->ask(trans('uccello::command.user.name'));
49
        $email = $this->ask(trans('uccello::command.user.email'));
50
        $password = $this->secret(trans('uccello::command.user.password'));
0 ignored issues
show
Bug introduced by
It seems like trans('uccello::command.user.password') can also be of type array; however, parameter $question of Illuminate\Console\Command::secret() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $password = $this->secret(/** @scrutinizer ignore-type */ trans('uccello::command.user.password'));
Loading history...
51
        $isAdmin = $this->confirm(trans('uccello::command.user.is_admin'));
0 ignored issues
show
Bug introduced by
It seems like trans('uccello::command.user.is_admin') can also be of type array; however, parameter $question of Illuminate\Console\Command::confirm() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $isAdmin = $this->confirm(/** @scrutinizer ignore-type */ trans('uccello::command.user.is_admin'));
Loading history...
52
53
        $user = User::create([
54
            'username' => $username,
55
            'name' => $name,
56
            'email' => $email,
57
            'password' => Hash::make($password),
58
            'is_admin' => $isAdmin,
59
            'domain_id' => $domain->id
60
        ]);
61
62
        $this->addRole($domain, $user);
63
64
        $this->info(trans('uccello::command.user.user_created'));
0 ignored issues
show
Bug introduced by
It seems like trans('uccello::command.user.user_created') can also be of type array; however, parameter $string of Illuminate\Console\Command::info() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
        $this->info(/** @scrutinizer ignore-type */ trans('uccello::command.user.user_created'));
Loading history...
65
    }
66
67
    /**
68
     * Add role to user
69
     */
70
    protected function addRole($domain, $user)
71
    {
72
        $roles = Role::orderBy('name')->get();
73
        $role = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $role is dead and can be removed.
Loading history...
74
        if ($roles) {
75
            $choices = [ ];
76
            foreach ($roles as $role) {
77
                $choices[ ] = $role->name;
78
            }
79
80
            $roleName = $this->choice(trans('uccello::command.user.role'), $choices);
0 ignored issues
show
Bug introduced by
It seems like trans('uccello::command.user.role') can also be of type array; however, parameter $question of Illuminate\Console\Command::choice() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
            $roleName = $this->choice(/** @scrutinizer ignore-type */ trans('uccello::command.user.role'), $choices);
Loading history...
81
            $role = Role::where('name', $roleName)->first();
82
83
            Privilege::firstOrCreate([
84
                'domain_id' => $domain->id,
85
                'role_id' => $role->id,
86
                'user_id' => $user->id
87
            ]);
88
        }
89
    }
90
}