Completed
Pull Request — master (#361)
by
unknown
03:02
created

SessionHistorySearch   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 54
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 6 1
A search() 0 31 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\Model\SessionHistory;
15
use Da\User\Traits\ContainerAwareTrait;
16
use yii\base\InvalidConfigException;
17
use yii\base\InvalidParamException;
18
use yii\data\ActiveDataProvider;
19
20
21
class SessionHistorySearch extends SessionHistory
22
{
23
    use ContainerAwareTrait;
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function rules()
29
    {
30
        return [
31
            [['user_agent', 'ip'], 'safe'],
32
        ];
33
    }
34
35
    /**
36
     * @param array $params
37
     *
38
     * @throws InvalidConfigException
39
     * @throws InvalidParamException
40
     *
41
     * @return ActiveDataProvider
42
     */
43
    public function search($params)
44
    {
45
        $query = SessionHistory::find()->andWhere([
46
            'user_id' => $this->user_id,
47
        ]);
48
49
        /** @var ActiveDataProvider $dataProvider */
50
        $dataProvider = $this->make(
51
            ActiveDataProvider::class,
52
            [],
53
            [
54
                'query' => $query,
55
                'sort' => [
56
                    'defaultOrder' => [
57
                        'updated_at' => SORT_DESC
58
                    ],
59
                ]
60
            ]
61
        );
62
63
        $this->load($params);
64
65
        if (!$this->validate()) {
66
            return $dataProvider;
67
        }
68
69
        $query->andFilterWhere(['like', 'user_agent', $this->user_agent])
70
            ->andFilterWhere(['like', 'ip', $this->ip]);
71
72
        return $dataProvider;
73
    }
74
}
75