Total Complexity | 98 |
Total Lines | 492 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ProductRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class ProductRequest extends AbstractRequest { |
||
16 | |||
17 | /** |
||
18 | * Convert request to object. |
||
19 | * |
||
20 | * @return \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External |
||
21 | */ |
||
22 | public function prepare_object() { |
||
23 | $object = $this->get_product_object(); |
||
24 | |||
25 | $this->set_common_props( $object ); |
||
26 | $this->set_meta_data( $object ); |
||
27 | |||
28 | switch ( $object->get_type() ) { |
||
29 | case 'grouped': |
||
30 | $this->set_grouped_props( $object ); |
||
31 | break; |
||
32 | case 'variable': |
||
33 | $this->set_variable_props( $object ); |
||
34 | break; |
||
35 | case 'external': |
||
36 | $this->set_external_props( $object ); |
||
37 | break; |
||
38 | } |
||
39 | |||
40 | if ( $object->get_downloadable() ) { |
||
41 | $this->set_downloadable_props( $object ); |
||
42 | } |
||
43 | |||
44 | return $object; |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * Get product object from request args. |
||
49 | * |
||
50 | * @throws \WC_REST_Exception Will throw an exception if the resulting product object is invalid. |
||
51 | * @return \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External |
||
52 | */ |
||
53 | protected function get_product_object() { |
||
54 | $id = (int) $this->get_param( 'id', 0 ); |
||
55 | $type = $this->get_param( 'type', '' ); |
||
56 | |||
57 | if ( $type ) { |
||
58 | $classname = '\\' . \WC_Product_Factory::get_classname_from_product_type( $type ); |
||
|
|||
59 | $object = class_exists( $classname ) ? new $classname( $id ) : new \WC_Product_Simple( $id ); |
||
60 | } elseif ( $id ) { |
||
61 | $object = wc_get_product( $id ); |
||
62 | } else { |
||
63 | $object = new \WC_Product_Simple(); |
||
64 | } |
||
65 | |||
66 | if ( ! $object ) { |
||
67 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_product_id', __( 'Invalid product.', 'woocommerce' ), 404 ); |
||
68 | } |
||
69 | |||
70 | if ( $object->is_type( 'variation' ) ) { |
||
71 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_product_id', __( 'To manipulate product variations you should use the /products/<product_id>/variations/<id> endpoint.', 'woocommerce' ), 404 ); |
||
72 | } |
||
73 | |||
74 | return $object; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Set common product props. |
||
79 | * |
||
80 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
81 | */ |
||
82 | protected function set_common_props( &$object ) { |
||
83 | $props = [ |
||
84 | 'name', |
||
85 | 'sku', |
||
86 | 'description', |
||
87 | 'short_description', |
||
88 | 'slug', |
||
89 | 'menu_order', |
||
90 | 'reviews_allowed', |
||
91 | 'virtual', |
||
92 | 'tax_status', |
||
93 | 'tax_class', |
||
94 | 'catalog_visibility', |
||
95 | 'purchase_note', |
||
96 | 'status', |
||
97 | 'featured', |
||
98 | 'regular_price', |
||
99 | 'sale_price', |
||
100 | 'date_on_sale_from', |
||
101 | 'date_on_sale_from_gmt', |
||
102 | 'date_on_sale_to', |
||
103 | 'date_on_sale_to_gmt', |
||
104 | 'parent_id', |
||
105 | 'sold_individually', |
||
106 | 'manage_stock', |
||
107 | 'backorders', |
||
108 | 'stock_status', |
||
109 | 'stock_quantity', |
||
110 | 'downloadable', |
||
111 | 'date_created', |
||
112 | 'date_created_gmt', |
||
113 | 'upsell_ids', |
||
114 | 'cross_sell_ids', |
||
115 | 'images', |
||
116 | 'categories', |
||
117 | 'tags', |
||
118 | 'attributes', |
||
119 | 'weight', |
||
120 | 'dimensions', |
||
121 | 'shipping_class', |
||
122 | ]; |
||
123 | |||
124 | $request_props = array_intersect_key( $this->request, array_flip( $props ) ); |
||
125 | $prop_values = []; |
||
126 | |||
127 | foreach ( $request_props as $prop => $value ) { |
||
128 | switch ( $prop ) { |
||
129 | case 'date_created': |
||
130 | case 'date_created_gmt': |
||
131 | $prop_values[ $prop ] = rest_parse_date( $value ); |
||
132 | break; |
||
133 | case 'upsell_ids': |
||
134 | case 'cross_sell_ids': |
||
135 | $prop_values[ $prop ] = wp_parse_id_list( $value ); |
||
136 | break; |
||
137 | case 'images': |
||
138 | $images = $this->parse_images_field( $value, $object ); |
||
139 | $prop_values = array_merge( $prop_values, $images ); |
||
140 | break; |
||
141 | case 'categories': |
||
142 | $prop_values['category_ids'] = $this->parse_terms_field( $value ); |
||
143 | break; |
||
144 | case 'tags': |
||
145 | $prop_values['tag_ids'] = $this->parse_terms_field( $value ); |
||
146 | break; |
||
147 | case 'attributes': |
||
148 | $prop_values['attributes'] = $this->parse_attributes_field( $value ); |
||
149 | break; |
||
150 | case 'dimensions': |
||
151 | $dimensions = $this->parse_dimensions_fields( $value ); |
||
152 | $prop_values = array_merge( $prop_values, $dimensions ); |
||
153 | break; |
||
154 | case 'shipping_class': |
||
155 | $prop_values['shipping_class_id'] = $this->parse_shipping_class( $value, $object ); |
||
156 | break; |
||
157 | default: |
||
158 | $prop_values[ $prop ] = $value; |
||
159 | } |
||
160 | } |
||
161 | |||
162 | foreach ( $prop_values as $prop => $value ) { |
||
163 | $object->{"set_$prop"}( $value ); |
||
164 | } |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Set grouped product props. |
||
169 | * |
||
170 | * @param \WC_Product_Grouped $object Product object reference. |
||
171 | */ |
||
172 | protected function set_grouped_props( &$object ) { |
||
173 | $children = $this->get_param( 'grouped_products', null ); |
||
174 | |||
175 | if ( ! is_null( $children ) ) { |
||
176 | $object->set_children( $children ); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Set variable product props. |
||
182 | * |
||
183 | * @param \WC_Product_Variable $object Product object reference. |
||
184 | */ |
||
185 | protected function set_variable_props( &$object ) { |
||
186 | $default_attributes = $this->get_param( 'default_attributes', null ); |
||
187 | |||
188 | if ( ! is_null( $default_attributes ) ) { |
||
189 | $object->set_default_attributes( $this->parse_default_attributes( $default_attributes, $object ) ); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Set external product props. |
||
195 | * |
||
196 | * @param \WC_Product_External $object Product object reference. |
||
197 | */ |
||
198 | protected function set_external_props( &$object ) { |
||
199 | $button_text = $this->get_param( 'button_text', null ); |
||
200 | $external_url = $this->get_param( 'external_url', null ); |
||
201 | |||
202 | if ( ! is_null( $button_text ) ) { |
||
203 | $object->set_button_text( $button_text ); |
||
204 | } |
||
205 | |||
206 | if ( ! is_null( $external_url ) ) { |
||
207 | $object->set_product_url( $external_url ); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Set downloadable product props. |
||
213 | * |
||
214 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
215 | */ |
||
216 | protected function set_downloadable_props( &$object ) { |
||
231 | } |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Set meta data. |
||
236 | * |
||
237 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
238 | */ |
||
239 | protected function set_meta_data( &$object ) { |
||
240 | $meta_data = $this->get_param( 'meta_data', null ); |
||
241 | |||
242 | if ( ! is_null( $meta_data ) ) { |
||
243 | foreach ( $meta_data as $meta ) { |
||
244 | $object->update_meta_data( $meta['key'], $meta['value'], isset( $meta['id'] ) ? $meta['id'] : '' ); |
||
245 | } |
||
246 | } |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Set product object's attributes. |
||
251 | * |
||
252 | * @param array $raw_attributes Attribute data from request. |
||
253 | * @return array |
||
254 | */ |
||
255 | protected function parse_attributes_field( $raw_attributes ) { |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * Set product images. |
||
322 | * |
||
323 | * @throws \WC_REST_Exception REST API exceptions. |
||
324 | * @param array $images Images data. |
||
325 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object. |
||
326 | * @return array |
||
327 | */ |
||
328 | protected function parse_images_field( $images, $object ) { |
||
329 | $response = [ |
||
330 | 'image_id' => '', |
||
331 | 'gallery_image_ids' => [], |
||
332 | ]; |
||
333 | |||
334 | $images = is_array( $images ) ? array_filter( $images ) : []; |
||
335 | |||
336 | if ( empty( $images ) ) { |
||
337 | return $response; |
||
338 | } |
||
339 | |||
340 | foreach ( $images as $index => $image ) { |
||
341 | $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; |
||
342 | |||
343 | if ( 0 === $attachment_id && isset( $image['src'] ) ) { |
||
344 | $upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) ); |
||
345 | |||
346 | if ( is_wp_error( $upload ) ) { |
||
347 | if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $object->get_id(), $images ) ) { |
||
348 | throw new \WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 ); |
||
349 | } else { |
||
350 | continue; |
||
351 | } |
||
352 | } |
||
353 | |||
354 | $attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $object->get_id() ); |
||
355 | } |
||
356 | |||
357 | if ( ! wp_attachment_is_image( $attachment_id ) ) { |
||
358 | /* translators: %s: image ID */ |
||
359 | throw new \WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 ); |
||
360 | } |
||
361 | |||
362 | if ( 0 === $index ) { |
||
363 | $response['image_id'] = $attachment_id; |
||
364 | } else { |
||
365 | $response['gallery_image_ids'][] = $attachment_id; |
||
366 | } |
||
367 | |||
368 | // Set the image alt if present. |
||
369 | if ( ! empty( $image['alt'] ) ) { |
||
370 | update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) ); |
||
371 | } |
||
372 | |||
373 | // Set the image name if present. |
||
374 | if ( ! empty( $image['name'] ) ) { |
||
375 | wp_update_post( |
||
376 | array( |
||
377 | 'ID' => $attachment_id, |
||
378 | 'post_title' => $image['name'], |
||
379 | ) |
||
380 | ); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | return $response; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Parse dimensions. |
||
389 | * |
||
390 | * @param array $dimensions Product dimensions. |
||
391 | * @return array |
||
392 | */ |
||
393 | protected function parse_dimensions_fields( $dimensions ) { |
||
394 | $response = []; |
||
395 | |||
396 | if ( isset( $dimensions['length'] ) ) { |
||
397 | $response['length'] = $dimensions['length']; |
||
398 | } |
||
399 | |||
400 | if ( isset( $dimensions['width'] ) ) { |
||
401 | $response['width'] = $dimensions['width']; |
||
402 | } |
||
403 | |||
404 | if ( isset( $dimensions['height'] ) ) { |
||
405 | $response['height'] = $dimensions['height']; |
||
406 | } |
||
407 | |||
408 | return $response; |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Parse shipping class. |
||
413 | * |
||
414 | * @param string $shipping_class Shipping class slug. |
||
415 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object. |
||
416 | * @return int |
||
417 | */ |
||
418 | protected function parse_shipping_class( $shipping_class, $object ) { |
||
419 | $data_store = $object->get_data_store(); |
||
420 | return $data_store->get_shipping_class_id_by_slug( wc_clean( $shipping_class ) ); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Parse downloadable files. |
||
425 | * |
||
426 | * @param array $downloads Downloads data. |
||
427 | * @return array |
||
428 | */ |
||
429 | protected function parse_downloads_field( $downloads ) { |
||
430 | $files = array(); |
||
431 | foreach ( $downloads as $key => $file ) { |
||
432 | if ( empty( $file['file'] ) ) { |
||
433 | continue; |
||
434 | } |
||
435 | |||
436 | $download = new \WC_Product_Download(); |
||
437 | $download->set_id( ! empty( $file['id'] ) ? $file['id'] : wp_generate_uuid4() ); |
||
438 | $download->set_name( $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['file'] ) ); |
||
439 | $download->set_file( $file['file'] ); |
||
440 | $files[] = $download; |
||
441 | } |
||
442 | return $files; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Save taxonomy terms. |
||
447 | * |
||
448 | * @param array $terms Terms data. |
||
449 | * @return array |
||
450 | */ |
||
451 | protected function parse_terms_field( $terms ) { |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Save default attributes. |
||
457 | * |
||
458 | * @param array $raw_default_attributes Default attributes. |
||
459 | * @param \WC_Product_Variable $object Product object reference. |
||
460 | * @return array |
||
461 | */ |
||
462 | protected function parse_default_attributes( $raw_default_attributes, $object ) { |
||
507 | } |
||
508 | } |
||
509 |