Completed
Push — master ( 71cd39...d95db8 )
by Evan
15s
created

TermQueryBuilder::forTaxonomy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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 TermQueryBuilder 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
    public function includeEmpty()
56
    {
57
        $this->args->put('hide_empty', false);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Limit the maximum number of results returned.
64
     *
65
     * @param  int $max_results  Maximum number to return. 0 or 'all' for unlimited.
66
     *
67
     * @return $this
68
     */
69
    public function limit($max_results)
70
    {
71
        $this->args->put('number', intval($max_results));
72
73
        return $this;
74
    }
75
76
    /**
77
     * Get the query results.
78
     *
79
     * @throws WP_ErrorException
80
     *
81
     * @return Collection
82
     */
83
    public function results()
84
    {
85
        if ($this->model) {
86
            return $this->collectModels();
87
        }
88
89
        if ($this->taxonomy) {
90
            $this->args->put('taxonomy', $this->taxonomy);
91
        }
92
93
        return Collection::make($this->fetchTerms());
94
    }
95
96
    /**
97
     * Get the results as a collection of models.
98
     *
99
     * @return Collection
100
     */
101 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...
102
    {
103
        $this->args->put('taxonomy', $this->model->taxonomy);
104
        $this->args->put('fields', 'all');
105
106
        $modelClass = get_class($this->model);
107
108
        return Collection::make($this->fetchTerms())
109
            ->map(function (WP_Term $term) use ($modelClass) {
110
                return new $modelClass($term);
111
            });
112
    }
113
114
    /**
115
     * Set the model for this query.
116
     *
117
     * @param mixed $model
118
     *
119
     * @return $this
120
     */
121
    public function setModel($model)
122
    {
123
        $this->model = $model;
124
125
        return $this;
126
    }
127
128
    /**
129
     * Get the model.
130
     *
131
     * @return mixed Model
132
     */
133
    public function getModel()
134
    {
135
        return $this->model;
136
    }
137
138
    /**
139
     * Perform the term query and return the results.
140
     *
141
     * @throws WP_ErrorException
142
     *
143
     * @return array
144
     */
145
    protected function fetchTerms()
146
    {
147
        if (is_wp_error($terms = get_terms($this->args->toArray()))) {
148
            throw new WP_ErrorException($terms);
149
        }
150
151
        return $terms;
152
    }
153
}
154