Completed
Push — master ( eec31e...9249bd )
by vistart
05:05
created

EntityQueryTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 47.06%

Importance

Changes 0
Metric Value
wmc 17
lcom 2
cbo 1
dl 0
loc 99
ccs 16
cts 34
cp 0.4706
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A buildNoInitModel() 0 7 3
A guid() 0 5 1
A createdAt() 0 9 3
A updatedAt() 0 9 3
A id() 0 5 1
B page() 0 16 6
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
/**
16
 * This trait is used for building entity query class for entity model.
17
 *
18
 * @version 1.0
19
 * @author vistart <[email protected]>
20
 */
21
trait EntityQueryTrait
22
{
23
    use QueryTrait;
24
    
25
    public $noInitModel;
26
27
    /**
28
     * Build model without any initializations.
29
     */
30 117
    public function buildNoInitModel()
31
    {
32 117
        if (empty($this->noInitModel) && is_string($this->modelClass)) {
33
            $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...
34
            $this->noInitModel = $modelClass::buildNoInitModel();
35
        }
36 117
    }
37
    
38
    /**
39
     * Specify guid attribute.
40
     * @param string|array $guid
41
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
42
     * @return $this
43
     */
44 8
    public function guid($guid, $like = false)
45
    {
46 8
        $model = $this->noInitModel;
47 8
        return $this->likeCondition((string)$guid, $model->guidAttribute, $like);
48
    }
49
    
50
    /**
51
     * Specify id attribute.
52
     * @param string|integer|array $id
53
     * @param false|string $like false, 'like', 'or like', 'not like', 'or not like'.
54
     * @return $this
55
     */
56 1
    public function id($id, $like = false)
57
    {
58 1
        $model = $this->noInitModel;
59 1
        return $this->likeCondition($id, $model->idAttribute, $like);
60
    }
61
    
62
    /**
63
     * Specify create time range.
64
     * @param string $start
65
     * @param string $end
66
     * @return $this
67
     */
68
    public function createdAt($start = null, $end = null)
69
    {
70
        $model = $this->noInitModel;
71
        /* @var $model static */
72
        if (!is_string($model->createdAtAttribute) || empty($model->createdAtAttribute)) {
0 ignored issues
show
Bug introduced by
The property createdAtAttribute 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...
73
            return $this;
74
        }
75
        return static::range($this, $model->createdAtAttribute, $start, $end);
76
    }
77
    
78
    /**
79
     * Specify update time range.
80
     * @param string $start 
81
     * @param string $end
82
     * @return $this
83
     */
84
    public function updatedAt($start = null, $end = null)
85
    {
86
        $model = $this->noInitModel;
87
        /* @var $model static */
88
        if (!is_string($model->updatedAtAttribute) || empty($model->updatedAtAttribute)) {
0 ignored issues
show
Bug introduced by
The property updatedAtAttribute 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...
89
            return $this;
90
        }
91
        return static::range($this, $model->updatedAtAttribute, $start, $end);
92
    }
93
    public static $pageAll = 'all';
94
    public static $defaultPageSize = 10;
95
    
96
    /**
97
     * Specify page condition.
98
     * @param string|int $pageSize It will return all models if it is 'all',
99
     * or it will be regarded as sum of models.
100
     * @param int $currentPage The current page number if it is integer begun with 0.
101
     * @return $this
102
     */
103 1
    public function page($pageSize = 10, $currentPage = 0)
104
    {
105 1
        if ($pageSize === static::$pageAll) {
106
            return $this;
107
        }
108
        /* normalize $currentPage and $currentPage */
109 1
        if (!is_numeric($currentPage) || $currentPage < 0) {
110
            $currentPage = 0;
111
        }
112 1
        $currentPage = (int) $currentPage;
113 1
        if (!is_numeric($pageSize) || $pageSize < 1) {
114
            $pageSize = static::$defaultPageSize;
115
        }
116 1
        $pageSize = (int) $pageSize;
117 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...
118
    }
119
}
120