BackupCommandRestore::getOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
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 PhpSpec\Exception\Exception;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Input\InputArgument;
8
9
class BackupCommandRestore extends BackupCommandAbstract
10
{
11
    /**
12
     * Name.
13
     *
14
     * @var string
15
     */
16
    protected $name = 'db:restore';
17
18
    /**
19
     * Description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Restore the default database from the given file';
24
25
    /**
26
     * Handle.
27
     *
28
     * @return void
29
     */
30
    public function handle(): void
31
    {
32
        $backupInstance = $this->getBackupInstance($this->input->getOption('database'));
33
        $backupInstance->setEnabled(true);
34
35 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...
36
            $backupInstance->setPath($this->input->getOption('path'));
37
        } else {
38
            $backupInstance->setPath('app/storage/backup/');
39
        }
40
41
        $selection = $this->choice(
42
            'Which database restoration file should be used?',
43
            $backupInstance->getRestorationFiles()
44
        );
45
46
        try {
47
            if (!$backupInstance->restore($selection)) {
48
                throw new Exception();
49
            }
50
        } catch (Exception $exception) {
51
            $this->error('An error occurred exporting the database.');
52
53
            return;
54
        }
55
56
        if (!$backupInstance->restore($selection)) {
57
            $this->error('An error occurred restoring the database.');
58
59
            return;
60
        }
61
62
        $this->info('Restored database from file: "' . $backupInstance->getWorkingFilepath() . '".');
63
    }
64
65
    /**
66
     * Get the console command arguments.
67
     *
68
     * @return array
69
     */
70
    protected function getArguments(): array
71
    {
72
        return [
73
            ['filename', InputArgument::OPTIONAL, 'Filepath for the database restoration file.']
74
        ];
75
    }
76
77
    /**
78
     * Get the console command options.
79
     *
80
     * @return array
81
     */
82
    protected function getOptions(): array
83
    {
84
        return [
85
            ['database', null, InputOption::VALUE_OPTIONAL, 'The database connection to backup'],
86
            ['path', null, InputOption::VALUE_OPTIONAL, 'The database export path']
87
        ];
88
    }
89
}
90