ConsoleController   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 9
eloc 21
c 4
b 0
f 2
dl 0
loc 65
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A actionRestore() 0 8 2
A actionImport() 0 7 2
A actionIndex() 0 12 4
A actionBackup() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 18.09.2018
6
 * Time: 20:34
7
 */
8
9
namespace floor12\backup\controllers;
10
11
use ErrorException;
12
use floor12\backup\Exceptions\ConfigurationNotFoundException;
13
use floor12\backup\Exceptions\FileNotFoundException;
14
use floor12\backup\Exceptions\ModuleNotConfiguredException;
15
use floor12\backup\logic\BackupCreate;
16
use floor12\backup\logic\BackupImporter;
17
use floor12\backup\logic\BackupRestore;
18
use floor12\backup\models\Backup;
19
use floor12\backup\models\BackupType;
20
use Yii;
21
use yii\base\InvalidConfigException;
22
use yii\console\Controller;
23
use yii\helpers\Console;
24
25
/**
26
 * Floor12 backup module console controller
27
 *
28
 * Class ConsoleController
29
 * @package floor12\backup\controllers
30
 */
31
class ConsoleController extends Controller
32
{
33
    /**
34
     * Pass config_id to this command to create new backup.
35
     *
36
     * @param string $config_id
37
     * @throws InvalidConfigException
38
     */
39
    public function actionBackup(string $config_id)
40
    {
41
        Yii::createObject(BackupCreate::class, [$config_id])->run();
42
        $this->stdout('Backup created.' . PHP_EOL, Console::FG_GREEN);
43
    }
44
45
    /**
46
     * Pass existing backup ID to this command to restore from backup.
47
     *
48
     * @param string $backup_id
49
     * @throws ErrorException
50
     * @throws InvalidConfigException
51
     */
52
    public function actionRestore(string $backup_id)
53
    {
54
        $model = Backup::findOne((int)$backup_id);
55
        if (!$model)
0 ignored issues
show
introduced by
$model is of type yii\db\ActiveRecord, thus it always evaluated to true.
Loading history...
56
            throw new ErrorException('Backup not found.');
57
58
        Yii::createObject(BackupRestore::class, [$model])->run();
59
        $this->stdout('Backup restored.' . PHP_EOL, Console::FG_GREEN);
60
    }
61
62
    /**
63
     * List all created backups.
64
     *
65
     * @throws ErrorException
66
     * @throws InvalidConfigException
67
     */
68
    public function actionIndex()
69
    {
70
        $models = Backup::find()->orderBy('id DESC')->all();
71
72
        if (empty($models))
73
            return $this->stderr('Backups not found.' . PHP_EOL, Console::FG_YELLOW);
74
75
        foreach ($models as $model)
76
            $this->stdout("{$model->id}: " . Yii::$app->formatter->asDatetime($model->date) . "\t\t{$model->config_id}\t\t" .
77
                BackupType::$list[$model->type] .
78
                PHP_EOL,
79
                $model->status ? Console::FG_GREEN : Console::FG_RED);
80
    }
81
82
    /**
83
     * @param string $config_id
84
     * @param string $absoluteFilePath
85
     * @throws ConfigurationNotFoundException
86
     * @throws FileNotFoundException
87
     * @throws ModuleNotConfiguredException
88
     */
89
    public function actionImport(string $config_id, string $absoluteFilePath)
90
    {
91
        $importer = new BackupImporter($config_id, $absoluteFilePath);
92
        if ($importer->import())
93
            $this->stdout("Backup imported: {$absoluteFilePath}\n", Console::FG_GREEN);
94
        else
95
            $this->stdout("Something went wrong.\n", Console::FG_RED);
96
    }
97
}
98