DashboardController   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 2
cbo 8
dl 0
loc 168
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 7 1
A behaviors() 0 23 2
A actionIndex() 0 10 1
A actionView() 0 7 2
A actionCreate() 0 16 4
B actionUpdate() 0 18 5
A actionDelete() 0 7 1
A actionSort() 0 14 4
A findModel() 0 12 3
1
<?php
2
3
namespace cornernote\dashboard\controllers;
4
5
use cornernote\dashboard\models\Dashboard;
6
use cornernote\dashboard\models\search\DashboardSearch;
7
use cornernote\dashboard\Module;
8
use yii\filters\AccessControl;
9
use yii\web\Controller;
10
use Yii;
11
use yii\web\HttpException;
12
use cornernote\dashboard\components\DashboardAccess;
13
14
/**
15
 * DashboardController implements the CRUD actions for Dashboard model.
16
 */
17
class DashboardController extends Controller
18
{
19
20
    public function init()
21
    {
22
        parent::init();
23
        $this->viewPath = Module::getInstance()->viewPath;
24
25
        // custom initialization code goes here
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function behaviors()
32
	{
33
		if (!$updateRoles = Module::getInstance()->updateRoles) {
34
			return [];
35
		}
36
		return [
37
			'access' => [
38
				'class' => AccessControl::className(),
39
				'rules' => [
40
					[
41
						'allow' => true,
42
						'actions' => ['view'],
43
						'roles' => ['@']
44
					],
45
					[
46
						'allow' => true,
47
						'actions' => ['index', 'create', 'update', 'delete'],
48
						'roles' => $updateRoles
49
					]
50
				]
51
			]
52
		];
53
	}
54
55
	/**
56
     * Lists all Dashboard models.
57
     * @return mixed
58
     */
59
    public function actionIndex()
60
    {
61
        $searchModel = new DashboardSearch;
62
        $dataProvider = $searchModel->search(Yii::$app->request->get());
63
64
        return $this->render('index', [
65
            'dataProvider' => $dataProvider,
66
            'searchModel' => $searchModel,
67
        ]);
68
    }
69
70
    /**
71
     * Displays a single Dashboard model.
72
     * @param string $id
73
     *
74
     * @return mixed
75
     */
76
    public function actionView($id)
77
    {
78
        if(!$model = $this->findModel($id)){
79
            return $this->render('empty');
80
        }
81
        return $this->render('view', compact('model'));
82
    }
83
84
    /**
85
     * Creates a new Dashboard model.
86
     * If creation is successful, the browser will be redirected to the 'view' page.
87
     * @return mixed
88
     */
89
    public function actionCreate()
90
    {
91
        $model = new Dashboard;
92
        //$model->scenario = 'create';
93
94
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
95
            Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard has been created.'));
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...
96
            return $this->redirect(['update', 'id' => $model->id]);
97
        }
98
99
        if (!\Yii::$app->request->isPost) {
100
            $model->load(Yii::$app->request->get());
101
        }
102
103
        return $this->render('create', compact('model'));
104
    }
105
106
    /**
107
     * Updates an existing Dashboard model.
108
     * If update is successful, the browser will be redirected to the 'view' page.
109
     * @param string $id
110
     * @return mixed
111
     */
112
    public function actionUpdate($id)
113
    {
114
        $model = $this->findModel($id);
115
        //$model->scenario = 'update';
116
117
        $data = Yii::$app->request->post();
118
        if ($data && $model->layout->load($data) && $model->layout->validate()) {
119
            $model->options = $model->layout->getOptions();
120
            if ($model->save(false)) {
0 ignored issues
show
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...
121
                $model->sortPanels($data['DashboardPanelSort']);
0 ignored issues
show
Bug introduced by
The method sortPanels 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...
122
                Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard has been updated.'));
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...
123
                return $this->redirect(['dashboard/view', 'id' => $model->id]);
124
            }
125
        }
126
127
128
        return $this->render('update', compact('model'));
129
    }
130
131
132
    /**
133
     * Deletes an existing Dashboard model.
134
     * If deletion is successful, the browser will be redirected to the 'index' page.
135
     * @param string $id
136
     * @return mixed
137
     */
138
    public function actionDelete($id)
139
    {
140
        $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...
141
        Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard has been deleted.'));
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...
142
143
        return $this->redirect(['index']);
144
    }
145
146
    /**
147
     *
148
     */
149
    public function actionSort()
150
    {
151
        $sort = Yii::$app->request->post('sort');
152
        if (!empty($sort)) {
153
            foreach ($sort as $k => $dashboardId) {
154
                $dashboardId = str_replace('dashboard-', '', $dashboardId);
155
                $dashboard = Dashboard::findOne($dashboardId);
156
                if ($dashboard) {
157
                    $dashboard->sort = $k;
158
                    $dashboard->save(false);
0 ignored issues
show
Bug introduced by
The method save cannot be called on $dashboard (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...
159
                }
160
            }
161
        }
162
    }
163
164
    /**
165
     * Finds the Dashboard model based on its primary key value.
166
     * If the model is not found, a 404 HTTP exception will be thrown.
167
	 * If defined for dashboard allowRoles, check user access
168
     * @param string $id
169
     * @return Dashboard|false the loaded model
170
     * @throws HttpException if the model cannot be found
171
     */
172
    protected function findModel($id)
173
    {
174
        if (($model = Dashboard::findOne($id)) !== null) {
175
176
			if (!DashboardAccess::userHasAccess($model->name)) {
177
                return false;
178
			}
179
180
			return $model;
181
		}
182
        throw new HttpException(404, 'The requested page does not exist.');
183
    }
184
}
185