Completed
Push — master ( 22896e...75e918 )
by vistart
03:54
created

UserProfileSearchTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 105
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A rules() 0 9 1
A gmdate() 0 7 2
A scenarios() 0 5 1
B search() 0 35 3
A attributeLabels() 0 7 1
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 Yii;
16
use yii\base\Model;
17
use yii\data\ActiveDataProvider;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait UserProfileSearchTrait
24
{
25
    /**
26
     * @var string 
27
     */
28
    public $createdFrom;
29
    protected $createdFromInUtc;
30
31
    /**
32
     * @var string 
33
     */
34
    public $createdTo;
35
    protected $createdToInUtc;
36
37
    /**
38
     * 
39
     * @return array
40
     */
41
    public function rules()
42
    {
43
        return [
44
            ['id', 'integer'],
45
            [['nickname', 'first_name', 'last_name'], 'string'],
46
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-MM-dd HH:mm'],
47
            [['createdFrom', 'createdTo'], 'gmdate'],
48
        ];
49
    }
50
51
    /**
52
     * Convert time attribute to UTC time.
53
     * @param string $attribute
54
     * @param array $params
55
     * @param mixed $validator
56
     */
57
    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...
58
    {
59
        if (isset($this->$attribute)) {
60
            $timestamp = strtotime($this->$attribute);
61
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
62
        }
63
    }
64
65
    /**
66
     * 
67
     * @return array
68
     */
69
    public function scenarios()
70
    {
71
        // bypass scenarios() implementation in the parent class
72
        return Model::scenarios();
73
    }
74
75
    /**
76
     * Search
77
     * @param array $params
78
     * @return ActiveDataProvider
79
     */
80
    public function search($params)
81
    {
82
        $query = static::find();
83
        $dataProvider = new ActiveDataProvider([
84
            'query' => $query,
85
            'pagination' => [
86
                'pageParam' => 'user-page',
87
                'defaultPageSize' => 20,
88
                'pageSizeParam' => 'user-per-page',
89
            ],
90
            'sort' => [
91
                'sortParam' => 'user-sort',
92
            ],
93
        ]);
94
95
        if (!($this->load($params) && $this->validate())) {
0 ignored issues
show
Bug introduced by
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
Bug introduced by
It seems like validate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
            return $dataProvider;
97
        }
98
99
        $query = $query->andFilterWhere([
100
            'LIKE', 'id', $this->id,
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
101
        ])->andFilterWhere([
102
            'LIKE', 'nickname', $this->nickname,
0 ignored issues
show
Bug introduced by
The property nickname does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
103
        ])->andFilterWhere([
104
            '>=', 'created_at', $this->createdFromInUtc,
105
        ])->andFilterWhere([
106
            '<=', 'created_at', $this->createdToInUtc,
107
        ])->andFilterWhere([
108
            'LIKE', 'first_name', $this->first_name,
0 ignored issues
show
Bug introduced by
The property first_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
109
        ])->andFilterWhere([
110
            'LIKE', 'last_name', $this->last_name,
0 ignored issues
show
Bug introduced by
The property last_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
111
        ]);
112
        $dataProvider->query = $query;
113
        return $dataProvider;
114
    }
115
116
    /**
117
     * Add `createdFrom` & `createdTo` attributes.
118
     * @return array
119
     */
120
    public function attributeLabels()
121
    {
122
        $attributeLabels = parent::attributeLabels();
123
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
124
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
125
        return $attributeLabels;
126
    }
127
}
128