Skip to content

Commit a2d8578

Browse files
committed
MAGETWO-35824: [MX] [Dragons] Code coverage - Sprint 67
MAGETWO-36990: Coverage or refactoring method
1 parent c8044af commit a2d8578

File tree

1 file changed

+93
-60
lines changed

1 file changed

+93
-60
lines changed

app/code/Magento/ConfigurableProduct/Model/OptionRepository.php

Lines changed: 93 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -151,69 +151,12 @@ public function deleteById($sku, $id)
151151
*/
152152
public function save($sku, \Magento\ConfigurableProduct\Api\Data\OptionInterface $option)
153153
{
154-
/** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
154+
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute */
155155
$configurableAttribute = $this->configurableAttributeFactory->create();
156156
if ($option->getId()) {
157-
/** @var \Magento\Catalog\Model\Product $product */
158-
$product = $this->getProduct($sku);
159-
$configurableAttribute->load($option->getId());
160-
if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
161-
throw new NoSuchEntityException(
162-
__(
163-
'Option with id "%1" not found',
164-
$option->getId()
165-
)
166-
);
167-
}
168-
$configurableAttribute->addData($option->getData());
169-
$configurableAttribute->setValues(
170-
$option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()
171-
);
172-
173-
try {
174-
$configurableAttribute->save();
175-
} catch (\Exception $e) {
176-
throw new CouldNotSaveException(
177-
__(
178-
'Could not update option with id "%1"',
179-
$option->getId()
180-
)
181-
);
182-
}
157+
$this->saveExistingOption($sku, $option, $configurableAttribute);
183158
} else {
184-
$this->validateNewOptionData($option);
185-
/** @var \Magento\Catalog\Model\Product $product */
186-
$product = $this->productRepository->get($sku);
187-
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
188-
if (!in_array($product->getTypeId(), $allowedTypes)) {
189-
throw new \InvalidArgumentException('Incompatible product type');
190-
}
191-
192-
$eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
193-
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
194-
if ($configurableAttribute->getId()) {
195-
throw new CouldNotSaveException(__('Product already has this option'));
196-
}
197-
198-
$configurableAttributesData = [
199-
'attribute_id' => $option->getAttributeId(),
200-
'position' => $option->getPosition(),
201-
'use_default' => $option->getIsUseDefault(),
202-
'label' => $option->getLabel(),
203-
'values' => $option->getValues()
204-
];
205-
206-
try {
207-
$product->setTypeId(ConfigurableType::TYPE_CODE);
208-
$product->setConfigurableAttributesData([$configurableAttributesData]);
209-
$product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
210-
$product->save();
211-
} catch (\Exception $e) {
212-
throw new CouldNotSaveException(__('An error occurred while saving option'));
213-
}
214-
215-
$configurableAttribute = $this->configurableAttributeFactory->create();
216-
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
159+
$this->saveNewOption($sku, $option, $configurableAttribute);
217160
}
218161
if (!$configurableAttribute->getId()) {
219162
throw new CouldNotSaveException(__('An error occurred while saving option'));
@@ -275,4 +218,94 @@ public function validateNewOptionData(\Magento\ConfigurableProduct\Api\Data\Opti
275218
throw $inputException;
276219
}
277220
}
221+
222+
/**
223+
* @param $sku
224+
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface $option
225+
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
226+
* @throws CouldNotSaveException
227+
* @throws InputException
228+
* @throws NoSuchEntityException
229+
* @return void
230+
*/
231+
private function saveExistingOption(
232+
$sku,
233+
\Magento\ConfigurableProduct\Api\Data\OptionInterface $option,
234+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
235+
) {
236+
/** @var \Magento\Catalog\Model\Product $product */
237+
$product = $this->getProduct($sku);
238+
$configurableAttribute->load($option->getId());
239+
if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
240+
throw new NoSuchEntityException(
241+
__(
242+
'Option with id "%1" not found',
243+
$option->getId()
244+
)
245+
);
246+
}
247+
$configurableAttribute->addData($option->getData());
248+
$configurableAttribute->setValues(
249+
$option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()
250+
);
251+
252+
try {
253+
$configurableAttribute->save();
254+
} catch (\Exception $e) {
255+
throw new CouldNotSaveException(
256+
__(
257+
'Could not update option with id "%1"',
258+
$option->getId()
259+
)
260+
);
261+
}
262+
}
263+
264+
/**
265+
* @param $sku
266+
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface $option
267+
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
268+
* @return void
269+
* @throws CouldNotSaveException
270+
* @throws InputException
271+
*/
272+
private function saveNewOption(
273+
$sku,
274+
\Magento\ConfigurableProduct\Api\Data\OptionInterface $option,
275+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
276+
) {
277+
$this->validateNewOptionData($option);
278+
/** @var \Magento\Catalog\Model\Product $product */
279+
$product = $this->productRepository->get($sku);
280+
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
281+
if (!in_array($product->getTypeId(), $allowedTypes)) {
282+
throw new \InvalidArgumentException('Incompatible product type');
283+
}
284+
285+
$eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
286+
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
287+
if ($configurableAttribute->getId()) {
288+
throw new CouldNotSaveException(__('Product already has this option'));
289+
}
290+
291+
$configurableAttributesData = [
292+
'attribute_id' => $option->getAttributeId(),
293+
'position' => $option->getPosition(),
294+
'use_default' => $option->getIsUseDefault(),
295+
'label' => $option->getLabel(),
296+
'values' => $option->getValues()
297+
];
298+
299+
try {
300+
$product->setTypeId(ConfigurableType::TYPE_CODE);
301+
$product->setConfigurableAttributesData([$configurableAttributesData]);
302+
$product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
303+
$product->save();
304+
} catch (\Exception $e) {
305+
throw new CouldNotSaveException(__('An error occurred while saving option'));
306+
}
307+
308+
$configurableAttribute = $this->configurableAttributeFactory->create();
309+
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
310+
}
278311
}

0 commit comments

Comments
 (0)