UserPassword::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
use function compact;
10
11
class UserPassword extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'user:password {email} {password?}
19
                {--guard= : The guard to use.}';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Update/reset user password.';
27
28
    /**
29
     * Execute the console command.
30
     *
31
     * @return mixed
32
     */
33
    public function handle(): void
34
    {
35
        $guard = $this->getGuard();
36
        $config = config('auth');
37
38 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...
39
            $this->error('Provider not found for guard "' . $guard . '"!');
40
41
            return;
42
        }
43
        $provider = $config['guards'][$guard]['provider'];
44 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...
45
            $this->error('Model not found for provider "' . $provider . '"!');
46
47
            return;
48
        }
49
        $model = $config['providers'][$provider]['model'];
50
        $user = new $model();
51
52
        $email = $this->argument('email');
53
        $user = $user->where(compact('email'))->first();
54
        if (empty($user)) {
55
            $this->error('User with email "' . $email . '" not found');
56
57
            return;
58
        }
59
60
        $password = $this->argument('password');
61 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...
62
            if ($this->confirm('Let system generate password?', true)) {
63
                $password = str_random(16);
64
            } else {
65
                $password = $this->secret('Please enter new password');
66
            }
67
        }
68
        $cryptedPassword = bcrypt($password);
69
        $user->update([
70
            'password' => $cryptedPassword,
71
        ]);
72
73
        $this->comment('User "' . $email . '" successfully updated');
74
        $this->info('New Password: ' . $password);
75
    }
76
77
    protected function getGuard(): string
78
    {
79
        $guard = $this->input->getOption('guard');
80
81
        return $guard ?? config('auth.defaults.guard');
82
    }
83
}
84