Completed
Branch develop (2a5993)
by Evan
02:52
created

QueryBuilder::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
rs 10
1
<?php
2
3
namespace Silk\Post;
4
5
use WP_Query;
6
use Illuminate\Support\Collection;
7
use Silk\Query\Builder as BaseBuilder;
8
9
class QueryBuilder extends BaseBuilder
10
{
11
    /**
12
     * WP_Query instance
13
     *
14
     * @var WP_Query
15
     */
16
    protected $query;
17
18
    /**
19
     * Post Model instance
20
     *
21
     * @var Model
22
     */
23
    protected $model;
24
25
    /**
26
     * Builder constructor.
27
     *
28
     * @param WP_Query $query
29
     */
30
    public function __construct(WP_Query $query)
31
    {
32
        $this->query = $query;
33
    }
34
35
    /**
36
     * Create a new instance.
37
     *
38
     * @return static
39
     */
40
    public static function make()
41
    {
42
        return new static(new WP_Query);
43
    }
44
45
    /**
46
     * Limit the number of returned results
47
     *
48
     * @param integer $limit  The maximum number of results to return
49
     *                        use -1 for no limit
50
     *
51
     * @return $this
52
     */
53
    public function limit($limit)
54
    {
55
        $this->query->set('posts_per_page', (int) $limit);
56
57
        return $this;
58
    }
59
60
    /**
61
     * Return an unlimited number of results.
62
     *
63
     * @return $this
64
     */
65
    public function all()
66
    {
67
        return $this->limit(-1);
68
    }
69
70
    /**
71
     * Set the order for the query
72
     *
73
     * @param  string $order
74
     *
75
     * @return $this
76
     */
77
    public function order($order)
78
    {
79
        $this->query->set('order', strtoupper($order));
80
81
        return $this;
82
    }
83
84
    /**
85
     * Query by post status
86
     *
87
     * @param  string|array $status  the post status or stati to match
88
     *
89
     * @return $this
90
     */
91
    public function whereStatus($status)
92
    {
93
        $this->query->set('post_status', $status);
94
95
        return $this;
96
    }
97
98
    /**
99
     * Query by slug
100
     *
101
     * @param  string $slug  the post slug to query by
102
     *
103
     * @return $this
104
     */
105
    public function whereSlug($slug)
106
    {
107
        $this->query->set('name', $slug);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get the results as a collection
114
     *
115
     * @return Collection
116
     */
117
    public function results()
118
    {
119
        if ($this->model) {
120
            return $this->collectModels();
121
        }
122
123
        return Collection::make($this->query->get_posts());
124
    }
125
126
    /**
127
     * Get the results as a collection of post model instances
128
     *
129
     * @return Collection
130
     */
131 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...
132
    {
133
        $this->query->set('post_type', $this->model->post_type);
134
        $this->query->set('fields', ''); // as WP_Post objects
135
        $modelClass = get_class($this->model);
136
137
        return Collection::make($this->query->get_posts())
138
            ->map(function ($post) use ($modelClass) {
139
                return new $modelClass($post);
140
            });
141
    }
142
143
    /**
144
     * Set a query variable on the query
145
     *
146
     * @param string $var   Query variable key
147
     * @param mixed  $value Query value for key
148
     *
149
     * @return $this
150
     */
151
    public function set($var, $value)
152
    {
153
        $this->query->set($var, $value);
154
155
        return $this;
156
    }
157
}
158