Skip to content

Commit

Permalink
MAGETWO-35824 [MX] [Dragons] Code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kasian committed May 15, 2015
1 parent 89b0bbb commit fc597c7
Showing 1 changed file with 60 additions and 93 deletions.
153 changes: 60 additions & 93 deletions app/code/Magento/ConfigurableProduct/Model/OptionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,69 @@ public function deleteById($sku, $id)
*/
public function save($sku, \Magento\ConfigurableProduct\Api\Data\OptionInterface $option)
{
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute */
/** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
$configurableAttribute = $this->configurableAttributeFactory->create();
if ($option->getId()) {
$this->saveExistingOption($sku, $option, $configurableAttribute);
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->getProduct($sku);
$configurableAttribute->load($option->getId());
if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
throw new NoSuchEntityException(
__(
'Option with id "%1" not found',
$option->getId()
)
);
}
$configurableAttribute->addData($option->getData());
$configurableAttribute->setValues(
$option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()
);

try {
$configurableAttribute->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(
__(
'Could not update option with id "%1"',
$option->getId()
)
);
}
} else {
$this->saveNewOption($sku, $option, $configurableAttribute);
$this->validateNewOptionData($option);
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productRepository->get($sku);
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
if (!in_array($product->getTypeId(), $allowedTypes)) {
throw new \InvalidArgumentException('Incompatible product type');
}

$eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
if ($configurableAttribute->getId()) {
throw new CouldNotSaveException(__('Product already has this option'));
}

$configurableAttributesData = [
'attribute_id' => $option->getAttributeId(),
'position' => $option->getPosition(),
'use_default' => $option->getIsUseDefault(),
'label' => $option->getLabel(),
'values' => $option->getValues()
];

try {
$product->setTypeId(ConfigurableType::TYPE_CODE);
$product->setConfigurableAttributesData([$configurableAttributesData]);
$product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
$product->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(__('An error occurred while saving option'));
}

$configurableAttribute = $this->configurableAttributeFactory->create();
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
}
if (!$configurableAttribute->getId()) {
throw new CouldNotSaveException(__('An error occurred while saving option'));
Expand Down Expand Up @@ -218,94 +275,4 @@ public function validateNewOptionData(\Magento\ConfigurableProduct\Api\Data\Opti
throw $inputException;
}
}

/**
* @param string $sku
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface $option
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
* @throws CouldNotSaveException
* @throws InputException
* @throws NoSuchEntityException
* @return void
*/
private function saveExistingOption(
$sku,
\Magento\ConfigurableProduct\Api\Data\OptionInterface $option,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
) {
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->getProduct($sku);
$configurableAttribute->load($option->getId());
if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
throw new NoSuchEntityException(
__(
'Option with id "%1" not found',
$option->getId()
)
);
}
$configurableAttribute->addData($option->getData());
$configurableAttribute->setValues(
$option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()
);

try {
$configurableAttribute->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(
__(
'Could not update option with id "%1"',
$option->getId()
)
);
}
}

/**
* @param string $sku
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface $option
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
* @return void
* @throws CouldNotSaveException
* @throws InputException
*/
private function saveNewOption(
$sku,
\Magento\ConfigurableProduct\Api\Data\OptionInterface $option,
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
) {
$this->validateNewOptionData($option);
/** @var \Magento\Catalog\Model\Product $product */
$product = $this->productRepository->get($sku);
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
if (!in_array($product->getTypeId(), $allowedTypes)) {
throw new \InvalidArgumentException('Incompatible product type');
}

$eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
if ($configurableAttribute->getId()) {
throw new CouldNotSaveException(__('Product already has this option'));
}

$configurableAttributesData = [
'attribute_id' => $option->getAttributeId(),
'position' => $option->getPosition(),
'use_default' => $option->getIsUseDefault(),
'label' => $option->getLabel(),
'values' => $option->getValues()
];

try {
$product->setTypeId(ConfigurableType::TYPE_CODE);
$product->setConfigurableAttributesData([$configurableAttributesData]);
$product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
$product->save();
} catch (\Exception $e) {
throw new CouldNotSaveException(__('An error occurred while saving option'));
}

$configurableAttribute = $this->configurableAttributeFactory->create();
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
}
}

0 comments on commit fc597c7

Please sign in to comment.