davide cucurnia
2024-01-30 9f6455d9b12bda65377e8501e00557982be9ff36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
 
namespace App\Console\Commands;
 
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
 
class CreateUserCommand extends Command
{
    public const E_OK = 0;
    public const E_USER_EXISTS = 1;
    public const E_UPDATING_FAILED = 2;
    public const E_CHAOS = 5;
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:create
                                {username? : Select the new users name}
                                {email? : Select the new users email}
                                {password? : Select the new users password}
                                {--force : Overwrite existing users }';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a user with a command with artisan';
 
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $class = config(
            'auth.providers.'
            .config(
                'auth.guards.'
                .config('auth.defaults.guard', 'web')
                .'.provider'
            )
            .'.model'
        );
 
        $user = new $class();
 
        $user->username = ($this->argument('username') === null)
            ? $this->ask('User name: ')
            : $this->argument('username');
 
        $user->email = ($this->argument('email') === null)
            ? $this->ask('User email: ')
            : $this->argument('email');
 
        $user->password = ($this->argument('password') === null)
            ? Hash::make($this->ask('User password: '))
            : Hash::make($this->argument('password'));
 
        if ($this->option('force') === true || $this->confirm('Save this user?', true)) {
            $exists = (new $class)->where([
                'username' => $user->username,
                'email' => $user->email,
            ])->first();
            if ($exists === null) { // user is not exisiting yet, just create him and return
                $user->save();
                $this->info('Created a user with id: '.$user->id);
 
                return self::E_OK;
            }
            // user is already existing, check the input and handle
            if ($this->option('force')) {
                if ($exists->update($user->getAttributes())) {
                    $this->info('Updated an existing user with id: '.$exists->id);
 
                    return self::E_OK;
                }
                $this->warn('Updating an existing user with id: '.$exists->id.' ended with errors');
 
                return self::E_UPDATING_FAILED;
            }
            $this->error('User already exist with id: '.$exists->id);
 
            return self::E_USER_EXISTS;
        }
 
        return self::E_CHAOS; // default end with an error to show, that this line was hit accidentially
    }
}