MakeUser::handle()   B
last analyzed

Complexity

Conditions 7
Paths 44

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9.2861

Importance

Changes 7
Bugs 1 Features 0
Metric Value
cc 7
eloc 25
c 7
b 1
f 0
nc 44
nop 0
dl 0
loc 42
ccs 16
cts 25
cp 0.64
crap 9.2861
rs 8.5866
1
<?php
2
3
namespace Dyrynda\Artisan\Console\Commands;
4
5
use Dyrynda\Artisan\Exceptions\MakeUserException;
6
use Exception;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\Password;
9
use Illuminate\Support\Str;
10
11
class MakeUser extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'make:user';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create a new application user';
26
27
    /**
28
     * Array of custom fields to attach to the user.
29
     *
30
     * @var array
31
     */
32
    protected $customFields = [];
33
34
    /**
35
     * Execute the console command.
36
     *
37
     * Handle creation of the new application user.
38
     *
39
     * @return void
40
     */
41 2
    public function handle()
42
    {
43 2
        $email = $this->ask("What is the new user's email address?");
44 2
        $name = $this->ask("What is the new user's name?") ?: '';
45 2
        $password = $this->secret("What is the new user's password? (blank generates a random one)") ?: Str::random(32);
46 2
        $encrypt = $this->confirm('Should the password be encrypted?', true);
47 2
        $sendReset = $this->confirm('Do you want to send a password reset email?');
48
49 2
        if ($encrypt) {
50 2
            $password = bcrypt($password);
51
        }
52
53 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

53
        while ($custom = $this->ask('Do you have any custom user fields to add? Field=Value (blank continues)', /** @scrutinizer ignore-type */ false)) {
Loading history...
54 1
            [$key, $value] = explode('=', $custom);
55 1
            $this->customFields[$key] = value($value);
56
        }
57
58
        try {
59 2
            app('db')->beginTransaction();
60
61
            $this->validateEmail($email);
62
63
            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

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

100
        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...
101
            throw MakeUserException::emailExists($email);
102
        }
103
    }
104
}
105