DashboardPanelController   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 15
c 3
b 0
f 0
lcom 1
cbo 7
dl 0
loc 111
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A actionCreate() 0 14 4
B actionUpdate() 0 16 5
A actionDelete() 0 8 1
A findModel() 0 13 3
A behaviors() 0 23 2
1
<?php
2
3
namespace cornernote\dashboard\controllers;
4
5
use cornernote\dashboard\models\DashboardPanel;
6
use cornernote\dashboard\models\search\DashboardPanelSearch;
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\DashboardPanelAccess;
13
14
/**
15
 * DashboardPanelController implements the CRUD actions for DashboardPanel model.
16
 */
17
class DashboardPanelController extends Controller
18
{
19
20
    /**
21
	 * @inheritdoc
22
	 */
23
	public function behaviors()
24
	{
25
		if (!$updateRoles = Module::getInstance()->updateRoles) {
26
			return [];
27
		}
28
		return [
29
			'access' => [
30
				'class' => AccessControl::className(),
31
				'rules' => [
32
					[
33
						'allow' => true,
34
						'actions' => ['view'],
35
						'roles' => ['@']
36
					],
37
					[
38
						'allow' => true,
39
						'actions' => ['index', 'create', 'update', 'delete'],
40
						'roles' => $updateRoles
41
					]
42
				]
43
			]
44
		];
45
	}
46
47
	/**
48
     * Creates a new DashboardPanel model.
49
     * If creation is successful, the browser will be redirected to the 'view' page.
50
     * @return mixed
51
     */
52
    public function actionCreate()
53
    {
54
        $model = new DashboardPanel;
55
        //$model->scenario = 'create';
56
57
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
58
            Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard Panel 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...
59
            return $this->redirect(['update', 'id' => $model->id]);
60
        } elseif (!\Yii::$app->request->isPost) {
61
            $model->load(Yii::$app->request->get());
62
        }
63
64
        return $this->render('create', compact('model'));
65
    }
66
67
    /**
68
     * Updates an existing DashboardPanel model.
69
     * If update is successful, the browser will be redirected to the 'view' page.
70
     * @param string $id
71
     * @return mixed
72
     */
73
    public function actionUpdate($id)
74
    {
75
        $model = $this->findModel($id);
76
        //$model->scenario = 'update';
77
78
        $data = Yii::$app->request->post();
79
        if ($data && $model->panel->load($data) && $model->panel->validate()) {
80
            $model->options = $model->panel->getOptions();
81
            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...
82
                Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard Panel 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...
83
                return $this->redirect(['dashboard/view', 'id' => $model->dashboard->id]);
84
            }
85
        }
86
87
        return $this->render('update', compact('model'));
88
    }
89
90
91
    /**
92
     * Deletes an existing DashboardPanel model.
93
     * If deletion is successful, the browser will be redirected to the 'index' page.
94
     * @param string $id
95
     * @return mixed
96
     */
97
    public function actionDelete($id)
98
    {
99
        $model = $this->findModel($id);
100
        $model->delete();
0 ignored issues
show
Bug introduced by
The method delete 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...
101
        Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Dashboard Panel 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...
102
103
        return $this->redirect(['dashboard/view', 'id' => $model->dashboard_id]);
104
    }
105
106
    /**
107
     * Finds the DashboardPanel model based on its primary key value.
108
     * If the model is not found, a 404 HTTP exception will be thrown.
109
	 * If defined for panel allowRoles, check user access
110
     * @param string $id
111
     * @return DashboardPanel the loaded model
112
     * @throws HttpException if the model cannot be found
113
     */
114
    protected function findModel($id)
115
    {
116
117
		if (($model = DashboardPanel::findOne($id)) !== null) {
118
119
			if (!DashboardPanelAccess::userHasAccess($model->name)) {
120
				throw new HttpException(401, 'You are not allowed to access this page.');
121
			}
122
			return $model;
123
		}
124
125
		throw new HttpException(404, 'The requested page does not exist.');
126
	}
127
}
128