Completed
Pull Request — master (#19)
by Evan
02:29
created

QueryBuilder::setModel()   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 1
rs 9.4285
1
<?php
2
3
namespace Silk\Term;
4
5
use WP_Term;
6
use Silk\Term\Model;
7
use Silk\Contracts\BuildsQueries;
8
use Silk\Exception\WP_ErrorException;
9
use Illuminate\Support\Collection;
10
11
class QueryBuilder implements BuildsQueries
12
{
13
    /**
14
     * The term model
15
     * @var Model
16
     */
17
    protected $model;
18
19
    /**
20
     * Collection of arguments
21
     * @var Collection
22
     */
23
    protected $args;
24
25
    /**
26
     * Taxonomy Identifier
27
     * @var string
28
     */
29
    protected $taxonomy;
30
31
    /**
32
     * TermQueryBuilder Constructor.
33
     *
34
     * @param array $args
35
     */
36
    public function __construct(array $args = [])
37
    {
38
        $this->args = Collection::make($args);
39
    }
40
41
    /**
42
     * Restrict the query to terms of the provided Taxonomy.
43
     *
44
     * @param  string $taxonomy
45
     *
46
     * @return $this
47
     */
48
    public function forTaxonomy($taxonomy)
49
    {
50
        $this->taxonomy = $taxonomy;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Include terms that have no related objects in the results.
57
     *
58
     * @return $this
59
     */
60
    public function includeEmpty()
61
    {
62
        $this->args->put('hide_empty', false);
63
64
        return $this;
65
    }
66
67
    /**
68
     * Limit the maximum number of results returned.
69
     *
70
     * @param  int $max_results  Maximum number to return. 0 or 'all' for unlimited.
71
     *
72
     * @return $this
73
     */
74
    public function limit($max_results)
75
    {
76
        $this->args->put('number', intval($max_results));
77
78
        return $this;
79
    }
80
81
    /**
82
     * Get the query results.
83
     *
84
     * @throws WP_ErrorException
85
     *
86
     * @return Collection
87
     */
88
    public function results()
89
    {
90
        if ($this->model) {
91
            return $this->collectModels();
92
        }
93
94
        if ($this->taxonomy) {
95
            $this->args->put('taxonomy', $this->taxonomy);
96
        }
97
98
        return Collection::make($this->fetchTerms());
99
    }
100
101
    /**
102
     * Get the results as a collection of models.
103
     *
104
     * @return Collection
105
     */
106 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...
107
    {
108
        $this->args->put('taxonomy', $this->model->taxonomy);
109
        $this->args->put('fields', 'all');
110
111
        $modelClass = get_class($this->model);
112
113
        return Collection::make($this->fetchTerms())
114
            ->map(function (WP_Term $term) use ($modelClass) {
115
                return new $modelClass($term);
116
            });
117
    }
118
119
    /**
120
     * Set the model for this query.
121
     *
122
     * @param mixed $model
123
     *
124
     * @return $this
125
     */
126
    public function setModel($model)
127
    {
128
        $this->model = $model;
129
130
        return $this;
131
    }
132
133
    /**
134
     * Get the model.
135
     *
136
     * @return mixed Model
137
     */
138
    public function getModel()
139
    {
140
        return $this->model;
141
    }
142
143
    /**
144
     * Perform the term query and return the results.
145
     *
146
     * @throws WP_ErrorException
147
     *
148
     * @return array
149
     */
150
    protected function fetchTerms()
151
    {
152
        if (is_wp_error($terms = get_terms($this->args->toArray()))) {
153
            throw new WP_ErrorException($terms);
154
        }
155
156
        return $terms;
157
    }
158
}
159