Conditions | 3 |
Paths | 2 |
Total Lines | 53 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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( |
||
|
|||
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 | } |
||
84 |