SessionController::getQuery()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 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