EntityQueryTrait::orderByCreatedAt()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 4
cts 5
cp 0.8
rs 9.6666
cc 3
eloc 5
nc 2
nop 1
crap 3.072
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\base\models\traits;
14
15
use rhosocial\base\helpers\Number;
16
use rhosocial\base\models\models\BaseEntityModel;
17
use yii\db\ActiveQuery;
18
19
/**
20
 * This trait is used for building entity query class for entity model.
21
 *
22
 * @version 1.0
23
 * @author vistart <[email protected]>
24
 */
25
trait EntityQueryTrait
26
{
27
    use QueryTrait;
28
29
    /**
30
     * @var BaseEntityModel
31
     */
32
    public $noInitModel;
33
34
    /**
35
     * Build model without any initializations.
36
     */
37 366
    public function buildNoInitModel()
38
    {
39 366
        if (empty($this->noInitModel) && is_string($this->modelClass)) {
40
            $modelClass = $this->modelClass;
0 ignored issues
show
Bug introduced by
The property modelClass 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...
41
            $this->noInitModel = $modelClass::buildNoInitModel();
42
        }
43 366
    }
44
45
    /**
46
     * Specify guid attribute.
47
     * @param string|array $guid
48
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
49
     * @return $this
50
     */
51 17
    public function guid($guid, $like = false)
52
    {
53
        /* @var $this ActiveQuery */
54 17
        $model = $this->noInitModel;
55 17
        return $this->likeCondition((string)$guid, $model->guidAttribute, $like);
56
    }
57
58
    /**
59
     * Specify id attribute.
60
     * @param string|integer|array $id
61
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
62
     * @return $this
63
     */
64 10
    public function id($id, $like = false)
65
    {
66
        /* @var $this ActiveQuery */
67 10
        $model = $this->noInitModel;
68 10
        return $this->likeCondition($id, $model->idAttribute, $like);
69
    }
70
71
    /**
72
     * Specify GUID or ID attribute.
73
     * Scalar parameter is acceptable only.
74
     * Please do not pass an array to the first parameter.
75
     * @param string|integer $param
76
     * @param bool|string $like false, 'like', 'or like', 'not like', 'or not like'.
77
     * @return $this
78
     */
79 1
    public function guidOrId($param, $like = false)
80
    {
81 1
        if (is_string($param) && (preg_match(Number::GUID_REGEX, $param) || strlen($param) == 16)) {
82 1
            return $this->guid($param, $like);
0 ignored issues
show
Bug introduced by
It seems like $like defined by parameter $like on line 79 can also be of type boolean; however, rhosocial\base\models\tr...ntityQueryTrait::guid() does only seem to accept false|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
83
        }
84 1
        return $this->id($param, $like);
0 ignored issues
show
Bug introduced by
It seems like $like defined by parameter $like on line 79 can also be of type boolean; however, rhosocial\base\models\tr...\EntityQueryTrait::id() does only seem to accept false|string, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
85
    }
86
87
    /**
88
     * Specify creation time range.
89
     * @param string $start
90
     * @param string $end
91
     * @return $this
92
     */
93 7
    public function createdAt($start = null, $end = null)
94
    {
95
        /* @var $this ActiveQuery */
96 7
        $model = $this->noInitModel;
97 7
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
98
            return $this;
99
        }
100 7
        return static::range($this, $model->createdAtAttribute, $start, $end);
101
    }
102
103
    /**
104
     * Specify creation time as today (in locally).
105
     * @return $this
106
     */
107 1
    public function createdAtToday()
108
    {
109
        /* @var $this ActiveQuery */
110 1
        $model = $this->noInitModel;
111 1
        $start = strtotime(date('Y-m-d'));
112 1
        $end = $start + 86400;
113 1
        if ($model->timeFormat == BaseEntityModel::$timeFormatDatetime) {
114 1
            $start = gmdate('Y-m-d H:i:s', $start);
115 1
            $end = gmdate('Y-m-d H:i:s', $end);
116
        }
117 1
        return $this->createdAt($start, $end);
118
    }
119
120
    /**
121
     * Specify order by creation time.
122
     * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
123
     * @return $this
124
     */
125 3
    public function orderByCreatedAt($sort = SORT_ASC)
126
    {
127
        /* @var $this ActiveQuery */
128 3
        $model = $this->noInitModel;
129 3
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
130
            return $this;
131
        }
132 3
        return $this->addOrderBy([$model->createdAtAttribute => $sort]);
133
    }
134
135
    /**
136
     * Specify last updated time range.
137
     * @param string $start 
138
     * @param string $end
139
     * @return $this
140
     */
141 8
    public function updatedAt($start = null, $end = null)
142
    {
143
        /* @var $this ActiveQuery */
144 8
        $model = $this->noInitModel;
145 8
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
146
            return $this;
147
        }
148 8
        return static::range($this, $model->updatedAtAttribute, $start, $end);
149
    }
150
151
    /**
152
     * Specify last updated time as today (in locally).
153
     * @return $this
154
     */
155 1
    public function updatedAtToday()
156
    {
157
        /* @var $this ActiveQuery */
158 1
        $model = $this->noInitModel;
159 1
        $start = strtotime(date('Y-m-d'));
160 1
        $end = $start + 86400;
161 1
        if ($model->timeFormat == BaseEntityModel::$timeFormatDatetime) {
162 1
            $start = gmdate('Y-m-d H:i:s', $start);
163 1
            $end = gmdate('Y-m-d H:i:s', $end);
164
        }
165 1
        return $this->updatedAt($start, $end);
166
    }
167
168
    /**
169
     * Specify order by update time.
170
     * @param string $sort only 'SORT_ASC' and 'SORT_DESC' are acceptable.
171
     * @return $this
172
     */
173 2
    public function orderByUpdatedAt($sort = SORT_ASC)
174
    {
175
        /* @var $this ActiveQuery */
176 2
        $model = $this->noInitModel;
177 2
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
178
            return $this;
179
        }
180 2
        return $this->addOrderBy([$model->updatedAtAttribute => $sort]);
181
    }
182
183
    public static $pageAll = 'all';
184
    public static $defaultPageSize = 10;
185
    
186
    /**
187
     * Specify page condition.
188
     * @param string|int $pageSize It will return all models if it is 'all',
189
     * or it will be regarded as sum of models.
190
     * @param int $currentPage The current page number if it is integer begun with 0.
191
     * @return $this
192
     */
193 3
    public function page($pageSize = 10, $currentPage = 0)
194
    {
195 3
        if ($pageSize === static::$pageAll) {
196 2
            return $this;
197
        }
198
        /* normalize $currentPage and $currentPage */
199 1
        if (!is_numeric($currentPage) || $currentPage < 0) {
200 1
            $currentPage = 0;
201
        }
202 1
        $currentPage = (int) $currentPage;
203 1
        if (!is_numeric($pageSize) || $pageSize < 1) {
204 1
            $pageSize = static::$defaultPageSize;
205
        }
206 1
        $pageSize = (int) $pageSize;
207 1
        return $this->limit($pageSize)->offset($pageSize * $currentPage);
0 ignored issues
show
Bug introduced by
It seems like limit() 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...
208
    }
209
}
210