UserAdd   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 17
loc 68
ccs 0
cts 29
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 17 38 5
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
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