Passed
Pull Request — master (#724)
by John
10:53
created

Install::createFrontAdminUser()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 6
nop 0
dl 0
loc 21
rs 9.4222
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands\Install;
4
5
use Illuminate\Console\Command;
6
use App\Models\Eloquent\Group;
7
use App\Models\Eloquent\User;
8
use Hash;
9
use Str;
10
use Exception;
11
12
class Install extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature='install';
20
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description='Install NOJ';
27
28
    public static $license=1630315446;
29
30
    public static $logoGeneral=<<< LOGOG
31
          ___
32
          \\\\||
33
         ,'_,-\
34
         ;'____\
35
         || =\=|
36
         ||  - |  Haec sententia nullam significationem habet, sed Latina pulchra est.
37
     ,---'._--''-,,---------.--.----_,
38
    / `-._- _--/,,|  ___,,--'--'._<
39
   /-._,  `-.__;,,|'
40
  /   ;\      / , ;
41
 /  ,' | _ - ',/, ;
42
(  (   |     /, ,,;
43
 \  \  |     ',,/,;
44
  \  \ |    /, / ,;
45
 (| ,^.|   / ,, ,/;
46
  `-'./ `-._,, ,/,;
47
       ⧫-._ `-._,,;
48
       |/,,`-._ `-.
49
       |, ,;, ,`-._\
50
╔═══════════════════════════════════════════════════════════════════════════════════════════════════════╗
51
║ ███╗   ██╗ ██████╗      ██╗    ██╗███╗   ██╗███████╗████████╗ █████╗ ██╗     ██╗     ███████╗██████╗  ║
52
║ ████╗  ██║██╔═══██╗     ██║    ██║████╗  ██║██╔════╝╚══██╔══╝██╔══██╗██║     ██║     ██╔════╝██╔══██╗ ║
53
║ ██╔██╗ ██║██║   ██║     ██║    ██║██╔██╗ ██║███████╗   ██║   ███████║██║     ██║     █████╗  ██████╔╝ ║
54
║ ██║╚██╗██║██║   ██║██   ██║    ██║██║╚██╗██║╚════██║   ██║   ██╔══██║██║     ██║     ██╔══╝  ██╔══██╗ ║
55
║ ██║ ╚████║╚██████╔╝╚█████╔╝    ██║██║ ╚████║███████║   ██║   ██║  ██║███████╗███████╗███████╗██║  ██║ ║
56
║ ╚═╝  ╚═══╝ ╚═════╝  ╚════╝     ╚═╝╚═╝  ╚═══╝╚══════╝   ╚═╝   ╚═╝  ╚═╝╚══════╝╚══════╝╚══════╝╚═╝  ╚═╝ ║
57
╚═══════════════════════════════════════════════════════════════════════════════════════════════════════╝
58
LOGOG;
59
60
    /**
61
     * Create a new command instance.
62
     *
63
     * @return void
64
     */
65
    public function __construct()
66
    {
67
        parent::__construct();
68
    }
69
70
    /**
71
     * Execute the console command.
72
     *
73
     * @return mixed
74
     */
75
    public function handle()
76
    {
77
        $this->initWelcone();
78
        $this->initSystemCheck();
79
        if ($this->confirm('Do you wish to continue?')) {
80
            if(!$this->acceptLicense()){
81
                return;
82
            }
83
            if($this->createFrontAdminUser()){
84
                $this->createFrontAdminGroup();
85
            }
86
            $this->installBabelExtensionNOJ();
87
            $this->importExampleProblem();
88
            $this->importBackAdminMenuPermissionsRoles();
89
            $this->createBackAdminUser();
90
        }
91
    }
92
93
    protected function initWelcone()
94
    {
95
        $this->line('<fg=cyan>'.static::$logoGeneral.'</>');
96
        $this->line(sprintf('NOJ <comment>version</comment> <info>%s</info>', version()));
97
        $this->comment('');
98
        sleep(1);
99
    }
100
101
    protected function initSystemCheck() {
102
        $this->info('All system check completed with 0 warning 0 error.');
103
        $this->comment('');
104
        sleep(1);
105
    }
106
107
    protected function acceptLicense() {
108
        $this->warn('NOJ open-source license has been updated, please accept it first:');
109
        $this->comment('');
110
        sleep(1);
111
        $this->line(file_get_contents(base_path('LICENSE')));
112
        $this->warn('Notice that above license is actually a copy from https://github.com/ZsgsDesign/NOJ and remote license is the one that takes effect.');
113
        $this->comment('');
114
        sleep(1);
115
        return $this->confirm('Do you agree with the license?');
116
    }
117
118
    protected function createFrontAdminUser() {
119
        $this->line('Creating frontstage admin user...');
120
        $this->comment('');
121
        $shallCreate = true;
122
        if (User::count()) {
123
            $shallCreate = $this->confirm('Detected existing frontstage user, do you really want to create frontstage admin?');
124
            $this->comment('');
125
        }
126
        if (!$shallCreate) {
127
            return false;
128
        }
129
        while(true) {
130
            try {
131
                $createdUser = $this->createFrontUser();
0 ignored issues
show
Unused Code introduced by
The assignment to $createdUser is dead and can be removed.
Loading history...
132
                break;
133
            } catch(Exception $e) {
134
                $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Error occured while creating admin user.</>\n");
135
                continue;
136
            }
137
        }
138
        return true;
139
    }
140
141
    private function createFrontUser() {
142
        $username = $this->ask('Please choose a username:', 'admin');
143
        $email = $this->ask('Please choose a email address:', '[email protected]');
144
        $password = Hash::make(Str::random(8));
145
        $createdUser=User::create([
146
            'name' => $username,
147
            'email' => $email,
148
            'password' => $password,
149
            'avatar' => '/static/img/avatar/noj.png',
150
        ]);
151
        $createdUser->markEmailAsVerified();
152
        return $createdUser;
153
    }
154
155
    protected function createFrontAdminGroup() {
156
157
    }
158
159
    protected function installBabelExtensionNOJ() {
160
161
    }
162
163
    protected function importExampleProblem() {
164
165
    }
166
167
    protected function importBackAdminMenuPermissionsRoles() {
168
169
    }
170
171
    protected function createBackAdminUser() {
172
173
    }
174
}
175