Conditions | 11 |
Paths | 57 |
Total Lines | 60 |
Code Lines | 36 |
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 |
||
88 | public function writeItem(array $item) |
||
89 | { |
||
90 | if (!isset($item['product_id'])) { |
||
91 | throw new WriterException("No product Id Found"); |
||
92 | } |
||
93 | |||
94 | $id = $item['product_id']; |
||
95 | |||
96 | if (!isset($item['qty'])) { |
||
97 | throw new WriterException( |
||
98 | sprintf('No Quantity found for Product: "%s". Using field "qty"', $id) |
||
99 | ); |
||
100 | } |
||
101 | |||
102 | //If Given a sku as the Product ID field, we need to get the product ID |
||
103 | //from the actual product |
||
104 | $product = clone $this->productModel; |
||
105 | switch ($this->options['productIdField']) { |
||
106 | case 'sku': |
||
107 | $productId = $product->getIdBySku($id); |
||
108 | if (!$productId) { |
||
109 | throw new WriterException( |
||
110 | sprintf('Product not found with SKU: "%s"', $id) |
||
111 | ); |
||
112 | } |
||
113 | break; |
||
114 | case 'id': |
||
115 | default: |
||
116 | //default to assume just using product_id |
||
117 | $productId = $id; |
||
118 | break; |
||
119 | } |
||
120 | |||
121 | $product->load($productId); |
||
122 | $stockItem = $product->getData('stock_item'); |
||
123 | |||
124 | switch ($this->options['stockUpdateType']) { |
||
125 | case self::STOCK_UPDATE_TYPE_ADD: |
||
126 | $stockItem->setData('qty', $stockItem->getData('qty') + $item['qty']); |
||
127 | break; |
||
128 | case self::STOCK_UPDATE_TYPE_SET: |
||
129 | $stockItem->setData('qty', $item['qty']); |
||
130 | break; |
||
131 | } |
||
132 | |||
133 | if ($this->options['updateStockStatusIfInStock']) { |
||
134 | // set item to in stock if the new qty matches or is greater than min qty in the config |
||
135 | if ($item['qty'] >= $stockItem->getMinQty()) { |
||
136 | $stockItem->setData('is_in_stock', 1); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | try { |
||
141 | $stockItem->save(); |
||
142 | } catch (\Exception $e) { |
||
143 | throw new MagentoSaveException($e); |
||
144 | } |
||
145 | |||
146 | return $this; |
||
147 | } |
||
148 | } |
||
149 |