BackupCommandExport::getArguments()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Cornford\Backup\Commands;
4
5
use Exception;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputArgument;
8
9
class BackupCommandExport extends BackupCommandAbstract
10
{
11
12
    /**
13
     * Name.
14
     *
15
     * @var string
16
     */
17
    protected $name = 'db:export';
18
19
    /**
20
     * Description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'Export the default database to `app/storage/backup`';
25
26
    /**
27
     * Handle.
28
     *
29
     * @return void
30
     */
31
    public function handle(): void
32
    {
33
        $backupInstance = $this->getBackupInstance($this->input->getOption('database'));
34
        $backupInstance->setEnabled(true);
35
36 View Code Duplication
        if ($this->input->getOption('path') !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
            $backupInstance->setPath($this->input->getOption('path'));
38
        } else {
39
            $backupInstance->setPath('app/storage/backup/');
40
        }
41
42
        if ($this->input->getOption('compress') !== null) {
43
            $backupInstance->setCompress($this->input->getOption('compress') != 'false' ?: false);
44
        }
45
46
        if ($this->argument('filename') !== null) {
47
            $backupInstance->setFilename($this->argument('filename'));
0 ignored issues
show
Bug introduced by
It seems like $this->argument('filename') targeting Illuminate\Console\Conce...ractsWithIO::argument() can also be of type array; however, Cornford\Backup\Contract...nterface::setFilename() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48
        }
49
50
        try {
51
            if (!$backupInstance->export()) {
52
                throw new Exception();
53
            }
54
        } catch (Exception $exception) {
55
            $this->error('An error occurred exporting the database.');
56
57
            return;
58
        }
59
60
        $this->info('Database exported to file: "' . $backupInstance->getWorkingFilepath() . '".');
61
    }
62
63
    /**
64
     * Get the console command arguments.
65
     *
66
     * @return array
67
     */
68
    protected function getArguments(): array
69
    {
70
        return [
71
            ['filename', InputArgument::OPTIONAL, 'Filename for the database export file.']
72
        ];
73
    }
74
75
    /**
76
     * Get the console command options.
77
     *
78
     * @return array
79
     */
80
    protected function getOptions(): array
81
    {
82
        return [
83
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to backup'],
84
            ['path', null, InputOption::VALUE_OPTIONAL, 'The database export path'],
85
            ['compress', null, InputOption::VALUE_OPTIONAL, 'Compress the database export using gzip'],
86
        ];
87
    }
88
}
89