DefaultController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 0
loc 79
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A behaviors() 0 12 1
A actionIndex() 0 18 2
A actionView() 0 6 1
A actionDelete() 0 6 1
A findModel() 0 8 2
1
<?php
2
3
namespace monsterhunter\yii2\log\controllers;
4
5
use monsterhunter\yii2\log\models\search\SystemLogSearch;
6
use monsterhunter\yii2\log\models\SystemLog;
7
use yii;
8
use yii\filters\VerbFilter;
9
use yii\web\Controller;
10
use yii\web\NotFoundHttpException;
11
12
/**
13
 * LogController implements the CRUD actions for SystemLog models.
14
 */
15
class DefaultController extends Controller
16
{
17
    public function behaviors()
18
    {
19
        return [
20
            'verbs' => [
21
                'class' => VerbFilter::className(),
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
                'actions' => [
23
                    'delete' => ['post'],
24
                    'clear' => ['post'],
25
                ],
26
            ],
27
        ];
28
    }
29
30
    /**
31
     * Lists all SystemLog models.
32
     * @return mixed
33
     */
34
    public function actionIndex()
35
    {
36
        $searchModel = new SystemLogSearch();
37
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
38
39
        if (strcasecmp(Yii::$app->request->method, 'delete') == 0) {
40
            SystemLog::deleteAll($dataProvider->query->where);
0 ignored issues
show
Bug introduced by
Accessing where on the interface yii\db\QueryInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
41
            return $this->refresh();
42
        }
43
        $dataProvider->sort = [
44
            'defaultOrder' => ['log_time' => SORT_DESC]
45
        ];
46
47
        return $this->render('index', [
48
            'searchModel' => $searchModel,
49
            'dataProvider' => $dataProvider,
50
        ]);
51
    }
52
53
    /**
54
     * Displays a single SystemLog models.
55
     * @param integer $id
56
     * @return mixed
57
     */
58
    public function actionView($id)
59
    {
60
        return $this->render('view', [
61
            'model' => $this->findModel($id),
62
        ]);
63
    }
64
65
    /**
66
     * Deletes an existing SystemLog models.
67
     * If deletion is successful, the browser will be redirected to the 'index' page.
68
     * @param integer $id
69
     * @return mixed
70
     */
71
    public function actionDelete($id)
72
    {
73
        $this->findModel($id)->delete();
74
75
        return $this->redirect(['index']);
76
    }
77
78
    /**
79
     * Finds the SystemLog models based on its primary key value.
80
     * If the models is not found, a 404 HTTP exception will be thrown.
81
     * @param integer $id
82
     * @return SystemLog the loaded models
83
     * @throws NotFoundHttpException if the models cannot be found
84
     */
85
    protected function findModel($id)
86
    {
87
        if (($model = SystemLog::findOne($id)) !== null) {
0 ignored issues
show
Bug Compatibility introduced by
The expression \monsterhunter\yii2\log\...ystemLog::findOne($id); of type yii\db\ActiveRecordInterface|array|null adds the type array to the return on line 88 which is incompatible with the return type documented by monsterhunter\yii2\log\c...ltController::findModel of type monsterhunter\yii2\log\models\SystemLog.
Loading history...
88
            return $model;
89
        } else {
90
            throw new NotFoundHttpException('The requested page does not exist.');
91
        }
92
    }
93
}
94