UserAdd::getGuard()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Longman\LaravelLodash\Commands;
6
7
use Illuminate\Console\Command;
8
9
class UserAdd extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'user:add {email} {password?}
17
                {--guard= : The guard to use.}';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create a new user.';
25
26
    /**
27
     * Execute the console command.
28
     *
29
     * @return mixed
30
     */
31
    public function handle(): void
32
    {
33
        $guard = $this->getGuard();
34
        $config = config('auth');
35
36 View Code Duplication
        if (! isset($config['guards'][$guard]['provider'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            $this->error('Provider not found for guard "' . $guard . '"!');
38
39
            return;
40
        }
41
        $provider = $config['guards'][$guard]['provider'];
42 View Code Duplication
        if (! isset($config['providers'][$provider]['model'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
43
            $this->error('Model not found for provider "' . $provider . '"!');
44
45
            return;
46
        }
47
        $model = $config['providers'][$provider]['model'];
48
        $user = new $model();
49
50
        $email = $this->argument('email');
51
        $password = $this->argument('password');
52 View Code Duplication
        if (! $password) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            if ($this->confirm('Let system generate password?', true)) {
54
                $password = str_random(16);
55
            } else {
56
                $password = $this->secret('Please enter new password');
57
            }
58
        }
59
        $cryptedPassword = bcrypt($password);
60
        $user->create([
61
            'email'    => $email,
62
            'password' => $cryptedPassword,
63
        ]);
64
65
        $this->comment('User successfully created');
66
        $this->info('E-Mail: ' . $email);
67
        $this->info('Password: ' . $password);
68
    }
69
70
    protected function getGuard(): string
71
    {
72
        $guard = $this->input->getOption('guard');
73
74
        return $guard ?? config('auth.defaults.guard');
75
    }
76
}
77