Completed
Branch develop (1ed5a2)
by Evan
03:01
created

QueryBuilder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 148
rs 10
wmc 12
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A forTaxonomy() 0 6 1
A includeEmpty() 0 6 1
A limit() 0 6 1
A results() 0 12 3
A collectModels() 0 12 1
A setModel() 0 6 1
A getModel() 0 4 1
A fetchTerms() 0 8 2
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
    protected function collectModels()
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