Passed
Push — master ( 59950a...c6d7cc )
by Ron
02:46 queued 13s
created

TbBackupDefaultCommand::handle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 28
rs 9.6333
cc 4
nc 3
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
8
class TbBackupDefaultCommand extends Command
9
{
10
    protected $signature   = 'tb_backup:default {--confirmed} {--demo}';
11
    protected $description = 'Completely wipe all Tech Bench data and start from scratch';
12
13
    /**
14
     * Create a new command instance
15
     */
16
    public function __construct()
17
    {
18
        parent::__construct();
19
    }
20
21
    /**
22
     * Execute the console command
23
     */
24
    public function handle()
25
    {
26
        $this->warn(' ___________________________________________________________________ ');
27
        $this->warn('|                      IMPORTANT NOTE:                              |');
28
        $this->warn('|               ALL EXISTING DATA WILL BE ERASED                    |');
29
        $this->warn('|                   THIS CANNOT BE UNDONE!!!                        |');
30
        $this->warn('|___________________________________________________________________|');
31
        $this->warn('                                                                     ');
32
33
        if(!$this->option('confirmed') && !$this->confirm('Are you sure?'))
34
        {
35
            $this->line('Operation Canceled');
36
            return 0;
37
        }
38
39
        $this->call('down');
40
        $this->callSilently('migrate:fresh');
41
        $this->wipeFiles();
42
        if($this->option('demo'))
43
        {
44
            $this->line('Creating demo data');
45
            $this->callSilently('db:seed');
46
        }
47
        $this->callSilently('storage:link');
48
49
        $this->info('Operation complete');
50
        $this->call('up');
51
        return 0;
52
    }
53
54
    protected function wipeFiles()
55
    {
56
        $disk = 'local';
57
58
        //  Clear all files from the disk
59
        $files = Storage::disk($disk)->allFiles();
60
61
        foreach($files as $file)
62
        {
63
            if($file != '.gitignore')
64
            {
65
                Storage::disk($disk)->delete($file);
66
            }
67
        }
68
69
        //  Clear all sub directories from the disk
70
        $folders = Storage::disk($disk)->directories();
71
        foreach($folders as $folder)
72
        {
73
            Storage::disk($disk)->deleteDirectory($folder);
74
        }
75
76
        //  Restore the public directory
77
        Storage::disk('public')->put('.gitignore', '*');
78
        Storage::disk('public')->append('.gitignore', '!.gitignore');
79
        Storage::disk('public')->append('.gitignore', '');
80
    }
81
}
82