Completed
Pull Request — master (#8)
by Michael
02:18 queued 20s
created

MakeUser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
eloc 30
dl 0
loc 86
ccs 14
cts 28
cp 0.5
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 37 5
A validateEmail() 0 8 3
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 = bcrypt($this->secret("What is the new user's password? (blank generates a random one)", str_random(32)));
0 ignored issues
show
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 = bcrypt($this->secret("What is the new user's password? (blank generates a random one)", /** @scrutinizer ignore-type */ str_random(32)));
Loading history...
45 2
        $sendReset = $this->confirm('Do you want to send a password reset email?');
46
47 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

47
        while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', /** @scrutinizer ignore-type */ false)) {
Loading history...
48 1
            list($key, $value) = explode('=', $custom);
49 1
            $this->customFields[$key] = value($value);
50
        }
51
52
        try {
53 2
            app('db')->beginTransaction();
54
55
            $this->validateEmail($email);
56
57
            app(config('auth.providers.users.model'))->create(array_merge(
0 ignored issues
show
Bug introduced by
The method create() does not exist on Illuminate\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

57
            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...
58
                compact('email', 'name', 'password'),
59
                $this->customFields
60
            ));
61
62
            if ($sendReset) {
63
                Password::sendResetLink(compact('email'));
64
65
                $this->info("Sent password reset email to {$email}");
66
            }
67
68
            $this->info("Created new user for email {$email}");
69
70
            app('db')->commit();
71 2
        } catch (Exception $e) {
72 2
            $this->error($e->getMessage());
73
74 2
            $this->error('The user was not created');
75
76 2
            app('db')->rollBack();
77
        }
78 2
    }
79
80
    /**
81
     * Determine if the given email address already exists.
82
     *
83
     * @param  string  $email
84
     * @return void
85
     *
86
     * @throws \Dyrynda\Artisan\Exceptions\MakeUserException
87
     */
88
    private function validateEmail($email)
89
    {
90
        if (! filter_var($email, FILTER_VALIDATE_EMAIL)) {
91
            throw MakeUserException::invalidEmail($email);
92
        }
93
94
        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\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

94
        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...
95
            throw MakeUserException::emailExists($email);
96
        }
97
    }
98
}
99