Completed
Push — master ( d0bd4d...b4b0fd )
by Igor
23:04
created

UserSearch   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 55.56%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 58
c 1
b 0
f 0
ccs 10
cts 18
cp 0.5556
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 13 1
B search() 0 32 4
1
<?php
2
3
namespace app\modules\admin\models\search;
4
5
use yii\data\ActiveDataProvider;
6
use app\models\entity\User;
7
8
/**
9
 * UserSearch represents the model behind the search form about `app\models\entity\User`.
10
 */
11
class UserSearch extends User
12
{
13
    /**
14
     * @inheritdoc
15
     */
16 6
    public function rules()
17
    {
18
        return [
19
            [
20 6
                ['username', 'email', 'ip', 'role_name'], 'string'
21
            ],
22
23
            ['date_create', 'date', 'format' => 'yyyy-mm-dd'],
24
25
            ['status', 'integer'],
26 6
            ['status', 'in', 'range' => array_keys(User::getStatuses())],
27
        ];
28
    }
29
30
    /**
31
     * Search by request criteria.
32
     *
33
     * @param array|null Filter params.
34
     * @return ActiveDataProvider Data provider.
35
     */
36 6
    public function search($params)
37
    {
38 6
        $query = User::find()->with('role');
39
40 6
        $dataProvider = new ActiveDataProvider([
41 6
            'query' => $query,
42
            'sort' => [
43
                'defaultOrder' => [
44 6
                    'date_create' => SORT_DESC,
45
                ]
46
            ],
47
            'pagination' => [
48
                'pageSize' => 50,
49
            ],
50
        ]);
51
52 6
        if (!($this->load($params) && $this->validate())) {
53 6
            return $dataProvider;
54
        }
55
56
        $query->andFilterWhere([
57
            'ip' => !empty($this->ip) ? ip2long($this->ip) : null,
58
            'status' => $this->status,
59
            'role_name' => $this->role_name,
60
            'DATE(date_create)' => $this->date_create
61
        ]);
62
63
        $query->andFilterWhere(['like', 'user.username', $this->username]);
64
        $query->andFilterWhere(['like', 'user.email', $this->email]);
65
66
        return $dataProvider;
67
    }
68
}
69