UserPassword   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 23.29 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 17
loc 73
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B handle() 17 43 6
A getGuard() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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