UserSearch::search()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 74

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 8.5672
c 0
b 0
f 0
cc 4
nc 4
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
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use rhosocial\base\models\queries\BaseUserQuery;
16
use Yii;
17
use yii\base\Model;
18
use yii\data\ActiveDataProvider;
19
20
/**
21
 * Class UserSearchTrait
22
 *
23
 * @package rhosocial\user
24
 * @version 1.0
25
 * @author vistart <[email protected]>
26
 */
27
class UserSearch extends Model
28
{
29
    public $userClass = User::class;
30
    public static function find()
31
    {
32
        $noInit = new static;
33
        $class = $noInit->userClass;
34
        if (empty($class)) {
35
            return null;
36
        }
37
        return $class::find();
38
    }
39
    public $userAlias = 'u_alias';
40
    public $profileAlias = 'p_alias';
41
    public $id;
42
    public $nickname;
43
    public $first_name;
44
    public $last_name;
45
    /**
46
     * @var string
47
     */
48
    public $createdFrom;
49
    protected $createdFromInUtc;
50
51
    /**
52
     * @var string
53
     */
54
    public $createdTo;
55
    protected $createdToInUtc;
56
57
    /**
58
     * @var string Gender filter.
59
     */
60
    public $gf;
61
62
    /**
63
     *
64
     * @return array
65
     */
66
    public function rules()
67
    {
68
        return [
69
            ['id', 'integer'],
70
            [['nickname', 'first_name', 'last_name'], 'string'],
71
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
72
            [['createdFrom', 'createdTo'], 'gmdate'],
73
            ['gf', 'in', 'range' => array_keys(Profile::getGenderDescsWithEmpty())],
74
            ['gf', 'default', 'value' => ''],
75
        ];
76
    }
77
78
    /**
79
     * Convert time attribute to UTC time.
80
     * @param string $attribute
81
     * @param array $params
82
     * @param mixed $validator
83
     */
84
    public function gmdate($attribute, $params, $validator)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    {
86
        if (isset($this->$attribute)) {
87
            $timestamp = strtotime($this->$attribute);
88
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
89
        }
90
    }
91
92
    /**
93
     * Search
94
     * @param array $params
95
     * @return ActiveDataProvider
96
     */
97
    public function search($params)
98
    {
99
        $query = static::find();
100
        /* @var $query BaseUserQuery */
101
        $userClass = $this->userClass;
102
        $query = $query->from("{$userClass::tableName()} {$this->userAlias}");
103
        $noInitUser = $userClass::buildNoInitModel();
104
        /* @var $noInitUser User */
105
        $profileClass = $noInitUser->profileClass;
106
        if (!empty($profileClass)) {
107
            $query = $query->joinWith(["profile {$this->profileAlias}"]);
108
        }
109
        $dataProvider = new ActiveDataProvider([
110
            'query' => $query,
111
            'pagination' => [
112
                'pageParam' => 'user-page',
113
                'defaultPageSize' => 20,
114
                'pageSizeParam' => 'user-per-page',
115
            ],
116
            'sort' => [
117
                'sortParam' => 'user-sort',
118
                'attributes' => [
119
                    'id',
120
                    'nickname',
121
                    'name' => [
122
                        'asc' => [$this->profileAlias . '.first_name' => SORT_ASC, $this->profileAlias . '.last_name' => SORT_ASC],
123
                        'desc' => [$this->profileAlias . '.first_name' => SORT_DESC, $this->profileAlias . '.last_name' => SORT_DESC],
124
                        'default' => SORT_DESC,
125
                        'label' => Yii::t('user', 'Name'),
126
                    ],
127
                    'gender' => [
128
                        'asc' => [$this->profileAlias . '.gender' => SORT_ASC],
129
                        'desc' => [$this->profileAlias . '.gender' => SORT_DESC],
130
                        'default' => SORT_ASC,
131
                        'label' => Yii::t('user', 'Gender'),
132
                    ],
133
                    'createdAt' => [
134
                        'asc' => [$this->userAlias . '.created_at' => SORT_ASC],
135
                        'desc' => [$this->userAlias . '.created_at' => SORT_DESC],
136
                        'default' => SORT_ASC,
137
                        'label' => Yii::t('user', 'Creation Time'),
138
                    ],
139
                    'updatedAt' => [
140
                        'asc' => [$this->userAlias . '.updated_at' => SORT_ASC],
141
                        'desc' => [$this->userAlias . '.updated_at' => SORT_DESC],
142
                        'default' => SORT_ASC,
143
                        'label' => Yii::t('user', 'Last Updated Time'),
144
                    ],
145
                ],
146
            ],
147
        ]);
148
149
        if (!($this->load($params) && $this->validate())) {
150
            return $dataProvider;
151
        }
152
153
        $query = $query->andFilterWhere([
154
            'LIKE', $this->userAlias . '.id', $this->id,
155
        ])->andFilterWhere([
156
            'LIKE', $this->profileAlias . '.nickname', $this->nickname,
157
        ])->andFilterWhere([
158
            '>=', $this->userAlias . '.created_at', $this->createdFromInUtc,
159
        ])->andFilterWhere([
160
            '<=', $this->userAlias . '.created_at', $this->createdToInUtc,
161
        ])->andFilterWhere([
162
            'LIKE', $this->profileAlias . '.first_name', $this->first_name,
163
        ])->andFilterWhere([
164
            'LIKE', $this->profileAlias . '.last_name', $this->last_name,
165
        ])->andFilterWhere([
166
            $this->profileAlias . '.gender' => $this->gf,
167
        ]);
168
        $dataProvider->query = $query;
169
        return $dataProvider;
170
    }
171
172
    /**
173
     * Add `createdFrom` & `createdTo` attributes.
174
     * @return array
175
     */
176
    public function attributeLabels()
177
    {
178
        $attributeLabels = parent::attributeLabels();
179
        $attributeLabels['id'] = Yii::t('user', 'User ID');
180
        $attributeLabels['nickname'] = Yii::t('user', 'Nickname');
181
        $attributeLabels['first_name'] = Yii::t('user', 'First Name');
182
        $attributeLabels['last_name'] = Yii::t('user', 'Last Name');
183
        $attributeLabels['gf'] = Yii::t('user', 'Gender');
184
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
185
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
186
        return $attributeLabels;
187
    }
188
}
189