Passed
Push — master ( 0f8d01...d9ac8c )
by R.Kukuh
05:14
created

Rebuild::wakeUpFromMaintenanceMode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 5
rs 10
1
<?php
2
3
namespace BukanKalengKaleng\LaravelRebuild\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Composer;
7
8
class Rebuild extends Command
9
{
10
    /**
11
     * The name and signature of the console command.
12
     *
13
     * @var string
14
     */
15
    protected $signature = 'rebuild';
16
17
    /**
18
     * The console command description.
19
     *
20
     * @var string
21
     */
22
    protected $description = 'Rebuild the app from scratch';
23
24
    /**
25
     * Create a new command instance.
26
     *
27
     * @return void
28
     */
29
    public function __construct(Composer $composer)
30
    {
31
        parent::__construct();
32
33
        $this->composer = $composer;
0 ignored issues
show
Bug Best Practice introduced by
The property composer does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        if (app()->environment(['prod', 'production'])) {
0 ignored issues
show
introduced by
The method environment() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

43
        if (app()->/** @scrutinizer ignore-call */ environment(['prod', 'production'])) {
Loading history...
44
            if ($this->confirm('You are in PRODUCTION environment. Continue?')) {
45
                $this->rebuildSequence();
46
47
                return;
48
            }
49
        }
50
51
        $this->rebuildSequence();
52
    }
53
54
    /**
55
     * Command operation sequence
56
     *
57
     * @return mixed
58
     */
59
    protected function rebuildSequence()
60
    {
61
        if ($this->confirm('You are about to rebuild the app from scratch. Continue?')) {
62
            $this->setToMaintenanceMode();
63
            $this->rebuildDatabaseSchema();
64
            $this->seedInitialData();
65
            $this->seedDummyData();
66
            $this->seedExampleData();
67
            $this->clearCache();
68
            $this->clearConfig();
69
            $this->clearRoute();
70
            $this->clearView();
71
            $this->flushExpiredPasswordResetToken();
72
            $this->clearCompiledClasses();
73
            $this->rediscoverPackages();
74
            $this->createSymbolicLink();
75
            $this->runSelfDiagnosis();
76
            $this->wakeUpFromMaintenanceMode();
77
        }
78
    }
79
80
    /**
81
     * Rebuild database schema
82
     *
83
     * @return void
84
     */
85
    protected function setToMaintenanceMode()
86
    {
87
        if (config('rebuild.should_set_to_maintenance_mode')) {
88
            $this->call('down');
89
            $this->line('');
90
        }
91
    }
92
93
    /**
94
     * Rebuild database schema
95
     *
96
     * @return void
97
     */
98
    protected function rebuildDatabaseSchema()
99
    {
100
        if (config('rebuild.should_rebuild_database_schema')) {
101
            $this->call('migrate:fresh', ['--force' => true]);
102
            $this->info('Rebuilding database schema is done.');
103
            $this->line('');
104
        }
105
    }
106
107
    /**
108
     * Seeding initial data
109
     *
110
     * @return void
111
     */
112
    protected function seedInitialData()
113
    {
114
        if (config('rebuild.should_seed_initial_data')) {
115
            $this->composer->dumpAutoloads();
116
117
            $this->call('db:seed', ['--force' => true]);
118
            $this->info('Seeding initial data is done.');
119
            $this->line('');
120
        }
121
    }
122
123
    /**
124
     * Seeding dummy data
125
     *
126
     * @return void
127
     */
128
    protected function seedDummyData()
129
    {
130
        if (config('rebuild.dummy.should_seed')) {
131
            if ($this->confirm('Install dummy data?')) {
132
                try {
133
                    $this->call('db:seed', [
134
                        '--class' => config('rebuild.dummy.seeder_name'),
135
                        '--force' => true,
136
                    ]);
137
                } catch (\Exception $e) {
138
                    $this->error($e->getMessage());
139
                    $this->info('Seeding dummy data is aborted.');
140
                    $this->line('');
141
142
                    return true;
143
                }
144
145
                $this->info('Seeding dummy data is done.');
146
                $this->line('');
147
            }
148
        }
149
    }
150
151
    protected function seedExampleData()
152
    {
153
        if (config('rebuild.example.should_seed')) {
154
            if ($this->confirm('Install example data?')) {
155
                try {
156
                    $this->call('db:seed', [
157
                        '--class' => config('rebuild.example.seeder_name'),
158
                        '--force' => true,
159
                    ]);
160
                } catch (\Exception $e) {
161
                    $this->error($e->getMessage());
162
                    $this->info('Seeding example data is aborted.');
163
                    $this->line('');
164
165
                    return true;
166
                }
167
168
                $this->info('Seeding example data is done.');
169
                $this->line('');
170
            }
171
        }
172
    }
173
174
    /**
175
     * Clear cache
176
     *
177
     * @return void
178
     */
179
    protected function clearCache()
180
    {
181
        if (config('rebuild.should_clear_cache')) {
182
            $this->line('Run artisan \'cache:clear\' command:');
183
            $this->call('cache:clear');
184
            $this->line('');
185
        }
186
    }
187
188
    /**
189
     * Clear config
190
     *
191
     * @return void
192
     */
193
    protected function clearConfig()
194
    {
195
        if (config('rebuild.should_celar_config')) {
196
            $this->line('Run artisan \'config:clear\' command:');
197
            $this->call('config:clear');
198
            $this->line('');
199
        }
200
    }
201
202
    /**
203
     * Clear route
204
     *
205
     * @return void
206
     */
207
    protected function clearRoute()
208
    {
209
        if (config('rebuild.should_clear_route')) {
210
            $this->line('Run artisan \'route:clear\' command:');
211
            $this->call('route:clear');
212
            $this->line('');
213
        }
214
    }
215
216
    /**
217
     * Clear view
218
     *
219
     * @return void
220
     */
221
    protected function clearView()
222
    {
223
        if (config('rebuild.should_clear_view')) {
224
            $this->line('Run artisan \'view:clear\' command:');
225
            $this->call('view:clear');
226
            $this->line('');
227
        }
228
    }
229
230
    /**
231
     * Flush expired password reset token
232
     *
233
     * @return void
234
     */
235
    protected function flushExpiredPasswordResetToken()
236
    {
237
        if (config('rebuild.should_flush_expired_password_reset_token')) {
238
            $this->line('Run artisan \'auth:clear-resets\' command:');
239
            $this->call('auth:clear-resets');
240
            $this->line('');
241
        }
242
    }
243
244
    /**
245
     * Clear compiled classes
246
     *
247
     * @return void
248
     */
249
    protected function clearCompiledClasses()
250
    {
251
        if (config('rebuild.should_clear_compiled_classes')) {
252
            $this->line('Run artisan \'clear-compiled\' command:');
253
            $this->call('clear-compiled');
254
            $this->line('');
255
        }
256
    }
257
258
    /**
259
     * Rebuild packages manifest cache
260
     *
261
     * @return void
262
     */
263
    protected function rediscoverPackages()
264
    {
265
        if (config('rebuild.should_rediscover_packages')) {
266
            $this->line('Run artisan \'package:discover\' command:');
267
            $this->call('package:discover');
268
            $this->line('');
269
        }
270
    }
271
272
    /**
273
     * Create symbolic link
274
     *
275
     * @return void
276
     */
277
    protected function createSymbolicLink()
278
    {
279
        if (config('rebuild.should_create_symbolic_link')) {
280
            $this->line('Run artisan \'storage:link\' command:');
281
            $this->call('storage:link');
282
            $this->line('');
283
        }
284
    }
285
286
    /**
287
     * Run BeyondCode's Laravel Self-Diagnosis command
288
     *
289
     * @return void
290
     */
291
    protected function runSelfDiagnosis()
292
    {
293
        if (config('rebuild.should_self_diagnosis')) {
294
            $this->line('Run artisan \'self-diagosis\' command:');
295
            $this->call('self-diagnosis');
296
            $this->line('');
297
        }
298
    }
299
300
    /**
301
     * Rebuild database schema
302
     *
303
     * @return void
304
     */
305
    protected function wakeUpFromMaintenanceMode()
306
    {
307
        if (config('rebuild.should_wake_up_from_maintenance_mode')) {
308
            $this->call('up');
309
            $this->line('');
310
        }
311
    }
312
}
313