SettingController::actionGroup()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace backend\controllers;
9
10
use common\models\Option;
11
use common\models\search\Option as OptionSearch;
12
use Yii;
13
use yii\filters\AccessControl;
14
use yii\filters\VerbFilter;
15
use yii\web\Controller;
16
use yii\web\NotFoundHttpException;
17
18
/**
19
 * SettingController implements the CRUD actions for Option model.
20
 *
21
 * @author Agiel K. Saputra <[email protected]>
22
 * @since 0.1.0
23
 */
24
class SettingController extends Controller
25
{
26
    /**
27
     * @inheritdoc
28
     */
29
    public function behaviors()
30
    {
31
        return [
32
33
            'access' => [
34
                'class' => AccessControl::className(),
35
                'rules' => [
36
                    [
37
                        'actions' => ['index', 'create', 'view', 'update', 'delete'],
38
                        'allow' => true,
39
                        'roles' => ['superadmin'],
40
                    ],
41
                    [
42
                        'actions' => ['group'],
43
                        'allow' => true,
44
                        'roles' => ['administrator'],
45
                    ],
46
                ],
47
            ],
48
            'verbs' => [
49
                'class' => VerbFilter::className(),
50
                'actions' => [
51
                    'delete' => ['post'],
52
                ],
53
            ],
54
        ];
55
    }
56
57
    /**
58
     * Lists all Option models.
59
     *
60
     * @return mixed
61
     */
62 View Code Duplication
    public function actionIndex()
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...
63
    {
64
        $searchModel = new OptionSearch();
65
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
66
67
        return $this->render('index', [
68
            'searchModel' => $searchModel,
69
            'dataProvider' => $dataProvider,
70
        ]);
71
    }
72
73
    /**
74
     * Displays single Option model.
75
     *
76
     * @param integer $id
77
     * @return mixed
78
     */
79
    public function actionView($id)
80
    {
81
        return $this->render('view', [
82
            'model' => $this->findModel($id),
83
        ]);
84
    }
85
86
    /**
87
     * Creates a new Option model.
88
     * If creation is successful, the browser will be redirected to the 'view' page.
89
     *
90
     * @return mixed
91
     */
92 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...
93
    {
94
        $model = new Option();
95
96
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
97
            return $this->redirect(['view', 'id' => $model->id]);
98
        }
99
100
        return $this->render('create', [
101
            'model' => $model,
102
        ]);
103
    }
104
105
    /**
106
     * Updates an existing Option model.
107
     * If update is successful, the browser will be redirected to the 'view' page.
108
     *
109
     * @param integer $id
110
     * @return mixed
111
     */
112 View Code Duplication
    public function actionUpdate($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...
113
    {
114
        $model = $this->findModel($id);
115
116
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
0 ignored issues
show
Bug introduced by
The method load cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
Bug introduced by
The method save cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
117
            return $this->redirect(['view', 'id' => $model->id]);
118
        }
119
120
        return $this->render('update', [
121
            'model' => $model,
122
        ]);
123
    }
124
125
    /**
126
     * Deletes an existing Option model.
127
     * If deletion is successful, the browser will be redirected to the 'index' page.
128
     *
129
     * @param integer $id
130
     * @return mixed
131
     */
132
    public function actionDelete($id)
133
    {
134
        $this->findModel($id)->delete();
0 ignored issues
show
Bug introduced by
The method delete cannot be called on $this->findModel($id) (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
135
136
        return $this->redirect(['index']);
137
    }
138
139
140
    /**
141
     * Render option page for specific group of option.
142
     * It will use group file if exist.
143
     *
144
     * @param string $id
145
     * @return mixed
146
     */
147
    public function actionGroup($id)
148
    {
149
        $model = $this->findModelByGroup($id);
150
151
        if ($options = Yii::$app->request->post('Option')) {
152
            foreach ($options as $optionName => $option) {
153
                Option::up($optionName, $option['value']);
154
            }
155
            Yii::$app->getSession()->setFlash('success', Yii::t('writesdown', 'Settings successfully saved.'));
0 ignored issues
show
Bug introduced by
The method getSession does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
156
157
            return $this->redirect(['group', 'id' => $id]);
158
        }
159
160
        if (is_file($this->getViewPath() . '/' . strtolower($id) . '.php')) {
161
            return $this->render(strtolower($id), [
162
                'model' => (object)$model,
163
                'group' => $id,
164
            ]);
165
        }
166
167
        return $this->redirect(['index']);
168
    }
169
170
    /**
171
     * Finds the Option model based on its primary key value.
172
     * If the model is not found, a 404 HTTP exception will be thrown.
173
     *
174
     * @param integer $id
175
     * @return Option the loaded model
176
     * @throws NotFoundHttpException if the model cannot be found
177
     */
178
    protected function findModel($id)
179
    {
180
        if (($model = Option::findOne($id)) !== null) {
181
            return $model;
182
        }
183
184
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
185
    }
186
187
    /**
188
     * Finds the Option models based on their group.
189
     * If the models are not found, a 404 HTTP exception will be thrown.
190
     *
191
     * @param string $id
192
     * @return Option the loaded model
193
     * @throws NotFoundHttpException if the models cannot be found
194
     */
195
    protected function findModelByGroup($id)
196
    {
197
        if ($model = Option::find()->where(['group' => $id])->indexBy('name')->all()) {
198
            return $model;
199
        }
200
201
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
202
    }
203
}
204