SessionController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 20
c 1
b 0
f 0
dl 0
loc 46
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getPermission() 0 2 1
A applyFilter() 0 10 4
A getQuery() 0 5 1
A actions() 0 9 1
A searchModel() 0 6 1
1
<?php
2
namespace App\Http\Api\Backend\Controller;
3
4
use App\Http\Api\Backend\Model\Session;
5
use Yii;
6
7
class SessionController extends ActiveController
8
{
9
    public $modelClass = Session::class;
10
11
    public function actions()
12
    {
13
        $actions = parent::actions();
14
15
        return [
16
            'index' => $actions['index'],
17
            'delete' => $actions['delete'],
18
            'view' => $actions['view'],
19
            'options' => $actions['options'],
20
        ];
21
    }
22
23
    public function getPermission($action)
24
    {
25
        // return 'userSession' . ucfirst($action);
26
    }
27
28
    public function searchModel()
29
    {
30
        return (new \yii\base\DynamicModel(['id', 'username' => null, 'email' => null, 'status' => null]))
31
            ->addRule(['id', 'status'], 'integer')
32
            ->addRule(['username', 'email'], 'trim')
33
            ->addRule(['username', 'email'], 'string');
34
    }
35
36
    protected function getQuery($action)
37
    {
38
        return parent::getQuery($action)
39
            ->alias('us')
40
            ->orderBy(['us.create_time' => SORT_DESC]);
41
    }
42
43
    protected function applyFilter($query, $model, $filter)
44
    {
45
        foreach (['id', 'username', 'email'] as $name) {
46
            if (!empty($model->$name)) {
47
                $query->andFilterWhere(['LIKE', 'u.' . $name, $model->$name]);
48
            }
49
        }
50
51
        if (is_numeric($model->status)) {
52
            $query->andWhere(['u.status' => intval($model->status)]);
53
        }
54
    }
55
}
56