Complex classes like OrderStatusWriter 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 OrderStatusWriter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class OrderStatusWriter implements WriterInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var \Mage_Core_Model_Resource_Transaction |
||
16 | */ |
||
17 | protected $transactionResourceModel; |
||
18 | |||
19 | /** |
||
20 | * @var \Mage_Sales_Model_Order |
||
21 | */ |
||
22 | protected $orderModel; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $options = [ |
||
28 | 'order_id_field' => 'increment_id', |
||
29 | 'send_shipment_email' => true, |
||
30 | 'send_credit_memo_email' => true, |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @param \Mage_Sales_Model_Order $orderModel |
||
35 | * @param \Mage_Core_Model_Resource_Transaction $transactionResourceModel |
||
36 | */ |
||
37 | public function __construct( |
||
44 | |||
45 | /** |
||
46 | * @param array $item |
||
47 | * @return $this |
||
48 | * @throws MagentoSaveException |
||
49 | * @throws \Ddeboer\DataImport\Exception\WriterException |
||
50 | */ |
||
51 | public function writeItem(array $item) |
||
104 | |||
105 | /** |
||
106 | * Create a Credit Memo with the Specified Quantities |
||
107 | * |
||
108 | * @param \Mage_Sales_Model_Order $order |
||
109 | * @param array $quantities |
||
110 | */ |
||
111 | public function ship(\Mage_Sales_Model_Order $order, array $quantities) |
||
139 | |||
140 | /** |
||
141 | * Create a Credit Memo with the Specified Quantities |
||
142 | * |
||
143 | * @param \Mage_Sales_Model_Order $order |
||
144 | * @param array $quantities |
||
145 | */ |
||
146 | public function creditMemo(\Mage_Sales_Model_Order $order, array $quantities) |
||
186 | |||
187 | |||
188 | /** |
||
189 | * @param int $orderId |
||
190 | * @throws WriterException |
||
191 | * @return \Mage_Sales_Model_Order |
||
192 | */ |
||
193 | public function getOrder($orderId) |
||
210 | |||
211 | /** |
||
212 | * If we have an item which has a qty of 7 to be refunded. What this actually means is we |
||
213 | * have refunded a total amount of 7, but part of that qty could have been refunded at an earlier time. |
||
214 | * So we need to get the total of that item already refunded and minus it from the qty to be refunded. |
||
215 | * |
||
216 | * Imagine we receive an refund with qty of 7 to refund. We have already refunded 4 so we want to refund the |
||
217 | * other 3. SO: QtyToRefund - AlreadyRefunded === ActualQtyToRefund. |
||
218 | * |
||
219 | * @param array $alreadyShipped |
||
220 | * @param array $toShip |
||
221 | * @return array |
||
222 | */ |
||
223 | public function getActualShipmentCount(array $alreadyShipped, array $toShip) |
||
239 | |||
240 | /** |
||
241 | * @param \Mage_Sales_Model_Order $order |
||
242 | * @return array |
||
243 | */ |
||
244 | public function getItemsShipped(\Mage_Sales_Model_Order $order) |
||
262 | |||
263 | /** |
||
264 | * @param \Mage_Sales_Model_Order $order |
||
265 | * @param array $items |
||
266 | * @return array |
||
267 | * @throws WriterException |
||
268 | */ |
||
269 | public function validateItemsToBeShipped(\Mage_Sales_Model_Order $order, array $items) |
||
285 | |||
286 | /** |
||
287 | * If we have an item which has a qty of 7 to be refunded. What this actually means is we |
||
288 | * have refunded a total amount of 7, but part of that qty could have been refunded at an earlier time. |
||
289 | * So we need to get the total of that item already refunded and minus it from the qty to be refunded. |
||
290 | * |
||
291 | * Imagine we receive an refund with qty of 7 to refund. We have already refunded 4 so we want to refund the |
||
292 | * other 3. SO: QtyToRefund - AlreadyRefunded === ActualQtyToRefund. |
||
293 | * |
||
294 | * @param array $alreadyRefunded |
||
295 | * @param array $toRefund |
||
296 | * @return array |
||
297 | */ |
||
298 | public function getActualRefundCount(array $alreadyRefunded, array $toRefund) |
||
314 | |||
315 | /** |
||
316 | * @param \Mage_Sales_Model_Order $order |
||
317 | * @return array |
||
318 | */ |
||
319 | public function getItemsRefunded(\Mage_Sales_Model_Order $order) |
||
337 | |||
338 | /** |
||
339 | * @param \Mage_Sales_Model_Order $order |
||
340 | * @param array $items |
||
341 | * @return array |
||
342 | * @throws WriterException |
||
343 | */ |
||
344 | public function validateItemsToBeRefunded(\Mage_Sales_Model_Order $order, array $items) |
||
360 | |||
361 | /** |
||
362 | * Wrap up the writer after all items have been written |
||
363 | * |
||
364 | * @return WriterInterface |
||
365 | */ |
||
366 | public function finish() |
||
369 | |||
370 | /** |
||
371 | * Prepare the writer before writing the items |
||
372 | * |
||
373 | * @return WriterInterface |
||
374 | */ |
||
375 | public function prepare() |
||
378 | |||
379 | /** |
||
380 | * @param \Mage_Sales_Model_Order $order |
||
381 | * @return \Mage_Sales_Model_Service_Order |
||
382 | */ |
||
383 | public function getServiceForOrder(\Mage_Sales_Model_Order $order) |
||
387 | } |
||
388 |