GenerateSuperAdmin   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 50
dl 0
loc 105
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A handle() 0 56 3
1
<?php
2
3
namespace Chuckbe\Chuckcms\Commands;
4
5
use Chuckbe\Chuckcms\Chuck\UserRepository;
6
use Chuckbe\Chuckcms\Models\User;
7
use Illuminate\Console\Command;
8
9
class GenerateSuperAdmin extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'chuckcms:generate-super-admin';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'This command generates a super admin user.';
24
25
    /**
26
     * The user repository implementation.
27
     *
28
     * @var UserRepository
29
     */
30
    protected $userRepository;
31
32
    /**
33
     * The user model implementation.
34
     *
35
     * @var User
36
     */
37
    protected $user;
38
39
    /**
40
     * Create a new command instance.
41
     *
42
     * @return void
43
     */
44
    public function __construct(UserRepository $userRepository, User $user)
45
    {
46
        parent::__construct();
47
48
        $this->userRepository = $userRepository;
49
50
        $this->user = $user;
51
    }
52
53
    /**
54
     * Execute the console command.
55
     *
56
     * @return mixed
57
     */
58
    public function handle()
59
    {
60
        $name = $this->ask('What is your name? ');
61
        $email = $this->ask('What is your email? ');
62
        $password = $this->secret('Enter a password ');
63
64
        // Validate user input
65
        $this->info('Validating your information and generating a new user...');
66
67
        $data = [
68
            'name'      => $name,
69
            'email'     => $email,
70
            'password'  => $password,
71
        ];
72
73
        $rules = [
74
            'name'      => 'required|max:185',
75
            'email'     => 'required|email',
76
            'password'  => 'required|min:8',
77
        ];
78
79
        $validator = \Validator::make($data, $rules);
80
81
        if ($validator->fails()) {
82
            $messages = $validator->errors();
83
            foreach ($messages->all() as $message) {
84
                $this->error($message);
85
            }
86
        } else {
87
            // create the user
88
            $user = $this->user->create([
89
                'name'     => $name,
90
                'email'    => $email,
91
                'token'    => $this->userRepository->createToken(),
92
                'password' => bcrypt($password),
93
                'active'   => 1,
94
            ]);
95
            // add role
96
            $user->assignRole('super-admin');
97
            $this->info('.         .');
98
            $this->info('..         ..');
99
            $this->info('...         ...');
100
            $this->info('.... AWESOME ....');
101
            $this->info('...         ...');
102
            $this->info('..         ..');
103
            $this->info('.         .');
104
            $this->info('.         .');
105
            $this->info('..         ..');
106
            $this->info('...         ...');
107
            $this->info('....   JOB   ....');
108
            $this->info('...         ...');
109
            $this->info('..         ..');
110
            $this->info('.         .');
111
            $this->info(' ');
112
            $this->info('New super admin: '.$name.' ('.$email.') generated successfully');
113
            $this->info(' ');
114
        }
115
    }
116
}
117