Completed
Push — develop ( d77dc3...4e0099 )
by Evan
02:28
created

QueryBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 119
rs 10
wmc 11
lcom 1
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A make() 0 4 1
A forTaxonomy() 0 6 1
A all() 0 5 1
A includeEmpty() 0 4 1
A limit() 0 4 1
A query() 0 15 4
A set() 0 6 1
1
<?php
2
3
namespace Silk\Term;
4
5
use Silk\Query\Builder as BaseBuilder;
6
use Silk\Exception\WP_ErrorException;
7
use Illuminate\Support\Collection;
8
9
class QueryBuilder extends BaseBuilder
10
{
11
    /**
12
     * Query arguments
13
     * @var Collection
14
     */
15
    protected $query;
16
17
    /**
18
     * Taxonomy Identifier
19
     * @var string
20
     */
21
    protected $taxonomy;
22
23
    /**
24
     * QueryBuilder Constructor.
25
     *
26
     * @param array $args
27
     */
28
    public function __construct(array $args = [])
29
    {
30
        $this->query = new Collection($args);
31
    }
32
33
    /**
34
     * Create a new instance.
35
     *
36
     * @return static
37
     */
38
    public static function make()
39
    {
40
        return new static;
41
    }
42
43
    /**
44
     * Restrict the query to terms of the provided Taxonomy.
45
     *
46
     * @param  string $taxonomy
47
     *
48
     * @return $this
49
     */
50
    public function forTaxonomy($taxonomy)
51
    {
52
        $this->taxonomy = $taxonomy;
53
54
        return $this;
55
    }
56
57
    /**
58
     * Get all terms.
59
     *
60
     * @return $this
61
     */
62
    public function all()
63
    {
64
        return $this->includeEmpty()
65
            ->limit('all');
66
    }
67
68
    /**
69
     * Include terms that have no related objects in the results.
70
     *
71
     * @return $this
72
     */
73
    public function includeEmpty()
74
    {
75
        return $this->set('hide_empty', false);
76
    }
77
78
    /**
79
     * Limit the maximum number of results returned.
80
     *
81
     * @param  int $max_results  Maximum number to return. 0 or 'all' for unlimited.
82
     *
83
     * @return $this
84
     */
85
    public function limit($max_results)
86
    {
87
        return $this->set('number', intval($max_results));
88
    }
89
90
    /**
91
     * Execute the query and return the raw results.
92
     *
93
     * @throws WP_ErrorException
94
     *
95
     * @return array
96
     */
97
    protected function query()
98
    {
99
        if ($this->model) {
100
            $this->set('taxonomy', $this->model->taxonomy)
0 ignored issues
show
Documentation introduced by
The property taxonomy does not exist on object<Silk\Type\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
                 ->set('fields', 'all');
102
        } elseif ($this->taxonomy) {
103
            $this->set('taxonomy', $this->taxonomy);
104
        }
105
106
        if (is_wp_error($terms = get_terms($this->query->toArray()))) {
107
            throw new WP_ErrorException($terms);
108
        }
109
110
        return $terms;
111
    }
112
113
    /**
114
     * Set an arbitrary query parameter.
115
     *
116
     * @param $parameter
117
     * @param $value
118
     *
119
     * @return $this
120
     */
121
    public function set($parameter, $value)
122
    {
123
        $this->query->put($parameter, $value);
124
125
        return $this;
126
    }
127
}
128