Completed
Push — master ( 55e0f0...d0f882 )
by Alexey
11:22
created

UserSearch::search()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 8.7433
c 0
b 0
f 0
cc 5
eloc 35
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace modules\users\models\search;
3
4
use Yii;
5
use yii\base\Model;
6
use yii\data\ActiveDataProvider;
7
use modules\users\models\User;
8
9
/**
10
 * Class UserSearch
11
 * @package modules\users\models\backend
12
 */
13
class UserSearch extends User
14
{
15
    public $userRoleName;
16
    public $date_from;
17
    public $date_to;
18
    public $pageSize;
19
20
    public function init()
21
    {
22
        parent::init();
23
        $this->pageSize = Yii::$app->params['user.pageSize'];
24
    }
25
26
    /**
27
     * @inheritdoc
28
     */
29
    public function rules()
30
    {
31
        return [
32
            [['id', 'status', 'pageSize'], 'integer'],
33
            [['username', 'email', 'role', 'userRoleName', 'date_from', 'date_to'], 'safe'],
34
        ];
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function scenarios()
41
    {
42
        // bypass scenarios() implementation in the parent class
43
        return Model::scenarios();
44
    }
45
46
    /**
47
     * Creates data provider instance with search query applied
48
     *
49
     * @param array $params
50
     *
51
     * @return ActiveDataProvider
52
     */
53
    public function search($params)
54
    {
55
        $query = User::find();
56
57
        // add conditions that should always apply here
58
        $query->leftJoin('{{%auth_assignment}}', '{{%auth_assignment}}.user_id = {{%user}}.id');
59
60
        $dataProvider = new ActiveDataProvider([
61
            'query' => $query,
62
            'sort' => [
63
                'defaultOrder' => ['id' => SORT_ASC],
64
            ],
65
        ]);
66
67
        $dataProvider->setSort([
68
            'attributes' => [
69
                'id',
70
                'username',
71
                'email',
72
                'status',
73
                'userRoleName' => [
74
                    'asc' => ['item_name' => SORT_ASC],
75
                    'desc' => ['item_name' => SORT_DESC],
76
                    'default' => SORT_ASC,
77
                    'label' => 'Role Name',
78
                ],
79
                'last_visit'
80
            ]
81
        ]);
82
83
        $this->load($params);
84
85
        if (!$this->validate()) {
86
            // uncomment the following line if you do not want to return any records when validation fails
87
            $query->where('0=1');
88
            return $dataProvider;
89
        }
90
91
        // grid filtering conditions
92
        $query->andFilterWhere([
93
            'id' => $this->id,
94
            'status' => $this->status,
95
        ]);
96
97
        $query->andFilterWhere(['like', 'username', $this->username])
98
            ->andFilterWhere(['like', 'email', $this->email])
99
            ->andFilterWhere(['like', 'item_name', $this->userRoleName])
100
            ->andFilterWhere(['>=', 'last_visit', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
101
            ->andFilterWhere(['<=', 'last_visit', $this->date_to ? strtotime($this->date_to . ' 23:59:59') : null]);
102
103
        if ($this->pageSize) {
104
            $dataProvider->pagination->pageSize = $this->pageSize;
105
        }
106
107
        $dataProvider->pagination->totalCount = $query->count();
108
        return $dataProvider;
109
    }
110
}
111