Total Complexity | 80 |
Total Lines | 422 |
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 |
||
17 | class ProductRequest extends AbstractObjectRequest { |
||
18 | |||
19 | /** |
||
20 | * Convert request to object. |
||
21 | * |
||
22 | * @return \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External |
||
23 | */ |
||
24 | public function prepare_object() { |
||
25 | $object = $this->get_product_object(); |
||
26 | |||
27 | $this->set_common_props( $object ); |
||
28 | $this->set_meta_data( $object ); |
||
29 | |||
30 | switch ( $object->get_type() ) { |
||
31 | case 'grouped': |
||
32 | $this->set_grouped_props( $object ); |
||
33 | break; |
||
34 | case 'variable': |
||
35 | $this->set_variable_props( $object ); |
||
36 | break; |
||
37 | case 'external': |
||
38 | $this->set_external_props( $object ); |
||
39 | break; |
||
40 | } |
||
41 | |||
42 | if ( $object->get_downloadable() ) { |
||
43 | $this->set_downloadable_props( $object ); |
||
44 | } |
||
45 | |||
46 | return $object; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get product object from request args. |
||
51 | * |
||
52 | * @throws \WC_REST_Exception Will throw an exception if the resulting product object is invalid. |
||
53 | * @return \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External |
||
54 | */ |
||
55 | protected function get_product_object() { |
||
56 | $id = (int) $this->get_param( 'id', 0 ); |
||
57 | $type = $this->get_param( 'type', '' ); |
||
58 | |||
59 | if ( $type ) { |
||
60 | $classname = \WC_Product_Factory::get_classname_from_product_type( $type ); |
||
61 | if ( $classname && class_exists( '\\' . $classname ) ) { |
||
62 | $classname = '\\' . $classname; |
||
63 | } else { |
||
64 | $classname = '\WC_Product_Simple'; |
||
65 | } |
||
66 | $object = new $classname( $id ); |
||
67 | } elseif ( $id ) { |
||
68 | $object = wc_get_product( $id ); |
||
69 | } else { |
||
70 | $object = new \WC_Product_Simple(); |
||
71 | } |
||
72 | |||
73 | if ( ! $object ) { |
||
74 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_product_id', __( 'Invalid product.', 'woocommerce-rest-api' ), 404 ); |
||
75 | } |
||
76 | |||
77 | if ( $object->is_type( 'variation' ) ) { |
||
78 | 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-rest-api' ), 404 ); |
||
79 | } |
||
80 | |||
81 | return $object; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Set common product props. |
||
86 | * |
||
87 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
88 | */ |
||
89 | protected function set_common_props( &$object ) { |
||
171 | } |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Set grouped product props. |
||
176 | * |
||
177 | * @param \WC_Product_Grouped $object Product object reference. |
||
178 | */ |
||
179 | protected function set_grouped_props( &$object ) { |
||
180 | $children = $this->get_param( 'grouped_products', null ); |
||
181 | |||
182 | if ( ! is_null( $children ) ) { |
||
183 | $object->set_children( $children ); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * Set variable product props. |
||
189 | * |
||
190 | * @param \WC_Product_Variable $object Product object reference. |
||
191 | */ |
||
192 | protected function set_variable_props( &$object ) { |
||
193 | $default_attributes = $this->get_param( 'default_attributes', null ); |
||
194 | |||
195 | if ( ! is_null( $default_attributes ) ) { |
||
196 | $object->set_default_attributes( $this->parse_default_attributes( $default_attributes, $object ) ); |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set external product props. |
||
202 | * |
||
203 | * @param \WC_Product_External $object Product object reference. |
||
204 | */ |
||
205 | protected function set_external_props( &$object ) { |
||
206 | $button_text = $this->get_param( 'button_text', null ); |
||
207 | $external_url = $this->get_param( 'external_url', null ); |
||
208 | |||
209 | if ( ! is_null( $button_text ) ) { |
||
210 | $object->set_button_text( $button_text ); |
||
211 | } |
||
212 | |||
213 | if ( ! is_null( $external_url ) ) { |
||
214 | $object->set_product_url( $external_url ); |
||
215 | } |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Set downloadable product props. |
||
220 | * |
||
221 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
222 | */ |
||
223 | protected function set_downloadable_props( &$object ) { |
||
224 | $download_limit = $this->get_param( 'download_limit', null ); |
||
225 | $download_expiry = $this->get_param( 'download_expiry', null ); |
||
226 | $downloads = $this->get_param( 'downloads', null ); |
||
227 | |||
228 | if ( ! is_null( $download_limit ) ) { |
||
229 | $object->set_download_limit( $download_limit ); |
||
230 | } |
||
231 | |||
232 | if ( ! is_null( $download_expiry ) ) { |
||
233 | $object->set_download_expiry( $download_expiry ); |
||
234 | } |
||
235 | |||
236 | if ( ! is_null( $downloads ) ) { |
||
237 | $object->set_downloads( $this->parse_downloads_field( $downloads ) ); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Set product object's attributes. |
||
243 | * |
||
244 | * @param array $raw_attributes Attribute data from request. |
||
245 | * @return array |
||
246 | */ |
||
247 | protected function parse_attributes_field( $raw_attributes ) { |
||
248 | $attributes = array(); |
||
249 | |||
250 | foreach ( $raw_attributes as $attribute ) { |
||
251 | if ( ! empty( $attribute['id'] ) ) { |
||
252 | $attribute_id = absint( $attribute['id'] ); |
||
253 | $attribute_name = wc_attribute_taxonomy_name_by_id( $attribute_id ); |
||
254 | } elseif ( ! empty( $attribute['name'] ) ) { |
||
255 | $attribute_id = 0; |
||
256 | $attribute_name = wc_clean( $attribute['name'] ); |
||
257 | } |
||
258 | |||
259 | if ( ! $attribute_name || ! isset( $attribute['options'] ) ) { |
||
260 | continue; |
||
261 | } |
||
262 | |||
263 | if ( ! is_array( $attribute['options'] ) ) { |
||
264 | $attribute['options'] = explode( \WC_DELIMITER, $attribute['options'] ); |
||
|
|||
265 | } |
||
266 | |||
267 | if ( $attribute_id ) { |
||
268 | $attribute['options'] = array_filter( array_map( 'wc_sanitize_term_text_based', $attribute['options'] ), 'strlen' ); |
||
269 | } |
||
270 | |||
271 | $attribute_object = new \WC_Product_Attribute(); |
||
272 | $attribute_object->set_id( $attribute_id ); |
||
273 | $attribute_object->set_name( $attribute_name ); |
||
274 | $attribute_object->set_options( $attribute['options'] ); |
||
275 | $attribute_object->set_position( isset( $attribute['position'] ) ? (string) absint( $attribute['position'] ) : '0' ); |
||
276 | $attribute_object->set_visible( ! empty( $attribute['visible'] ) ? 1 : 0 ); |
||
277 | $attribute_object->set_variation( ! empty( $attribute['variation'] ) ? 1 : 0 ); |
||
278 | $attributes[] = $attribute_object; |
||
279 | } |
||
280 | return $attributes; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Set product images. |
||
285 | * |
||
286 | * @throws \WC_REST_Exception REST API exceptions. |
||
287 | * @param array $images Images data. |
||
288 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object. |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function parse_images_field( $images, $object ) { |
||
292 | $response = [ |
||
293 | 'image_id' => '', |
||
294 | 'gallery_image_ids' => [], |
||
295 | ]; |
||
296 | |||
297 | $images = is_array( $images ) ? array_filter( $images ) : []; |
||
298 | |||
299 | if ( empty( $images ) ) { |
||
300 | return $response; |
||
301 | } |
||
302 | |||
303 | foreach ( $images as $index => $image ) { |
||
304 | $attachment_id = isset( $image['id'] ) ? absint( $image['id'] ) : 0; |
||
305 | $attachment = new ImageAttachment( $attachment_id, $object->get_id() ); |
||
306 | |||
307 | if ( 0 === $attachment->id && ! empty( $image['src'] ) ) { |
||
308 | $attachment->upload_image_from_src( $image['src'] ); |
||
309 | } |
||
310 | |||
311 | if ( ! empty( $image['alt'] ) ) { |
||
312 | $attachment->update_alt_text( $image['alt'] ); |
||
313 | } |
||
314 | |||
315 | if ( ! empty( $image['name'] ) ) { |
||
316 | $attachment->update_name( $image['name'] ); |
||
317 | } |
||
318 | |||
319 | if ( 0 === $index ) { |
||
320 | $response['image_id'] = $attachment->id; |
||
321 | } else { |
||
322 | $response['gallery_image_ids'][] = $attachment->id; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | return $response; |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Parse dimensions. |
||
331 | * |
||
332 | * @param array $dimensions Product dimensions. |
||
333 | * @return array |
||
334 | */ |
||
335 | protected function parse_dimensions_fields( $dimensions ) { |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * Parse shipping class. |
||
355 | * |
||
356 | * @param string $shipping_class Shipping class slug. |
||
357 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object. |
||
358 | * @return int |
||
359 | */ |
||
360 | protected function parse_shipping_class( $shipping_class, $object ) { |
||
361 | $data_store = $object->get_data_store(); |
||
362 | return $data_store->get_shipping_class_id_by_slug( wc_clean( $shipping_class ) ); |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Parse downloadable files. |
||
367 | * |
||
368 | * @param array $downloads Downloads data. |
||
369 | * @return array |
||
370 | */ |
||
371 | protected function parse_downloads_field( $downloads ) { |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Save default attributes. |
||
389 | * |
||
390 | * @param array $raw_default_attributes Default attributes. |
||
391 | * @param \WC_Product_Variable $object Product object reference. |
||
392 | * @return array |
||
393 | */ |
||
394 | protected function parse_default_attributes( $raw_default_attributes, $object ) { |
||
441 |