Passed
Push — dev6 ( e1f615...9dca69 )
by Ron
15:55
created

TbBackupDefaultCommand   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 34
c 1
b 0
f 0
dl 0
loc 72
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 28 4
A __construct() 0 3 1
A wipeFiles() 0 26 4
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