DeviceController::actionView()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace app\controllers;
4
5
use Yii;
6
use app\models\Device;
7
use app\models\Screen;
8
use yii\helpers\ArrayHelper;
9
use yii\data\ActiveDataProvider;
10
use yii\web\NotFoundHttpException;
11
use yii\filters\VerbFilter;
12
use yii\filters\AccessControl;
13
14
/**
15
 * ScreenController implements the CRUD actions for Device model.
16
 */
17
class DeviceController extends BaseController
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 View Code Duplication
    public function behaviors()
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...
23
    {
24
        return [
25
            'verbs' => [
26
                'class' => VerbFilter::class,
27
                'actions' => [
28
                    'delete' => ['POST'],
29
                ],
30
            ],
31
            'access' => [
32
                'class' => AccessControl::class,
33
                'only' => ['index', 'view', 'create', 'update', 'delete', 'link', 'unlink'],
34
                'rules' => [
35
                    ['allow' => true, 'actions' => ['index', 'view', 'create', 'update', 'delete', 'link', 'unlink'], 'roles' => ['setDevices']],
36
                ],
37
            ],
38
        ];
39
    }
40
41
    /**
42
     * Lists all Device models.
43
     *
44
     * @return string
45
     */
46 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...
47
    {
48
        $dataProvider = new ActiveDataProvider([
49
            'query' => Device::find(),
50
        ]);
51
52
        return $this->render('index', [
53
            'dataProvider' => $dataProvider,
54
        ]);
55
    }
56
57
    /**
58
     * Displays a single Device model.
59
     *
60
     * @param int $id
61
     *
62
     * @return string
63
     */
64
    public function actionView($id)
65
    {
66
        $model = $this->findModel($id);
67
68
        $dataProvider = new ActiveDataProvider([
69
            'query' => $model->getScreens(),
70
        ]);
71
72
        return $this->render('view', [
73
            'model' => $model,
74
            'dataProvider' => $dataProvider,
75
        ]);
76
    }
77
78
    /**
79
     * Updates an existing Device model.
80
     * If update is successful, the browser will be redirected to the 'view' page.
81
     *
82
     * @param int $id
83
     *
84
     * @return \yii\web\Response|string redirect or render
85
     */
86
    public function actionUpdate($id)
87
    {
88
        $model = $this->findModel($id);
89
90
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
91
            return $this->redirect(['view', 'id' => $model->id]);
92
        } else {
93
            return $this->render('update', [
94
                'model' => $model,
95
            ]);
96
        }
97
    }
98
99
    /**
100
     * Deletes an existing Device model.
101
     * If deletion is successful, the browser will be redirected to the 'index' page.
102
     *
103
     * @param int $id
104
     *
105
     * @return \yii\web\Response
106
     */
107
    public function actionDelete($id)
108
    {
109
        $this->findModel($id)->delete();
110
111
        return $this->redirect(['index']);
112
    }
113
114
    /**
115
     * Adds a Screen to this Device or render link view.
116
     *
117
     * @param int $id
118
     * @param int $screenId
119
     *
120
     * @return \yii\web\Response|string redirec or render
121
     */
122 View Code Duplication
    public function actionLink($id, $screenId = null)
123
    {
124
        $model = $this->findModel($id);
125
126
        if ($screenId === null) {
127
            $dataProvider = new ActiveDataProvider([
128
                'query' => Screen::find()->where(['not', ['id' => ArrayHelper::getColumn($model->screens, 'id')]]),
129
            ]);
130
131
            return $this->render('link', [
132
                'model' => $model,
133
                'dataProvider' => $dataProvider,
134
            ]);
135
        } else {
136
            if (!$model->getScreens()->where(['id' => $screenId])->exists() && ($screen = Screen::findOne($screenId)) !== null) {
137
                $model->link('screens', $screen);
0 ignored issues
show
Bug introduced by
It seems like $screen defined by \app\models\Screen::findOne($screenId) on line 136 can also be of type array; however, yii\db\BaseActiveRecord::link() does only seem to accept object<yii\db\ActiveRecordInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
138
            }
139
140
            return $this->redirect(['view', 'id' => $id]);
141
        }
142
    }
143
144
    /**
145
     * Remove a Screen from a Device.
146
     *
147
     * @param int $id
148
     * @param int $screenId
149
     *
150
     * @return \yii\web\Response
151
     */
152 View Code Duplication
    public function actionUnlink($id, $screenId)
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...
153
    {
154
        $model = $this->findModel($id);
155
156
        if ($model->getScreens()->where(['id' => $screenId])->exists() && ($screen = Screen::findOne($screenId)) !== null) {
157
            $model->unlink('screens', $screen, true);
0 ignored issues
show
Bug introduced by
It seems like $screen defined by \app\models\Screen::findOne($screenId) on line 156 can also be of type array; however, yii\db\BaseActiveRecord::unlink() does only seem to accept object<yii\db\ActiveRecordInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
158
        }
159
160
        return $this->redirect(['view', 'id' => $id]);
161
    }
162
163
    /**
164
     * Enables or disables a Device.
165
     *
166
     * @param int $id screen id
167
     *
168
     * @return \yii\web\Response
169
     */
170 View Code Duplication
    public function actionToggle($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...
171
    {
172
        $model = $this->findModel($id);
173
174
        $model->enabled = !$model->enabled;
175
        $model->save();
176
177
        return $this->smartGoBack();
178
    }
179
180
    /**
181
     * Finds the Device model based on its primary key value.
182
     * If the model is not found, a 404 HTTP exception will be thrown.
183
     *
184
     * @param int $id
185
     *
186
     * @return Device the loaded model
187
     *
188
     * @throws NotFoundHttpException if the model cannot be found
189
     */
190 View Code Duplication
    protected function findModel($id)
191
    {
192
        if (($model = Device::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \app\models\Device::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 193 which is incompatible with the return type documented by app\controllers\DeviceController::findModel of type app\models\Device.
Loading history...
193
            return $model;
194
        } else {
195
            throw new NotFoundHttpException(Yii::t('app', 'The requested device does not exist.'));
196
        }
197
    }
198
}
199