Completed
Push — master ( c064eb...85769a )
by Igor
03:38
created

RolesController::actionEdit()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 11.4603

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 27
ccs 7
cts 15
cp 0.4667
rs 8.439
cc 6
eloc 16
nc 12
nop 1
crap 11.4603
1
<?php
2
3
namespace app\modules\admin\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\helpers\Url;
8
use app\traits\ModelTrait;
9
use app\models\AuthItem;
10
use app\modules\admin\models\forms\AuthItemForm;
11
use app\modules\admin\models\search\AuthItemSearch;
12
13
class RolesController extends \yii\web\Controller
14
{
15
    use ModelTrait;
16
17 4
    public function behaviors()
18
    {
19
        return [
20 4
            'verbs' => [
21
                'class' => VerbFilter::class,
22
                'actions' => [
23
                    'delete' => ['post'],
24
                    'operations' => ['post'],
25
                ],
26
            ],
27
        ];
28
    }
29
30 4
    public function actions()
31
    {
32
        return [
33 4
            'operations' => [
34
                'class' => 'app\modules\admin\controllers\common\OperationsAction',
35
                'modelClass' => 'app\models\AuthItem',
36
                'operations' => [
37
                    'delete' => [],
38
                ]
39
            ],
40
            'delete' => [
41
                'class' => 'app\modules\admin\controllers\common\DeleteAction',
42
                'modelClass' => 'app\models\AuthItem',
43
            ],
44
        ];
45
    }
46
47 4
    public function actionIndex()
48
    {
49 4
        $authItemSearch = new AuthItemSearch();
50 4
        $dataProvider = $authItemSearch->search(Yii::$app->request->get());
51
52 4
        return $this->render('index', [
53 4
            'authItemSearch' => $authItemSearch,
54 4
            'dataProvider' => $dataProvider,
55
        ]);
56
    }
57
58 3
    public function actionEdit($name = null)
59
    {
60 3
        $model = new AuthItemForm();
61
62 3
        if ($name) {
63 2
            $model->setModel($this->findModel(new AuthItem, $name));
0 ignored issues
show
Compatibility introduced by
$this->findModel(new \ap...dels\AuthItem(), $name) of type object<yii\db\ActiveRecord> is not a sub-type of object<app\models\AuthItem>. It seems like you assume a child class of the class yii\db\ActiveRecord to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
64
        }
65
66 2
        if (Yii::$app->request->isPost) {
67
            if ($model->load(Yii::$app->request->post()) && $model->validate()) {
68
                try {
69
                    $model->save();
70
71
                    Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Saved successfully'));
72
                    return $this->asJson(['redirect' => Url::toRoute(['edit', 'name' => $model->name])]);
73
                } catch (\Exception $e) {
74
                    Yii::error($e);
75
                    return $this->asJson(false);
76
                }
77
            }
78
            return $this->asJson($this->collectErrors($model));
79
        }
80
81 2
        return $this->render('edit', [
82 2
            'model' => $model,
83
        ]);
84
    }
85
}
86