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) { |
|
|
|
|
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
|
|
|
|
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.