Completed
Push — master ( 30b2a3...fab25c )
by CodexShaper
02:32
created

QueryBuilderTrait::where()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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