AdminController::getBackup()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: floor12
5
 * Date: 17.09.2018
6
 * Time: 23:07
7
 */
8
9
namespace floor12\backup\controllers;
10
11
use floor12\backup\logic\BackupCreate;
12
use floor12\backup\logic\BackupRestore;
13
use floor12\backup\models\Backup;
14
use floor12\backup\models\BackupFilter;
15
use floor12\backup\models\ImportForm;
16
use Throwable;
17
use Yii;
18
use yii\base\InvalidConfigException;
19
use yii\db\StaleObjectException;
20
use yii\filters\AccessControl;
21
use yii\filters\VerbFilter;
22
use yii\web\BadRequestHttpException;
23
use yii\web\Controller;
24
use yii\web\NotFoundHttpException;
25
use yii\web\UploadedFile;
26
27
class AdminController extends Controller
28
{
29
30
    /**
31
     * @var Backup
32
     */
33
    protected $model;
34
35
    /**
36
     * @inheritDoc
37
     * @return array
38
     */
39
    public function behaviors()
40
    {
41
        return [
42
            'access' => [
43
                'class' => AccessControl::class,
44
                'rules' => [
45
                    [
46
                        'allow' => true,
47
                        'roles' => [Yii::$app->getModule('backup')->administratorRoleName],
48
                    ],
49
                ],
50
            ],
51
            'verbs' => [
52
                'class' => VerbFilter::class,
53
                'actions' => [
54
                    'index' => ['get'],
55
                    'delete' => ['delete'],
56
                    'backup' => ['post'],
57
                    'restore' => ['post'],
58
                    'import' => ['post'],
59
                ],
60
            ],
61
        ];
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function init()
68
    {
69
        $this->layout = Yii::$app->getModule('backup')->adminLayout;
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->getModule('backup')->adminLayout can also be of type object. However, the property $layout is declared as type false|null|string. 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...
70
        parent::init();
71
    }
72
73
    /**
74
     * @return string
75
     */
76
    public function actionIndex()
77
    {
78
        $model = new BackupFilter();
79
        return $this->render('index', [
80
            'model' => $model,
81
            'configs' => Yii::$app->getModule('backup')->configs ?: []
82
        ]);
83
    }
84
85
    /**
86
     * @return string
87
     * @throws NotFoundHttpException
88
     * @throws Throwable
89
     * @throws StaleObjectException
90
     */
91
    public function actionDelete()
92
    {
93
        $this->getBackup((int)Yii::$app->request->post('id'));
94
        $this->model->delete();
95
        $this->model->delete();
96
    }
97
98
    /**
99
     * @throws InvalidConfigException
100
     * @throws NotFoundHttpException
101
     * @throws StaleObjectException
102
     * @throws Throwable
103
     */
104
    public function actionBackup()
105
    {
106
        $config_id = Yii::$app->request->post('config_id');
107
        Yii::createObject(BackupCreate::class, [$config_id])->run();
108
    }
109
110
    /**
111
     * @throws InvalidConfigException
112
     * @throws NotFoundHttpException
113
     */
114
    public function actionRestore()
115
    {
116
        $this->getBackup((int)Yii::$app->request->post('id'));
117
        Yii::createObject(BackupRestore::class, [$this->model])->run();
118
    }
119
120
    /**
121
     * @param $id
122
     * @throws NotFoundHttpException
123
     */
124
    public function actionDownload($id)
125
    {
126
        $this->getBackup((int)$id);
127
        Yii::$app->response->sendFile($this->model->getFullPath());
128
    }
129
130
    /**
131
     * @param $config_id
132
     * @return string
133
     */
134
    public function actionImport()
135
    {
136
        $model = new ImportForm();
137
        $model->load(Yii::$app->request->post());
0 ignored issues
show
Bug introduced by
It seems like Yii::app->request->post() can also be of type object; however, parameter $data of yii\base\Model::load() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

137
        $model->load(/** @scrutinizer ignore-type */ Yii::$app->request->post());
Loading history...
138
        $model->file = UploadedFile::getInstance($model, 'file');
139
        if (!$model->import()) {
140
            throw new BadRequestHttpException($model->getFirstError('file'));
141
        }
142
    }
143
144
    /**
145
     * @param int $id
146
     * @throws NotFoundHttpException
147
     */
148
    protected function getBackup(int $id)
149
    {
150
        $this->model = Backup::findOne($id);
151
        if (!$this->model)
152
            throw new NotFoundHttpException(Yii::t('app.f12.backup', 'Backup is not found.'));
153
    }
154
155
}
156