Total Complexity | 73 |
Total Lines | 371 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like OrderRequest 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 OrderRequest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class OrderRequest extends AbstractRequest { |
||
16 | |||
17 | /** |
||
18 | * Convert request to object. |
||
19 | * |
||
20 | * @return \WC_Order |
||
21 | */ |
||
22 | public function prepare_object() { |
||
23 | $id = (int) $this->get_param( 'id', 0 ); |
||
24 | $object = new \WC_Order( $id ); |
||
25 | |||
26 | $this->set_props( $object ); |
||
27 | $this->set_meta_data( $object ); |
||
28 | $this->set_line_items( $object ); |
||
29 | $this->calculate_coupons( $object ); |
||
30 | |||
31 | return $object; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Set order props. |
||
36 | * |
||
37 | * @param \WC_Order $object Order object reference. |
||
38 | */ |
||
39 | protected function set_props( &$object ) { |
||
40 | $props = [ |
||
41 | 'parent_id', |
||
42 | 'currency', |
||
43 | 'customer_id', |
||
44 | 'customer_note', |
||
45 | 'payment_method', |
||
46 | 'payment_method_title', |
||
47 | 'transaction_id', |
||
48 | 'billing', |
||
49 | 'shipping', |
||
50 | 'status', |
||
51 | ]; |
||
52 | |||
53 | $request_props = array_intersect_key( $this->request, array_flip( $props ) ); |
||
54 | $prop_values = []; |
||
55 | |||
56 | foreach ( $request_props as $prop => $value ) { |
||
57 | switch ( $prop ) { |
||
58 | case 'customer_id': |
||
59 | $prop_values[ $prop ] = $this->parse_customer_id_field( $value ); |
||
60 | break; |
||
61 | case 'billing': |
||
62 | case 'shipping': |
||
63 | $address = $this->parse_address_field( $value, $object, $prop ); |
||
64 | $prop_values = array_merge( $prop_values, $address ); |
||
65 | break; |
||
66 | default: |
||
67 | $prop_values[ $prop ] = $value; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | foreach ( $prop_values as $prop => $value ) { |
||
72 | $object->{"set_$prop"}( $value ); |
||
73 | } |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Set order line items. |
||
78 | * |
||
79 | * @param \WC_Order $object Order object reference. |
||
80 | */ |
||
81 | protected function set_line_items( &$object ) { |
||
82 | $types = [ |
||
83 | 'line_items', |
||
84 | 'shipping_lines', |
||
85 | 'fee_lines', |
||
86 | ]; |
||
87 | |||
88 | foreach ( $types as $type ) { |
||
89 | if ( ! isset( $this->request[ $type ] ) || ! is_array( $this->request[ $type ] ) ) { |
||
90 | continue; |
||
91 | } |
||
92 | $items = $this->request[ $type ]; |
||
93 | |||
94 | foreach ( $items as $item ) { |
||
95 | if ( ! is_array( $item ) ) { |
||
96 | continue; |
||
97 | } |
||
98 | if ( $this->item_is_null( $item ) || ( isset( $item['quantity'] ) && 0 === $item['quantity'] ) ) { |
||
99 | $object->remove_item( $item['id'] ); |
||
100 | } else { |
||
101 | $this->set_item( $object, $type, $item ); |
||
102 | } |
||
103 | } |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Helper method to check if the resource ID associated with the provided item is null. |
||
109 | * Items can be deleted by setting the resource ID to null. |
||
110 | * |
||
111 | * @param array $item Item provided in the request body. |
||
112 | * @return bool True if the item resource ID is null, false otherwise. |
||
113 | */ |
||
114 | protected function item_is_null( $item ) { |
||
115 | $keys = array( 'product_id', 'method_id', 'method_title', 'name', 'code' ); |
||
116 | |||
117 | foreach ( $keys as $key ) { |
||
118 | if ( array_key_exists( $key, $item ) && is_null( $item[ $key ] ) ) { |
||
119 | return true; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return false; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Maybe set an item prop if the value was posted. |
||
128 | * |
||
129 | * @param WC_Order_Item $item Order item. |
||
|
|||
130 | * @param string $prop Order property. |
||
131 | * @param array $posted Request data. |
||
132 | */ |
||
133 | protected function maybe_set_item_prop( $item, $prop, $posted ) { |
||
134 | if ( isset( $posted[ $prop ] ) ) { |
||
135 | $item->{"set_$prop"}( $posted[ $prop ] ); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Maybe set item props if the values were posted. |
||
141 | * |
||
142 | * @param WC_Order_Item $item Order item data. |
||
143 | * @param string[] $props Properties. |
||
144 | * @param array $posted Request data. |
||
145 | */ |
||
146 | protected function maybe_set_item_props( $item, $props, $posted ) { |
||
149 | } |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Maybe set item meta if posted. |
||
154 | * |
||
155 | * @param WC_Order_Item $item Order item data. |
||
156 | * @param array $posted Request data. |
||
157 | */ |
||
158 | protected function maybe_set_item_meta_data( $item, $posted ) { |
||
159 | if ( ! empty( $posted['meta_data'] ) && is_array( $posted['meta_data'] ) ) { |
||
160 | foreach ( $posted['meta_data'] as $meta ) { |
||
161 | if ( isset( $meta['key'] ) ) { |
||
162 | $value = isset( $meta['value'] ) ? $meta['value'] : null; |
||
163 | $item->update_meta_data( $meta['key'], $value, isset( $meta['id'] ) ? $meta['id'] : '' ); |
||
164 | } |
||
165 | } |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Gets the product ID from the SKU or posted ID. |
||
171 | * |
||
172 | * @param array $posted Request data. |
||
173 | * @return int |
||
174 | * @throws \WC_REST_Exception When SKU or ID is not valid. |
||
175 | */ |
||
176 | protected function get_product_id_from_line_item( $posted ) { |
||
177 | if ( ! empty( $posted['sku'] ) ) { |
||
178 | $product_id = (int) wc_get_product_id_by_sku( $posted['sku'] ); |
||
179 | } elseif ( ! empty( $posted['product_id'] ) && empty( $posted['variation_id'] ) ) { |
||
180 | $product_id = (int) $posted['product_id']; |
||
181 | } elseif ( ! empty( $posted['variation_id'] ) ) { |
||
182 | $product_id = (int) $posted['variation_id']; |
||
183 | } else { |
||
184 | throw new \WC_REST_Exception( 'woocommerce_rest_required_product_reference', __( 'Product ID or SKU is required.', 'woocommerce' ), 400 ); |
||
185 | } |
||
186 | return $product_id; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Create or update a line item. |
||
191 | * |
||
192 | * @param array $posted Line item data. |
||
193 | * @param string $action 'create' to add line item or 'update' to update it. |
||
194 | * @param object $item Passed when updating an item. Null during creation. |
||
195 | * @return WC_Order_Item_Product |
||
196 | * @throws WC_REST_Exception Invalid data, server error. |
||
197 | */ |
||
198 | protected function prepare_line_items( $posted, $action = 'create', $item = null ) { |
||
199 | $item = is_null( $item ) ? new \WC_Order_Item_Product( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; |
||
200 | $product = wc_get_product( $this->get_product_id_from_line_item( $posted ) ); |
||
201 | |||
202 | if ( $product !== $item->get_product() ) { |
||
203 | $item->set_product( $product ); |
||
204 | |||
205 | if ( 'create' === $action ) { |
||
206 | $quantity = isset( $posted['quantity'] ) ? $posted['quantity'] : 1; |
||
207 | $total = wc_get_price_excluding_tax( $product, array( 'qty' => $quantity ) ); |
||
208 | $item->set_total( $total ); |
||
209 | $item->set_subtotal( $total ); |
||
210 | } |
||
211 | } |
||
212 | |||
213 | $this->maybe_set_item_props( $item, array( 'name', 'quantity', 'total', 'subtotal', 'tax_class' ), $posted ); |
||
214 | $this->maybe_set_item_meta_data( $item, $posted ); |
||
215 | |||
216 | return $item; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Create or update an order shipping method. |
||
221 | * |
||
222 | * @param array $posted $shipping Item data. |
||
223 | * @param string $action 'create' to add shipping or 'update' to update it. |
||
224 | * @param object $item Passed when updating an item. Null during creation. |
||
225 | * @return \WC_Order_Item_Shipping |
||
226 | * @throws \WC_REST_Exception Invalid data, server error. |
||
227 | */ |
||
228 | protected function prepare_shipping_lines( $posted, $action = 'create', $item = null ) { |
||
229 | $item = is_null( $item ) ? new \WC_Order_Item_Shipping( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; |
||
230 | |||
231 | if ( 'create' === $action ) { |
||
232 | if ( empty( $posted['method_id'] ) ) { |
||
233 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_shipping_item', __( 'Shipping method ID is required.', 'woocommerce' ), 400 ); |
||
234 | } |
||
235 | } |
||
236 | |||
237 | $this->maybe_set_item_props( $item, array( 'method_id', 'method_title', 'total' ), $posted ); |
||
238 | $this->maybe_set_item_meta_data( $item, $posted ); |
||
239 | |||
240 | return $item; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Create or update an order fee. |
||
245 | * |
||
246 | * @param array $posted Item data. |
||
247 | * @param string $action 'create' to add fee or 'update' to update it. |
||
248 | * @param object $item Passed when updating an item. Null during creation. |
||
249 | * @return \WC_Order_Item_Fee |
||
250 | * @throws \WC_REST_Exception Invalid data, server error. |
||
251 | */ |
||
252 | protected function prepare_fee_lines( $posted, $action = 'create', $item = null ) { |
||
253 | $item = is_null( $item ) ? new \WC_Order_Item_Fee( ! empty( $posted['id'] ) ? $posted['id'] : '' ) : $item; |
||
254 | |||
255 | if ( 'create' === $action ) { |
||
256 | if ( empty( $posted['name'] ) ) { |
||
257 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_fee_item', __( 'Fee name is required.', 'woocommerce' ), 400 ); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | $this->maybe_set_item_props( $item, array( 'name', 'tax_class', 'tax_status', 'total' ), $posted ); |
||
262 | $this->maybe_set_item_meta_data( $item, $posted ); |
||
263 | |||
264 | return $item; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Wrapper method to create/update order items. |
||
269 | * When updating, the item ID provided is checked to ensure it is associated |
||
270 | * with the order. |
||
271 | * |
||
272 | * @param \WC_Order $order order object. |
||
273 | * @param string $item_type The item type. |
||
274 | * @param array $posted item provided in the request body. |
||
275 | * @throws \WC_REST_Exception If item ID is not associated with order. |
||
276 | */ |
||
277 | protected function set_item( &$order, $item_type, $posted ) { |
||
278 | if ( ! empty( $posted['id'] ) ) { |
||
279 | $action = 'update'; |
||
280 | } else { |
||
281 | $action = 'create'; |
||
282 | } |
||
283 | |||
284 | $method = 'prepare_' . $item_type; |
||
285 | $item = null; |
||
286 | |||
287 | // Verify provided line item ID is associated with order. |
||
288 | if ( 'update' === $action ) { |
||
289 | $item = $order->get_item( absint( $posted['id'] ), false ); |
||
290 | |||
291 | if ( ! $item ) { |
||
292 | throw new \WC_REST_Exception( 'woocommerce_rest_invalid_item_id', __( 'Order item ID provided is not associated with order.', 'woocommerce' ), 400 ); |
||
293 | } |
||
294 | } |
||
295 | |||
296 | // Prepare item data. |
||
297 | $item = $this->$method( $posted, $action, $item ); |
||
298 | |||
299 | do_action( 'woocommerce_rest_set_order_item', $item, $posted ); |
||
300 | |||
301 | // If creating the order, add the item to it. |
||
302 | if ( 'create' === $action ) { |
||
303 | $order->add_item( $item ); |
||
304 | } else { |
||
305 | $item->save(); |
||
306 | } |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Parse address data. |
||
311 | * |
||
312 | * @param array $data Posted data. |
||
313 | * @param \WC_Order $object Order object reference. |
||
314 | * @param string $type Address type. |
||
315 | * @return array |
||
316 | */ |
||
317 | protected function parse_address_field( $data, $object, $type = 'billing' ) { |
||
318 | $address = []; |
||
319 | foreach ( $data as $key => $value ) { |
||
320 | if ( is_callable( array( $object, "set_{$type}_{$key}" ) ) ) { |
||
321 | $address[ "{$type}_{$key}" ] = $value; |
||
322 | } |
||
323 | } |
||
324 | return $address; |
||
325 | } |
||
326 | |||
327 | /** |
||
328 | * Parse customer ID. |
||
329 | * |
||
330 | * @throws \WC_REST_Exception Will throw an exception if the customer is invalid. |
||
331 | * @param int $customer_id Customer ID to set. |
||
332 | * @return int |
||
333 | */ |
||
334 | protected function parse_customer_id_field( $customer_id ) { |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Calculate coupons. |
||
351 | * |
||
352 | * @throws \WC_REST_Exception When fails to set any item. |
||
353 | * |
||
354 | * @param \WC_Order $order Order data. |
||
355 | * @return bool |
||
356 | */ |
||
357 | protected function calculate_coupons( &$order ) { |
||
386 | } |
||
387 | } |
||
388 |