UserSearch::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the 2amigos/yii2-usuario project.
5
 *
6
 * (c) 2amigOS! <http://2amigos.us/>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Da\User\Search;
13
14
use Da\User\Query\UserQuery;
15
use Yii;
16
use yii\base\InvalidParamException;
17
use yii\base\Model;
18
use yii\data\ActiveDataProvider;
19
20
class UserSearch extends Model
21
{
22
    /**
23
     * @var string
24
     */
25
    public $username;
26
    /**
27
     * @var string
28
     */
29
    public $email;
30
    /**
31
     * @var int
32
     */
33
    public $created_at;
34
    /**
35
     * @var int
36
     */
37
    public $last_login_at;
38
    /**
39
     * @var string
40
     */
41
    public $registration_ip;
42
    /**
43
     * @var string
44
     */
45
    public $last_login_ip;
46
    /**
47
     * @var UserQuery
48
     */
49
    protected $query;
50
51
    /**
52
     * UserSearch constructor.
53
     *
54
     * @param UserQuery $query
55
     * @param array     $config
56
     */
57
    public function __construct(UserQuery $query, $config = [])
58
    {
59
        $this->query = $query;
60
        parent::__construct($config);
61
    }
62
63
    /**
64
     * @return array
65
     */
66
    public function rules()
67
    {
68
        return [
69
            'safeFields' => [['username', 'email', 'registration_ip', 'created_at', 'last_login_at, last_login_ip'], 'safe'],
70
            'createdDefault' => [['created_at', 'last_login_at'], 'default', 'value' => null],
71
        ];
72
    }
73
74
    /**
75
     * @return array
76
     */
77
    public function attributeLabels()
78
    {
79
        return [
80
            'username' => Yii::t('usuario', 'Username'),
81
            'email' => Yii::t('usuario', 'Email'),
82
            'created_at' => Yii::t('usuario', 'Registration time'),
83
            'registration_ip' => Yii::t('usuario', 'Registration IP'),
84
            'last_login_at' => Yii::t('usuario', 'Last login time'),
85
            'last_login_ip' => Yii::t('usuario', 'Last login IP'),
86
        ];
87
    }
88
89
    /**
90
     * @param $params
91
     *
92
     * @throws InvalidParamException
93
     * @return ActiveDataProvider
94
     */
95
    public function search($params)
96
    {
97
        $query = $this->query;
98
99
        $dataProvider = new ActiveDataProvider(
100
            [
101
                'query' => $query,
102
            ]
103
        );
104
105
        if (!($this->load($params) && $this->validate())) {
106
            return $dataProvider;
107
        }
108
109
        if ($this->created_at !== null) {
110
            $date = strtotime($this->created_at);
111
            $query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]);
112
        }
113
114
        if ($this->last_login_at !== null) {
115
            $date = strtotime($this->last_login_at);
116
            $query->andFilterWhere(['between', 'last_login_at', $date, $date + 3600 * 24]);
117
        }
118
119
        $query
120
            ->andFilterWhere(['like', 'username', $this->username])
121
            ->andFilterWhere(['like', 'email', $this->email])
122
            ->andFilterWhere(['registration_ip' => $this->registration_ip])
123
            ->andFilterWhere(['last_login_ip' => $this->last_login_ip]);
124
125
        return $dataProvider;
126
    }
127
}
128