RuleController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace yii2mod\rbac\controllers;
4
5
use Yii;
6
use yii\filters\VerbFilter;
7
use yii\web\Controller;
8
use yii\web\NotFoundHttpException;
9
use yii2mod\rbac\models\BizRuleModel;
10
use yii2mod\rbac\models\search\BizRuleSearch;
11
12
/**
13
 * Class RuleController
14
 *
15
 * @package yii2mod\rbac\controllers
16
 */
17
class RuleController extends Controller
18
{
19
    /**
20
     * @var string search class name for rules search
21
     */
22
    public $searchClass = [
23
        'class' => BizRuleSearch::class,
24
    ];
25
26
    /**
27
     * Returns a list of behaviors that this component should behave as.
28
     *
29
     * @return array
30
     */
31
    public function behaviors(): array
32
    {
33
        return [
34
            'verbs' => [
35
                'class' => VerbFilter::class,
36
                'actions' => [
37
                    'index' => ['get'],
38
                    'view' => ['get'],
39
                    'create' => ['get', 'post'],
40
                    'update' => ['get', 'post'],
41
                    'delete' => ['post'],
42
                ],
43
            ],
44
        ];
45
    }
46
47
    /**
48
     * List of all rules
49
     *
50
     * @return mixed
51
     */
52
    public function actionIndex()
53
    {
54
        $searchModel = Yii::createObject($this->searchClass);
55
        $dataProvider = $searchModel->search(Yii::$app->request->getQueryParams());
56
57
        return $this->render('index', [
58
            'dataProvider' => $dataProvider,
59
            'searchModel' => $searchModel,
60
        ]);
61
    }
62
63
    /**
64
     * Displays a single Rule item.
65
     *
66
     * @param string $id
67
     *
68
     * @return mixed
69
     */
70
    public function actionView(string $id)
71
    {
72
        $model = $this->findModel($id);
73
74
        return $this->render('view', ['model' => $model]);
75
    }
76
77
    /**
78
     * Creates a new Rule item.
79
     *
80
     * If creation is successful, the browser will be redirected to the 'view' page.
81
     *
82
     * @return mixed
83
     */
84 View Code Duplication
    public function actionCreate()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $model = new BizRuleModel();
87
88
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
89
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.'));
90
91
            return $this->redirect(['view', 'id' => $model->name]);
92
        }
93
94
        return $this->render('create', ['model' => $model]);
95
    }
96
97
    /**
98
     * Updates an existing Rule item.
99
     *
100
     * If update is successful, the browser will be redirected to the 'view' page.
101
     *
102
     * @param string $id
103
     *
104
     * @return mixed
105
     */
106 View Code Duplication
    public function actionUpdate(string $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $model = $this->findModel($id);
109
110
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
111
            Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been saved.'));
112
113
            return $this->redirect(['view', 'id' => $model->name]);
114
        }
115
116
        return $this->render('update', ['model' => $model]);
117
    }
118
119
    /**
120
     * Deletes an existing Rule item.
121
     *
122
     * If deletion is successful, the browser will be redirected to the 'index' page.
123
     *
124
     * @param string $id
125
     *
126
     * @return mixed
127
     */
128 View Code Duplication
    public function actionDelete(string $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        $model = $this->findModel($id);
131
        Yii::$app->authManager->remove($model->item);
0 ignored issues
show
Documentation introduced by
The property item does not exist on object<yii2mod\rbac\models\BizRuleModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
132
        Yii::$app->session->setFlash('success', Yii::t('yii2mod.rbac', 'Rule has been deleted.'));
133
134
        return $this->redirect(['index']);
135
    }
136
137
    /**
138
     * Finds the BizRuleModel based on its primary key value.
139
     *
140
     * If the model is not found, a 404 HTTP exception will be thrown.
141
     *
142
     * @param string $id
143
     *
144
     * @return BizRuleModel the loaded model
145
     *
146
     * @throws \yii\web\NotFoundHttpException
147
     */
148
    protected function findModel(string $id)
149
    {
150
        $item = Yii::$app->authManager->getRule($id);
151
152
        if (!empty($item)) {
153
            return new BizRuleModel($item);
154
        }
155
156
        throw new NotFoundHttpException(Yii::t('yii2mod.rbac', 'The requested page does not exist.'));
157
    }
158
}
159