Completed
Push — master ( e275d9...24dcac )
by CodexShaper
04:18
created

DatabaseBackup::findComposer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
namespace CodexShaper\DBM\Commands;
3
4
use CodexShaper\DBM\Facades\Driver;
5
use CodexShaper\Dumper\Contracts\Dumper;
6
use DBM;
0 ignored issues
show
Bug introduced by
The type DBM was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Console\Command;
8
use Illuminate\Filesystem\Filesystem;
9
use Illuminate\Support\Facades\File;
10
use Symfony\Component\Console\Input\InputOption;
11
12
class DatabaseBackup extends Command
13
{
14
    /**
15
     * The console command name.
16
     *
17
     * @var string
18
     */
19
    protected $signature = 'dbm:backup
20
                        {--t|table=}';
21
    /**
22
     * The console command description.
23
     *
24
     * @var string
25
     */
26
    protected $description = 'Database Backup';
27
28
    /**
29
     * Get Option
30
     *
31
     * @return array
32
     */
33
    protected function getOptions()
34
    {
35
        return [
36
            ['force', null, InputOption::VALUE_NONE, 'Force the operation to run when in production', null],
37
        ];
38
    }
39
40
    /**
41
     * Get the composer command for the environment.
42
     *
43
     * @return string
44
     */
45
    protected function findComposer()
46
    {
47
        if (file_exists(getcwd() . '/composer.phar')) {
48
            return '"' . PHP_BINARY . '" ' . getcwd() . '/composer.phar';
49
        }
50
        return 'composer';
51
    }
52
53
    public function getFileName($table, $database)
54
    {
55
56
        $prefix = (strlen($table) > 0)
57
        ? 'table_' . strtolower(str_replace('-', '_', $table)) . "_"
58
        : 'database_' . strtolower(str_replace('-', '_', $database)) . "_";
59
60
        $extension = Driver::isMongoDB() ? '' : '.sql';
61
        $fileName  = $prefix . 'backup_' . date('G_a_m_d_y_h_i_s') . $extension;
62
63
        if (Driver::isSqlite()) {
64
            $fileName = 'backup_' . date('G_a_m_d_y_h_i_s') . $extension;
65
        }
66
67
        return $fileName;
68
    }
69
70
    public function backup($dumper, $filePath)
71
    {
72
        $isCompress         = config('dbm.backup.compress', false);
73
        $isDebug            = config('dbm.backup.debug', false);
74
        $compressBinaryPath = config('dbm.backup.compress_binary_path', "");
75
        $compressCommand    = config('dbm.backup.compress_command', "gzip");
76
        $compressExtension  = config('dbm.backup.compress_extension', ".gz");
77
        $dumpBinaryPath     = config('dbm.backup.' . $driver . '.binary_path', '');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $driver seems to be never defined.
Loading history...
78
79
        switch ($driver) {
80
            case 'mysql':
81
            case 'pgsql':
82
                if (!empty($table)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $table seems to never exist and therefore empty should always be true.
Loading history...
83
                    $dumper->setTables($table);
84
                }
85
                break;
86
            case 'mongodb':
87
                $dsn = config('dbm.backup.mongodb.dsn', '');
88
                if (!empty($dsn) && method_exists($dumper, 'setUri')) {
89
                    $dumper->setUri($dsn);
90
                }
91
                break;
92
93
        }
94
95
        if ($isCompress) {
96
            $dumper->setCompressBinaryPath($compressBinaryPath);
97
            $dumper->setCompressCommand($compressCommand);
98
            $dumper->setCompressExtension($compressExtension);
99
        }
100
        if ($isDebug) {
101
            $dumper->enableDebug();
102
        }
103
        $dumper->setCommandBinaryPath($dumpBinaryPath)
104
            ->setDestinationPath($filePath)
105
            ->dump();
106
    }
107
108
    /**
109
     * Execute the console command.
110
     *
111
     * @param \Illuminate\Filesystem\Filesystem $filesystem
112
     *
113
     * @return void
114
     */
115
    public function handle(Filesystem $filesystem, Dumper $dumper)
116
    {
117
        $this->info('Start Database Backup');
118
119
        $driver = dbm_driver();
120
121
        // $hostname = config('database.connections.' . $driver . '.host', '127.0.0.1');
122
        // $port     = config('database.connections.' . $driver . '.port', '3306');
123
        $database = config('database.connections.' . $driver . '.database', 'dbm');
124
        $table    = ($this->option('table') != null) ? $this->option('table') : '';
125
        // $username = config('database.connections.' . $driver . '.username', 'root');
126
        // $password = config('database.connections.' . $driver . '.password', '');
127
128
        try
129
        {
130
            $directory = (config('dbm.backup.dir', 'backups') != '')
131
            ? DIRECTORY_SEPARATOR . config('dbm.backup.dir', 'backups')
132
            : '';
133
            $directoryPath = DBM::getPathPrefix() . $directory . DIRECTORY_SEPARATOR . $driver;
134
            $filePath      = $directoryPath . DIRECTORY_SEPARATOR . $this->getFileName($table, $database);
135
136
            if (!File::isDirectory($directoryPath)) {
137
138
                File::makeDirectory($directoryPath, 0777, true, true);
139
140
            }
141
142
            $this->backup($dumper, $filePath);
143
144
            $this->info("Backup completed");
145
146
        } catch (\Exception $e) {
147
148
            throw new \Exception($e->getMessage(), 1);
149
150
        }
151
152
    }
153
}
154