Passed
Push — dev5 ( 1140b1...da16e0 )
by Ron
09:38
created

backupRun::backupFiles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 8
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Zip;
6
use Carbon\Carbon;
7
use Illuminate\Console\Command;
8
use Illuminate\Support\Facades\File;
9
use Symfony\Component\Process\Process;
10
use Illuminate\Support\Facades\Storage;
11
12
class backupRun extends Command
13
{
14
    /**
15
     * The name and signature of the console command.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'tb-backup:run
20
                                {--databaseOnly : Only backup configuration database}
21
                                {--filesOnly : Only backup uploaded files}';
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Creates a Tech Bench Bacup';
29
30
    protected $archive, $bar;
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
36
     */
37 2
    public function __construct()
38
    {
39 2
        parent::__construct();
40 2
    }
41
42
    /**
43
     * Execute the console command.
44
     *
45
     * @return mixed
46
     */
47
    public function handle()
48
    {
49
        $this->line('Creating Tech Bench Backup...');
50
        $this->bar = $this->output->createProgressBar(12);
51
52
        $this->createBackupArchive();
53
54
        if(!$this->option('databaseOnly'))
55
        {
56
            $this->backupFiles();
57
        }
58
        if(!$this->option('filesOnly'))
59
        {
60
            $this->backupDatabase();
61
        }
62
63
        $this->closeBackupArchive();
64
        $this->cleanup();
65
        $this->bar->finish();
66
        $this->info('Backup Completed');
67
    }
68
69
70
    protected function createBackupArchive()
71
    {
72
        $backupName = 'TB_Backup_'.Carbon::now()->format('Ymd_his').'.zip';
73
74
        //  Create a text file holding the current system version
75
        $version = new \PragmaRX\Version\Package\Version();
76
        File::put(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'version.txt', $version->version_only());
77
        $this->bar->advance();
78
79
        //  Create the archive and place the version file in it
80
        $this->zip = Zip::create(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.$backupName);
0 ignored issues
show
Bug introduced by
The property zip does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
81
        $this->bar->advance();
82
        $this->zip->add(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'version.txt');
83
        $this->bar->advance();
84
        $this->zip->add(base_path().DIRECTORY_SEPARATOR.'.env');
85
        $this->bar->advance();
86
    }
87
88
    protected function closeBackupArchive()
89
    {
90
        $this->zip->close();
91
        $this->bar->advance();
92
    }
93
94
    //  Backup stored files
95
    protected function backupFiles()
96
    {
97
        //  All uploaded files
98
        $this->zip->add(config('filesystems.disks.local.root'));
99
        $this->bar->advance();
100
        //  All publicly available files
101
        $this->zip->add(config('filesystems.disks.public.root'));
102
        $this->bar->advance();
103
        //  All system logs
104
        $this->zip->add(config('filesystems.disks.logs.root'));
105
        $this->bar->advance();
106
    }
107
108
    //  Create a MYSQLDUMP of the database
109
    protected function backupDatabase()
110
    {
111
        $process = new Process(
112
        /** @scrutinizer ignore-type */
113
        'mysqldump tb-dev5-data -u root');
0 ignored issues
show
Documentation introduced by
'mysqldump tb-dev5-data -u root' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
114
        $process->run();
115
116
        File::put(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'database.sql', $process->getOutput());
117
        $this->bar->advance();
118
        $this->zip->add(config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'database.sql');
119
        $this->bar->advance();
120
    }
121
122
    //  Remove any temporary files
123
    protected function cleanup()
124
    {
125
        $this->bar->advance();
126
        Storage::disk('backup')->delete('version.txt');
127
        Storage::disk('backup')->delete('database.sql');
128
    }
129
}
130