Completed
Push — master ( b05876...483ce2 )
by CodexShaper
02:22 queued 11s
created

QueryBuilderTrait::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Codexshaper\WooCommerce\Traits;
4
5
use Codexshaper\WooCommerce\Facades\WooCommerce;
6
7
trait QueryBuilderTrait
8
{
9
    /**
10
     * @var
11
     */
12
    protected $options = [];
13
    /**
14
     * @var
15
     */
16
    protected $where = [];
17
    /**
18
     * @var
19
     */
20
    protected $properties = [];
21
22
    /**
23
     * Retrieve all Items.
24
     *
25
     * @param array $options
26
     *
27
     * @return array
28
     */
29
    protected function all($options = [])
30
    {
31
        return WooCommerce::all($this->endpoint, $options);
0 ignored issues
show
Bug introduced by
The property endpoint does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
    }
33
34
    /**
35
     * Retrieve single Item.
36
     *
37
     * @param int   $id
38
     * @param array $options
39
     *
40
     * @return object
41
     */
42
    protected function find($id, $options = [])
43
    {
44
        return collect(WooCommerce::find("{$this->endpoint}/{$id}", $options));
45
    }
46
47
    /**
48
     * Create new Item.
49
     *
50
     * @param array $data
51
     *
52
     * @return object
53
     */
54
    protected function create($data)
55
    {
56
        return WooCommerce::create($this->endpoint, $data);
57
    }
58
59
    /**
60
     * Update Existing Item.
61
     *
62
     * @param int   $id
63
     * @param array $data
64
     *
65
     * @return object
66
     */
67
    protected function update($id, $data)
68
    {
69
        return WooCommerce::update("{$this->endpoint}/{$id}", $data);
70
    }
71
72
    /**
73
     * Destroy Item.
74
     *
75
     * @param int   $id
76
     * @param array $options
77
     *
78
     * @return object
79
     */
80
    protected function delete($id, $options = [])
81
    {
82
        return WooCommerce::delete("{$this->endpoint}/{$id}", $options);
83
    }
84
85
    /**
86
     * Batch Update.
87
     *
88
     * @param array $data
89
     *
90
     * @return object
91
     */
92
    protected function batch($data)
93
    {
94
        return WooCommerce::create("{$this->endpoint}/batch", $data);
95
    }
96
97
    /**
98
     * Retrieve data.
99
     *
100
     * @return array
101
     */
102
    protected function get()
103
    {
104
        $results = WooCommerce::all($this->endpoint, $this->options);
105
106
        if (empty($this->where)) {
107
            return $results;
108
        }
109
        $filteredResults = [];
110
        foreach ($this->where as $key => $where) {
111
            foreach ($results as $result) {
112
                $name = $where['name'];
113
                $name = $result->$name;
114
                $operator = ($where['operator'] == '=') ? $where['operator'].'=' : $where['operator'];
115
                $value = $where['value'];
116
                $condition = "'$name' $operator '$value'";
117
                if (eval("return $condition;")) {
118
                    $filteredResults[] = $result;
119
                }
120
            }
121
        }
122
123
        return $filteredResults;
124
    }
125
126
    /**
127
     * Retrieve data.
128
     *
129
     * @return object
130
     */
131
    protected function first()
132
    {
133
        return collect($this->get()[0]);
134
    }
135
136
    /**
137
     * Set options for woocommerce request.
138
     *
139
     * @param array $parameters
140
     *
141
     * @return object $this
142
     */
143
    protected function options($parameters)
144
    {
145
        if (!is_array($parameters)) {
146
            throw new \Exception('Options must be an array', 1);
147
        }
148
149
        if (empty($parameters)) {
150
            throw new \Exception('Options must be pass at least one element', 1);
151
        }
152
153
        foreach ($parameters as $key => $value) {
154
            $this->options[$key] = $value;
155
        }
156
157
        return $this;
158
    }
159
160
    /**
161
     * Join options for woocommerce request.
162
     *
163
     * @param array $parameters
164
     *
165
     * @return object $this
166
     */
167
    protected function where(...$parameters)
168
    {
169
        if (count($parameters) == 3) {
170
            $where = [
171
                'name'     => $parameters[0],
172
                'operator' => $parameters[1],
173
                'value'    => $parameters[2],
174
            ];
175
            $this->where[] = $where;
176
        }
177
178
        if (count($parameters) == 2) {
179
            $this->options[$parameters[0]] = $parameters[1];
180
        }
181
182
        if (count($parameters) == 1) {
183
            foreach ($parameters as $parameter) {
184
                foreach ($parameter as $key => $value) {
185
                    $this->options[$key] = $value;
186
                }
187
            }
188
        }
189
190
        return $this;
191
    }
192
193
    /**
194
     * Set order direction.
195
     *
196
     * @param string $name
197
     * @param string $direction
198
     *
199
     * @return object $this
200
     */
201
    protected function orderBy($name, $direction = 'desc')
202
    {
203
        $this->options['orderby'] = $name;
204
        $this->options['order'] = $direction;
205
206
        return $this;
207
    }
208
209
    /**
210
     * Paginate results.
211
     *
212
     * @param int $per_page
213
     * @param int $current_page
214
     *
215
     * @return array
216
     */
217
    protected function paginate($per_page, $current_page = 1)
218
    {
219
        $this->options['per_page'] = (int) $per_page;
220
221
        if ($current_page > 0) {
222
            $this->options['page'] = (int) $current_page;
223
        }
224
225
        $results = $this->get();
226
        $totalResults = WooCommerce::countResults();
227
        $totalPages = WooCommerce::countPages();
228
        $currentPage = WooCommerce::current();
229
        $previousPage = WooCommerce::previous();
230
        $nextPage = WooCommerce::next();
231
232
        $pagination = [
233
            'total_results' => $totalResults,
234
            'total_pages'   => $totalPages,
235
            'current_page'  => $currentPage,
236
            'previous_page' => $previousPage,
237
            'next_page'     => $nextPage,
238
            'first_page'    => 1,
239
            'last_page'     => $totalResults,
240
        ];
241
242
        $results['pagination'] = $pagination;
243
244
        return $results;
245
    }
246
247
    /**
248
     * Count all results.
249
     *
250
     * @return int
251
     */
252
    protected function count()
253
    {
254
        $results = WooCommerce::all($this->endpoint, $this->options);
0 ignored issues
show
Unused Code introduced by
$results is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
255
        $totalResults = WooCommerce::countResults();
256
        
257
        return $totalResults;
258
    }
259
260
    /**
261
     * Store data.
262
     *
263
     * @return array
264
     */
265
    public function save()
266
    {
267
        $this->results = WooCommerce::create($this->endpoint, $this->properties);
0 ignored issues
show
Bug introduced by
The property results does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
268
269
        return $this->results;
270
    }
271
}
272