Backup   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
c 2
b 0
f 0
dl 0
loc 64
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 43 5
1
<?php
2
3
namespace EDouna\LaravelDBBackup\Commands;
4
5
use Illuminate\Support\Facades\Log;
6
7
class Backup extends BaseCommand
8
{
9
    /**
10
     * The name and signature of the console command.
11
     *
12
     * @var string
13
     */
14
    protected $signature = 'db:backup';
15
16
    /**
17
     * The console command description.
18
     *
19
     * @var string
20
     */
21
    protected $description = 'Attempts to run a database back-up procedure.';
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return mixed
27
     */
28
    public function handle(): int
29
    {
30
        Log::info('Starting back-up procedure.');
31
        $this->line('Starting back-up procedure.');
32
        $startTime = microtime(true);
33
34
        $this->database->buildDatabaseClass();
35
36
        if (false === $this->database->isDatabaseSupported()) {
37
            $this->error(sprintf('The current selected %s database driver is not (yet) supported.', $this->database->getRealDatabase()->getDatabaseIdentifier()));
38
39
            return 1;
40
        }
41
42
        if (false === $this->storage->initializeStorageFolder()) {
43
            $this->error('Error in the back-up directory. Please see the error log for further details.');
44
45
            return 1;
46
        }
47
48
        $this->database->setBackupFilename($this->storage->generateBackupFilename($this->database->getRealDatabase()->getDatabaseIdentifier(), $this->database->getRealDatabase()->getFileExtension()));
49
50
        $this->comment(sprintf('Current selected database driver: %s', $this->database->getRealDatabase()->getDatabaseIdentifier()));
51
        $this->comment('Creating back-up. Depending on the database size, this might take few moments...');
52
        // Run the back-up
53
        if (false === $this->database->getRealDatabase()->backup($this->database->getBackupFilename())) {
54
            $this->error('Error while performing back-up. Please find the error log for further details.');
55
56
            return 1;
57
        }
58
59
        $this->comment('Done creating back-up! Archiving the database...');
60
        if (false === $this->storage->createArchiveFile($this->database->getBackupFilename())) {
61
            $this->error('Error while creating the archive file. Please find the error log for further details.');
62
63
            return 1;
64
        }
65
66
        $this->comment('Done creating archive!');
67
        $endTime = round(microtime(true) - $startTime, 2);
68
        $this->line(sprintf('Finished back-up procedure in %s second(s).', $endTime));
69
70
        return 0;
71
    }
72
}
73