Completed
Branch dev4 (2f299a)
by Ron
08:25
created

BackupApplication::handle()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 34
nc 2
nop 0
dl 0
loc 53
rs 9.376
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Console\Commands;
4
5
use Zip;
6
use Carbon\Carbon;
7
use Illuminate\Support\Facades\Log;
8
use Illuminate\Support\Facades\File;
9
use Illuminate\Console\Command;
10
use Illuminate\Support\Facades\Storage;
11
use Symfony\Component\Process\Process;
12
use Symfony\Component\Process\Exception\ProcessFailedException;
13
14
class BackupApplication extends Command
15
{
16
    //  Command Name
17
    protected $signature = 'backup:now {type=manual}';
18
19
    //  Command description
20
    protected $description = 'Create a full backup of the Tech Bench Application';
21
22
    //  Constructor
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    //  Create backup of system
29
    public function handle()
30
    {
31
        $backupType = $this->argument('type');
32
        $backupDir = config('filesystems.disks.backup.root');
33
        $backupTmp = config('filesystems.disks.backup.root').DIRECTORY_SEPARATOR.'tmp';
34
        $localDir  = config('filesystems.disks.local.root');
35
        $publicDir = config('filesystems.disks.public.root');
36
        $backupBase = $backupType.'_backup-'.Carbon::now()->toDateString().'_'.Carbon::now()->hour.Carbon::now()->minute;
37
                
38
        //  Determine if the backup file already exists
39
        $i = 0;
40
        do
41
        {
42
            if($i)
43
            {
44
                $backupName = $backupBase.'('.$i.')';
45
            }
46
            else
47
            {
48
                $backupName = $backupBase;
49
            }
50
            $i++;
51
        } while(Storage::disk('backup')->exists($backupName.'.zip'));
52
        
53
        //  Write a file that shows the system version    
54
        $version = new \PragmaRX\Version\Package\Version();
55
        Storage::disk('backup')->put('tmp/version.txt', $version->compact());
56
        
57
        //  Create a dump file of the MySQL database
58
        $process = new Process(sprintf(
0 ignored issues
show
Bug introduced by
sprintf('mysqldump -u%s ...ARATOR . 'db_dump.sql') of type string is incompatible with the type array expected by parameter $command of Symfony\Component\Process\Process::__construct(). ( Ignorable by Annotation )

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

58
        $process = new Process(/** @scrutinizer ignore-type */ sprintf(
Loading history...
59
            'mysqldump -u%s -p%s %s > %s',
60
            config('database.connections.mysql.username'),
61
            config('database.connections.mysql.password'),
62
            config('database.connections.mysql.database'),
63
            $backupTmp.DIRECTORY_SEPARATOR.'db_dump.sql'
64
        ));
65
        $process->mustRun();
66
        
67
        //  Create a zip archive of the tmp folder
68
        $zip = Zip::create($backupDir.DIRECTORY_SEPARATOR.$backupName.'.zip');        
69
        $zip->add($backupTmp.DIRECTORY_SEPARATOR.'version.txt');
70
        $zip->add($backupTmp.DIRECTORY_SEPARATOR.'db_dump.sql');
71
        $zip->add(base_path('.env'));
72
        $zip->add($localDir);
73
        $zip->add($publicDir);
74
        $zip->add(storage_path('logs'));
75
        $zip->close();
76
        
77
        //  Clean up the tmp folder
78
        Storage::disk('backup')->deleteDirectory('tmp');
79
        
80
        Log::notice('Backup file created successfully');
81
        $this->line('Backup Successful');
82
    }
83
}
84