1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: floor12 |
5
|
|
|
* Date: 18.09.2018 |
6
|
|
|
* Time: 20:35 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace floor12\backup\logic; |
10
|
|
|
|
11
|
|
|
use ErrorException; |
12
|
|
|
use Exception; |
13
|
|
|
use floor12\backup\Exceptions\ConfigurationNotFoundException; |
14
|
|
|
use floor12\backup\Exceptions\ModuleNotConfiguredException; |
15
|
|
|
use floor12\backup\models\Backup; |
16
|
|
|
use floor12\backup\models\BackupType; |
17
|
|
|
use floor12\backup\models\IOPriority; |
18
|
|
|
use Yii; |
19
|
|
|
use yii\base\InvalidConfigException; |
20
|
|
|
use yii\db\Connection; |
21
|
|
|
|
22
|
|
|
class BackupRestore |
23
|
|
|
{ |
24
|
|
|
/** @var array */ |
25
|
|
|
private $configs = []; |
26
|
|
|
/** @var array */ |
27
|
|
|
private $currentConfig = []; |
28
|
|
|
/** @var string */ |
29
|
|
|
private $config_id; |
30
|
|
|
/** @var Backup */ |
31
|
|
|
private $model; |
32
|
|
|
/** @var Connection */ |
33
|
|
|
private $connection; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* BackupRestore constructor. |
37
|
|
|
* @param Backup $model |
38
|
|
|
* @throws InvalidConfigException |
39
|
|
|
* @throws ErrorException |
40
|
|
|
*/ |
41
|
|
|
public function __construct(Backup $model) |
42
|
|
|
{ |
43
|
|
|
$this->config_id = $model->config_id; |
44
|
|
|
$this->model = $model; |
45
|
|
|
$this->loadConfigs(); |
46
|
|
|
$this->setUpActiveConfig(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @throws ModuleNotConfiguredException |
52
|
|
|
*/ |
53
|
|
|
protected function loadConfigs() |
54
|
|
|
{ |
55
|
|
|
$this->configs = Yii::$app->getModule('backup')->configs; |
|
|
|
|
56
|
|
|
if (!is_array($this->configs) || !sizeof($this->configs)) |
57
|
|
|
throw new ModuleNotConfiguredException('Backup configs is empty'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $config_id |
62
|
|
|
* @throws ConfigurationNotFoundException |
63
|
|
|
*/ |
64
|
|
|
protected function setUpActiveConfig() |
65
|
|
|
{ |
66
|
|
|
if (!is_array($this->configs[$this->config_id])) |
67
|
|
|
throw new ConfigurationNotFoundException("Configuration `{$this->config_id}` not found."); |
68
|
|
|
$this->currentConfig = $this->configs[$this->config_id]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Ma |
73
|
|
|
* @return bool |
74
|
|
|
* @throws InvalidConfigException |
75
|
|
|
* @throws Exception |
76
|
|
|
*/ |
77
|
|
|
public function run() |
78
|
|
|
{ |
79
|
|
|
if (empty($this->currentConfig['io'])) |
80
|
|
|
$this->currentConfig['io'] = IOPriority::IDLE; |
81
|
|
|
|
82
|
|
|
if ($this->currentConfig['type'] == BackupType::DB) { |
83
|
|
|
$this->connection = Yii::$app->{$this->currentConfig['connection']}; |
84
|
|
|
return Yii::createObject(DatabaseBackuper::class, [$this->model->getFullPath(), $this->connection, $this->currentConfig['io']]) |
85
|
|
|
->restore(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($this->currentConfig['type'] == BackupType::FILES) { |
89
|
|
|
$targetFolder = Yii::getAlias($this->currentConfig['path']); |
90
|
|
|
return Yii::createObject(FolderBackupRestorer::class, [$this->model->getFullPath(), $targetFolder, $this->currentConfig['io']])->execute(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.