Completed
Push — master ( ab3e16...edcf17 )
by Avtandil
03:22
created

UserPassword::handle()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 43
Code Lines 28

Duplication

Lines 17
Ratio 39.53 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 17
loc 43
ccs 0
cts 36
cp 0
rs 8.439
cc 6
eloc 28
nc 6
nop 0
crap 42
1
<?php
2
/*
3
 * This file is part of the Laravel Lodash package.
4
 *
5
 * (c) Avtandil Kikabidze aka LONGMAN <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
declare(strict_types=1);
11
12
namespace Longman\LaravelLodash\Commands;
13
14
use Illuminate\Console\Command;
15
16
class UserPassword extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'user:password {email} {password?}
24
                {--guard= : The guard to use.}';
25
26
    /**
27
     * The console command description.
28
     *
29
     * @var string
30
     */
31
    protected $description = 'Update/reset user password.';
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * @return mixed
37
     */
38
    public function handle()
39
    {
40
        $guard = $this->getGuard();
41
        $config = config('auth');
42
43 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...
44
            $this->error('Provider not found for guard "' . $guard . '"!');
45
46
            return;
47
        }
48
        $provider = $config['guards'][$guard]['provider'];
49 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...
50
            $this->error('Model not found for provider "' . $provider . '"!');
51
52
            return;
53
        }
54
        $model = $config['providers'][$provider]['model'];
55
        $user = new $model;
56
57
        $email = $this->argument('email');
58
        $user = $user->where(compact('email'))->first();
59
        if (empty($user)) {
60
            $this->error('User with email "' . $email . '" not found');
61
62
            return;
63
        }
64
65
        $password = $this->argument('password');
66 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...
67
            if ($this->confirm('Let system generate password?', true)) {
68
                $password = str_random(16);
69
            } else {
70
                $password = $this->secret('Please enter new password');
71
            }
72
        }
73
        $crypted_password = bcrypt($password);
0 ignored issues
show
Bug introduced by
It seems like $password defined by $this->argument('password') on line 65 can also be of type array; however, bcrypt() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
        $user->update([
75
            'password' => $crypted_password,
76
        ]);
77
78
        $this->comment('User "' . $email . '" successfully updated');
79
        $this->info('New Password: ' . $password);
80
    }
81
82
    protected function getGuard(): string
83
    {
84
        $guard = $this->input->getOption('guard');
85
86
        return $guard ?? config('auth.defaults.guard');
87
    }
88
89
}
90