Passed
Pull Request — master (#10)
by
unknown
02:47
created

MakeUser   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 54.84%

Importance

Changes 0
Metric Value
wmc 9
eloc 33
dl 0
loc 91
ccs 17
cts 31
cp 0.5484
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateEmail() 0 8 3
B handle() 0 42 6
1
<?php
2
3
namespace Dyrynda\Artisan\Console\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\Password;
8
use Dyrynda\Artisan\Exceptions\MakeUserException;
9
10
class MakeUser extends Command 
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'make:user';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Create a new application user';
25
26
    /**
27
     * Array of custom fields to attach to the user.
28
     *
29
     * @var array
30
     */
31
    protected $customFields = [];
32
33
    /**
34
     * Execute the console command.
35
     *
36
     * Handle creation of the new application user.
37
     *
38
     * @return void
39
     */
40 2
    public function handle() 
41
    {
42 2
        $email = $this->ask("What is the new user's email address?");
43 2
        $name = $this->ask("What is the new user's name?") ?: '';
44 2
        $password = $this->secret("What is the new user's password? (blank generates a random one)", str_random(32));
0 ignored issues
show
Deprecated Code introduced by
The function str_random() has been deprecated: Str::random() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

44
        $password = $this->secret("What is the new user's password? (blank generates a random one)", /** @scrutinizer ignore-deprecated */ str_random(32));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
Bug introduced by
str_random(32) of type string is incompatible with the type boolean expected by parameter $fallback of Illuminate\Console\Command::secret(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $password = $this->secret("What is the new user's password? (blank generates a random one)", /** @scrutinizer ignore-type */ str_random(32));
Loading history...
45 2
        $encrypt = $this->confirm('Should the password be encrypted?', true);
46 2
        $sendReset = $this->confirm('Do you want to send a password reset email?');
47
48 2
        if ($encrypt) {
49 2
            $password = bcrypt($password);
50
        }
51
52 2
        while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', false)) {
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type null|string expected by parameter $default of Illuminate\Console\Command::ask(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', /** @scrutinizer ignore-type */ false)) {
Loading history...
53 1
            list($key, $value) = explode('=', $custom);
54 1
            $this->customFields[$key] = value($value);
55
        }
56
57
        try {
58 2
            app('db')->beginTransaction();
59
60
            $this->validateEmail($email);
61
62
            app(config('auth.providers.users.model'))->create(array_merge(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
            app(config('auth.providers.users.model'))->/** @scrutinizer ignore-call */ create(array_merge(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
                compact('email', 'name', 'password'),
64
                $this->customFields
65
            ));
66
67
            if ($sendReset) {
68
                Password::sendResetLink(compact('email'));
69
70
                $this->info("Sent password reset email to {$email}");
71
            }
72
73
            $this->info("Created new user for email {$email}");
74
75
            app('db')->commit();
76 2
        } catch (Exception $e) {
77 2
            $this->error($e->getMessage());
78
79 2
            $this->error('The user was not created');
80
81 2
            app('db')->rollBack();
82
        }
83 2
    }
84
85
    /**
86
     * Determine if the given email address already exists.
87
     *
88
     * @param  string  $email
89
     * @return void
90
     *
91
     * @throws \Dyrynda\Artisan\Exceptions\MakeUserException
92
     */
93
    private function validateEmail($email) 
94
    {
95
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
96
            throw MakeUserException::invalidEmail($email);
97
        }
98
99
        if (app(config('auth.providers.users.model'))->where('email', $email)->exists()) {
0 ignored issues
show
Bug introduced by
The method where() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        if (app(config('auth.providers.users.model'))->/** @scrutinizer ignore-call */ where('email', $email)->exists()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
100
            throw MakeUserException::emailExists($email);
101
        }
102
    }
103
}
104