| Total Complexity | 84 |
| Total Lines | 455 |
| 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() { |
||
| 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 | if ( $classname && class_exists( '\\' . $classname ) ) { |
||
| 60 | $classname = '\\' . $classname; |
||
| 61 | } else { |
||
| 62 | $classname = '\WC_Product_Simple'; |
||
| 63 | } |
||
| 64 | $object = new $classname( $id ); |
||
| 65 | } elseif ( $id ) { |
||
| 66 | $object = wc_get_product( $id ); |
||
| 67 | } else { |
||
| 68 | $object = new \WC_Product_Simple(); |
||
| 69 | } |
||
| 70 | |||
| 71 | if ( ! $object ) { |
||
| 72 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_product_id', __( 'Invalid product.', 'woocommerce' ), 404 ); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ( $object->is_type( 'variation' ) ) { |
||
| 76 | 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 ); |
||
| 77 | } |
||
| 78 | |||
| 79 | return $object; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Set common product props. |
||
| 84 | * |
||
| 85 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
| 86 | */ |
||
| 87 | protected function set_common_props( &$object ) { |
||
| 88 | $props = [ |
||
| 89 | 'name', |
||
| 90 | 'sku', |
||
| 91 | 'description', |
||
| 92 | 'short_description', |
||
| 93 | 'slug', |
||
| 94 | 'menu_order', |
||
| 95 | 'reviews_allowed', |
||
| 96 | 'virtual', |
||
| 97 | 'tax_status', |
||
| 98 | 'tax_class', |
||
| 99 | 'catalog_visibility', |
||
| 100 | 'purchase_note', |
||
| 101 | 'status', |
||
| 102 | 'featured', |
||
| 103 | 'regular_price', |
||
| 104 | 'sale_price', |
||
| 105 | 'date_on_sale_from', |
||
| 106 | 'date_on_sale_from_gmt', |
||
| 107 | 'date_on_sale_to', |
||
| 108 | 'date_on_sale_to_gmt', |
||
| 109 | 'parent_id', |
||
| 110 | 'sold_individually', |
||
| 111 | 'manage_stock', |
||
| 112 | 'backorders', |
||
| 113 | 'stock_status', |
||
| 114 | 'stock_quantity', |
||
| 115 | 'downloadable', |
||
| 116 | 'date_created', |
||
| 117 | 'date_created_gmt', |
||
| 118 | 'upsell_ids', |
||
| 119 | 'cross_sell_ids', |
||
| 120 | 'images', |
||
| 121 | 'categories', |
||
| 122 | 'tags', |
||
| 123 | 'attributes', |
||
| 124 | 'weight', |
||
| 125 | 'dimensions', |
||
| 126 | 'shipping_class', |
||
| 127 | ]; |
||
| 128 | |||
| 129 | $request_props = array_intersect_key( $this->request, array_flip( $props ) ); |
||
| 130 | $prop_values = []; |
||
| 131 | |||
| 132 | foreach ( $request_props as $prop => $value ) { |
||
| 133 | switch ( $prop ) { |
||
| 134 | case 'date_created': |
||
| 135 | case 'date_created_gmt': |
||
| 136 | $prop_values[ $prop ] = rest_parse_date( $value ); |
||
| 137 | break; |
||
| 138 | case 'upsell_ids': |
||
| 139 | case 'cross_sell_ids': |
||
| 140 | $prop_values[ $prop ] = wp_parse_id_list( $value ); |
||
| 141 | break; |
||
| 142 | case 'images': |
||
| 143 | $images = $this->parse_images_field( $value, $object ); |
||
| 144 | $prop_values = array_merge( $prop_values, $images ); |
||
| 145 | break; |
||
| 146 | case 'categories': |
||
| 147 | $prop_values['category_ids'] = $this->parse_terms_field( $value ); |
||
| 148 | break; |
||
| 149 | case 'tags': |
||
| 150 | $prop_values['tag_ids'] = $this->parse_terms_field( $value ); |
||
| 151 | break; |
||
| 152 | case 'attributes': |
||
| 153 | $prop_values['attributes'] = $this->parse_attributes_field( $value ); |
||
| 154 | break; |
||
| 155 | case 'dimensions': |
||
| 156 | $dimensions = $this->parse_dimensions_fields( $value ); |
||
| 157 | $prop_values = array_merge( $prop_values, $dimensions ); |
||
| 158 | break; |
||
| 159 | case 'shipping_class': |
||
| 160 | $prop_values['shipping_class_id'] = $this->parse_shipping_class( $value, $object ); |
||
| 161 | break; |
||
| 162 | default: |
||
| 163 | $prop_values[ $prop ] = $value; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | foreach ( $prop_values as $prop => $value ) { |
||
| 168 | $object->{"set_$prop"}( $value ); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Set grouped product props. |
||
| 174 | * |
||
| 175 | * @param \WC_Product_Grouped $object Product object reference. |
||
| 176 | */ |
||
| 177 | protected function set_grouped_props( &$object ) { |
||
| 178 | $children = $this->get_param( 'grouped_products', null ); |
||
| 179 | |||
| 180 | if ( ! is_null( $children ) ) { |
||
| 181 | $object->set_children( $children ); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Set variable product props. |
||
| 187 | * |
||
| 188 | * @param \WC_Product_Variable $object Product object reference. |
||
| 189 | */ |
||
| 190 | protected function set_variable_props( &$object ) { |
||
| 191 | $default_attributes = $this->get_param( 'default_attributes', null ); |
||
| 192 | |||
| 193 | if ( ! is_null( $default_attributes ) ) { |
||
| 194 | $object->set_default_attributes( $this->parse_default_attributes( $default_attributes, $object ) ); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set external product props. |
||
| 200 | * |
||
| 201 | * @param \WC_Product_External $object Product object reference. |
||
| 202 | */ |
||
| 203 | protected function set_external_props( &$object ) { |
||
| 204 | $button_text = $this->get_param( 'button_text', null ); |
||
| 205 | $external_url = $this->get_param( 'external_url', null ); |
||
| 206 | |||
| 207 | if ( ! is_null( $button_text ) ) { |
||
| 208 | $object->set_button_text( $button_text ); |
||
| 209 | } |
||
| 210 | |||
| 211 | if ( ! is_null( $external_url ) ) { |
||
| 212 | $object->set_product_url( $external_url ); |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Set downloadable product props. |
||
| 218 | * |
||
| 219 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object reference. |
||
| 220 | */ |
||
| 221 | protected function set_downloadable_props( &$object ) { |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Set product object's attributes. |
||
| 241 | * |
||
| 242 | * @param array $raw_attributes Attribute data from request. |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | protected function parse_attributes_field( $raw_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 | |||
| 306 | if ( 0 === $attachment_id && isset( $image['src'] ) ) { |
||
| 307 | $upload = wc_rest_upload_image_from_url( esc_url_raw( $image['src'] ) ); |
||
| 308 | |||
| 309 | if ( is_wp_error( $upload ) ) { |
||
| 310 | if ( ! apply_filters( 'woocommerce_rest_suppress_image_upload_error', false, $upload, $object->get_id(), $images ) ) { |
||
| 311 | throw new \WC_REST_Exception( 'woocommerce_product_image_upload_error', $upload->get_error_message(), 400 ); |
||
| 312 | } else { |
||
| 313 | continue; |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | $attachment_id = wc_rest_set_uploaded_image_as_attachment( $upload, $object->get_id() ); |
||
| 318 | } |
||
| 319 | |||
| 320 | if ( ! wp_attachment_is_image( $attachment_id ) ) { |
||
| 321 | /* translators: %s: image ID */ |
||
| 322 | throw new \WC_REST_Exception( 'woocommerce_product_invalid_image_id', sprintf( __( '#%s is an invalid image ID.', 'woocommerce' ), $attachment_id ), 400 ); |
||
| 323 | } |
||
| 324 | |||
| 325 | if ( 0 === $index ) { |
||
| 326 | $response['image_id'] = $attachment_id; |
||
| 327 | } else { |
||
| 328 | $response['gallery_image_ids'][] = $attachment_id; |
||
| 329 | } |
||
| 330 | |||
| 331 | // Set the image alt if present. |
||
| 332 | if ( ! empty( $image['alt'] ) ) { |
||
| 333 | update_post_meta( $attachment_id, '_wp_attachment_image_alt', wc_clean( $image['alt'] ) ); |
||
| 334 | } |
||
| 335 | |||
| 336 | // Set the image name if present. |
||
| 337 | if ( ! empty( $image['name'] ) ) { |
||
| 338 | wp_update_post( |
||
| 339 | array( |
||
| 340 | 'ID' => $attachment_id, |
||
| 341 | 'post_title' => $image['name'], |
||
| 342 | ) |
||
| 343 | ); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | return $response; |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Parse dimensions. |
||
| 352 | * |
||
| 353 | * @param array $dimensions Product dimensions. |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | protected function parse_dimensions_fields( $dimensions ) { |
||
| 357 | $response = []; |
||
| 358 | |||
| 359 | if ( isset( $dimensions['length'] ) ) { |
||
| 360 | $response['length'] = $dimensions['length']; |
||
| 361 | } |
||
| 362 | |||
| 363 | if ( isset( $dimensions['width'] ) ) { |
||
| 364 | $response['width'] = $dimensions['width']; |
||
| 365 | } |
||
| 366 | |||
| 367 | if ( isset( $dimensions['height'] ) ) { |
||
| 368 | $response['height'] = $dimensions['height']; |
||
| 369 | } |
||
| 370 | |||
| 371 | return $response; |
||
| 372 | } |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Parse shipping class. |
||
| 376 | * |
||
| 377 | * @param string $shipping_class Shipping class slug. |
||
| 378 | * @param \WC_Product_Simple|\WC_Product_Grouped|\WC_Product_Variable|\WC_Product_External $object Product object. |
||
| 379 | * @return int |
||
| 380 | */ |
||
| 381 | protected function parse_shipping_class( $shipping_class, $object ) { |
||
| 382 | $data_store = $object->get_data_store(); |
||
| 383 | return $data_store->get_shipping_class_id_by_slug( wc_clean( $shipping_class ) ); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Parse downloadable files. |
||
| 388 | * |
||
| 389 | * @param array $downloads Downloads data. |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | protected function parse_downloads_field( $downloads ) { |
||
| 393 | $files = array(); |
||
| 394 | foreach ( $downloads as $key => $file ) { |
||
| 395 | if ( empty( $file['file'] ) ) { |
||
| 396 | continue; |
||
| 397 | } |
||
| 398 | |||
| 399 | $download = new \WC_Product_Download(); |
||
| 400 | $download->set_id( ! empty( $file['id'] ) ? $file['id'] : wp_generate_uuid4() ); |
||
| 401 | $download->set_name( $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['file'] ) ); |
||
| 402 | $download->set_file( $file['file'] ); |
||
| 403 | $files[] = $download; |
||
| 404 | } |
||
| 405 | return $files; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Save taxonomy terms. |
||
| 410 | * |
||
| 411 | * @param array $terms Terms data. |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected function parse_terms_field( $terms ) { |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Save default attributes. |
||
| 420 | * |
||
| 421 | * @param array $raw_default_attributes Default attributes. |
||
| 422 | * @param \WC_Product_Variable $object Product object reference. |
||
| 423 | * @return array |
||
| 424 | */ |
||
| 425 | protected function parse_default_attributes( $raw_default_attributes, $object ) { |
||
| 470 | } |
||
| 471 | } |
||
| 472 |