QueryBuilderTrait::where()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 17
cp 0
rs 8.8333
c 0
b 0
f 0
cc 7
nc 9
nop 1
crap 56
1
<?php
2
3
namespace Codexshaper\WooCommerce\PHP\Traits;
4
5
use Codexshaper\WooCommerce\PHP\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 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
        return WooCommerce::all($this->endpoint, $this->options);
105
    }
106
107
    /**
108
     * Retrieve data.
109
     *
110
     * @return object
111
     */
112
    protected function first()
113
    {
114
        return $this->get()[0] ?? new \stdClass();
115
    }
116
117
    /**
118
     * Set options for woocommerce request.
119
     *
120
     * @param array $parameters
121
     *
122
     * @return object $this
123
     */
124
    protected function options($parameters)
125
    {
126
        if (!is_array($parameters)) {
127
            throw new \Exception('Options must be an array', 1);
128
        }
129
130
        if (empty($parameters)) {
131
            throw new \Exception('Options must be pass at least one element', 1);
132
        }
133
134
        foreach ($parameters as $key => $value) {
135
            $this->options[$key] = $value;
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Join options for woocommerce request.
143
     *
144
     * @param array $parameters
145
     *
146
     * @return object $this
147
     */
148
    protected function where(...$parameters)
149
    {
150
        if (count($parameters) < 2 || count($parameters) > 3) {
151
            throw new \Exception('You can pass minimum 2 and maximum 3 paramneters');
152
        }
153
        $field = strtolower($parameters[0]);
154
        $value = count($parameters) == 3 ? $parameters[2] : $parameters[1];
155
156
        switch ($field) {
157
            case 'name': case 'title': case 'description':
158
                $this->options['search'] = $value;
159
                break;
160
            default:
161
                $this->options[$field] = $value;
162
                break;
163
        }
164
165
        return $this;
166
    }
167
168
    /**
169
     * Set order direction.
170
     *
171
     * @param string $name
172
     * @param string $direction
173
     *
174
     * @return object $this
175
     */
176
    protected function orderBy($name, $direction = 'desc')
177
    {
178
        $this->options['orderby'] = $name;
179
        $this->options['order'] = $direction;
180
181
        return $this;
182
    }
183
184
    /**
185
     * Paginate results.
186
     *
187
     * @param int $per_page
188
     * @param int $current_page
189
     *
190
     * @return array
191
     */
192
    protected function paginate($per_page, $current_page = 1)
193
    {
194
        try {
195
            $this->options['per_page'] = (int) $per_page;
196
197
            if ($current_page > 0) {
198
                $this->options['page'] = (int) $current_page;
199
            }
200
201
            $results = $this->get();
202
            $totalResults = WooCommerce::countResults();
203
            $totalPages = WooCommerce::countPages();
204
            $currentPage = WooCommerce::current();
205
            $previousPage = WooCommerce::previous();
206
            $nextPage = WooCommerce::next();
207
208
            $pagination = [
209
                'total_results' => $totalResults,
210
                'total_pages'   => $totalPages,
211
                'current_page'  => $currentPage,
212
                'previous_page' => $previousPage,
213
                'next_page'     => $nextPage,
214
                'first_page'    => 1,
215
                'last_page'     => $totalResults,
216
            ];
217
218
            $results['pagination'] = $pagination;
219
220
            return $results;
221
        } catch (\Exception $ex) {
222
            throw new \Exception($ex->getMessage(), 1);
223
        }
224
    }
225
226
    /**
227
     * Count all results.
228
     *
229
     * @return int
230
     */
231
    protected function count()
232
    {
233
        try {
234
            $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...
235
            $totalResults = WooCommerce::countResults();
236
237
            return $totalResults;
238
        } catch (\Exception $ex) {
239
            throw new \Exception($ex->getMessage(), 1);
240
        }
241
    }
242
243
    /**
244
     * Store data.
245
     *
246
     * @return array
247
     */
248
    public function save()
249
    {
250
        $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...
251
252
        return $this->results;
253
    }
254
}
255