Completed
Branch develop (73bc23)
by Evan
02:37
created

QueryBuilder::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
cc 1
eloc 3
nc 1
nop 2
rs 9.4285
1
<?php
2
3
namespace Silk\Post;
4
5
use Illuminate\Support\Collection;
6
use Silk\Contracts\WP_QueryInterface;
7
8
class QueryBuilder
9
{
10
    /**
11
     * WP_Query instance
12
     *
13
     * @var WP_Query
14
     */
15
    protected $query;
16
17
    /**
18
     * Post Model instance
19
     *
20
     * @var Model
21
     */
22
    protected $model;
23
24
    /**
25
     * Builder constructor.
26
     *
27
     * @param WP_QueryInterface $query
28
     */
29
    public function __construct(WP_QueryInterface $query)
30
    {
31
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query of type object<Silk\Contracts\WP_QueryInterface> is incompatible with the declared type object<Silk\Post\WP_Query> of property $query.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
    /**
35
     * Limit the number of returned results
36
     *
37
     * @param integer $limit  The maximum number of results to return
38
     *                        use -1 for no limit
39
     *
40
     * @return $this
41
     */
42
    public function limit($limit)
43
    {
44
        $this->query->set('posts_per_page', (int) $limit);
45
46
        return $this;
47
    }
48
49
    /**
50
     * Return an unlimited number of results.
51
     *
52
     * @return $this
53
     */
54
    public function all()
55
    {
56
        return $this->limit(-1);
57
    }
58
59
    /**
60
     * Set the order for the query
61
     *
62
     * @param  string $order
63
     *
64
     * @return $this
65
     */
66
    public function order($order)
67
    {
68
        $this->query->set('order', strtoupper($order));
69
70
        return $this;
71
    }
72
73
    /**
74
     * Query by post status
75
     *
76
     * @param  string|array $status  the post status or stati to match
77
     *
78
     * @return $this
79
     */
80
    public function whereStatus($status)
81
    {
82
        $this->query->set('post_status', $status);
83
84
        return $this;
85
    }
86
87
    /**
88
     * Query by slug
89
     *
90
     * @param  string $slug  the post slug to query by
91
     *
92
     * @return $this
93
     */
94
    public function whereSlug($slug)
95
    {
96
        $this->query->set('name', $slug);
97
98
        return $this;
99
    }
100
101
    /**
102
     * Get the results as a collection
103
     *
104
     * @return Collection
105
     */
106
    public function results()
107
    {
108
        if ($this->model) {
109
            return $this->collectModels();
110
        }
111
112
        return Collection::make($this->query->get_posts());
113
    }
114
115
    /**
116
     * Get the results as a collection of post model instances
117
     *
118
     * @return Collection
119
     */
120 View Code Duplication
    protected function collectModels()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $this->query->set('post_type', $this->model->post_type);
123
        $this->query->set('fields', ''); // as WP_Post objects
124
        $modelClass = get_class($this->model);
125
126
        return Collection::make($this->query->get_posts())
127
            ->map(function ($post) use ($modelClass) {
128
                return new $modelClass($post);
129
            });
130
    }
131
132
    /**
133
     * Set a query variable on the query
134
     *
135
     * @param string $var   Query variable key
136
     * @param mixed  $value Query value for key
137
     *
138
     * @return $this
139
     */
140
    public function set($var, $value)
141
    {
142
        $this->query->set($var, $value);
143
144
        return $this;
145
    }
146
147
    /**
148
     * Set the model for this query.
149
     *
150
     * @param Model $model
151
     *
152
     * @return $this
153
     */
154
    public function setModel(Model $model)
155
    {
156
        $this->model = $model;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get the model
163
     *
164
     * @return Model
165
     */
166
    public function getModel()
167
    {
168
        return $this->model;
169
    }
170
171
}
172