Test Failed
Push — master ( eaa667...0d9623 )
by Evgenii
38:52
created

BackupRestore::loadConfigs()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 3
nc 2
nop 0
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;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->getModule('backup')->configs can also be of type object. However, the property $configs is declared as type array. Maybe add an additional type check?

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 the id property of an instance of the Account 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.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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