Conditions | 8 |
Paths | 19 |
Total Lines | 74 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
52 | public function writeItem(array $item) |
||
53 | { |
||
54 | if (!isset($item['orderId'])) { |
||
55 | throw new WriterException('order_id must be set'); |
||
56 | } |
||
57 | |||
58 | $order = $this->getOrder($item['orderId']); |
||
59 | $service = $this->getServiceForOrder($order); |
||
60 | |||
61 | $quantities = $this->validateItemsToBeRefunded($order, $item['items']); |
||
62 | $alreadyReturned = $this->getItemsRefunded($order); |
||
63 | //TODO: Make this configurable - Some Returns will not include the total qty's returned |
||
64 | //TODO: Just the exact qty to return. |
||
65 | $returnQuantities = $this->getActualRefundCount($alreadyReturned, $quantities); |
||
66 | |||
67 | if (!count($returnQuantities)) { |
||
68 | throw new WriterException( |
||
69 | sprintf( |
||
70 | 'Credit Memo cannot be created with no Items to Refund. Order ID: "%s"', |
||
71 | $item['orderId'] |
||
72 | ) |
||
73 | ); |
||
74 | } |
||
75 | |||
76 | try { |
||
77 | /** @var \Mage_Sales_Model_Order_Creditmemo $creditMemo */ |
||
78 | $creditMemo = $service->prepareCreditmemo([ |
||
79 | 'qtys' => $returnQuantities, |
||
80 | //TODO: Make this configurable - have an option whether to refund shipping or not |
||
81 | //TODO: if yes, then grab the amount from the input data |
||
82 | 'shipping_amount' => 0, |
||
83 | ]); |
||
84 | } catch (\Mage_Core_Exception $e) { |
||
85 | //Probably something to do trying to refund |
||
86 | //quantities which don't add up |
||
87 | throw new MagentoSaveException($e->getMessage()); |
||
88 | } |
||
89 | |||
90 | //don't actually perform refund. |
||
91 | //TODO: Make this configurable ^ |
||
92 | $creditMemo->addData([ |
||
93 | 'offline_requested' => true, |
||
94 | ]); |
||
95 | |||
96 | if (isset($item['comment'])) { |
||
97 | $creditMemo->addComment($item['comment']); |
||
98 | } |
||
99 | |||
100 | $creditMemo->register(); |
||
101 | |||
102 | try { |
||
103 | $transactionSave = clone $this->transactionResourceModel; |
||
104 | $transactionSave |
||
105 | ->addObject($creditMemo) |
||
106 | ->addObject($creditMemo->getOrder()) |
||
107 | ->save(); |
||
108 | |||
109 | //if there is a custom status for the order |
||
110 | //set it - we do it here, because somewhere in the save process |
||
111 | //Magento sets the order to complete. |
||
112 | if (isset($item['orderStatus'])) { |
||
113 | //$order->setStatus(strtolower($item['orderStatus'])); |
||
114 | $order->addStatusHistoryComment("Returned", strtolower($item['orderStatus'])); |
||
115 | $order->save(); |
||
116 | } |
||
117 | |||
118 | } catch (\Exception $e) { |
||
119 | throw new MagentoSaveException($e->getMessage()); |
||
120 | } |
||
121 | |||
122 | if ($this->options['send_credit_memo_email']) { |
||
123 | $creditMemo->sendEmail(true); |
||
124 | } |
||
125 | } |
||
126 | |||
235 |