OptionRepository.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Bundle\Model;
  8. use Magento\Bundle\Model\Option\SaveAction;
  9. use Magento\Catalog\Api\Data\ProductInterface;
  10. use Magento\Framework\Exception\InputException;
  11. use Magento\Framework\Exception\NoSuchEntityException;
  12. /**
  13. * Repository for performing CRUD operations for a bundle product's options.
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class OptionRepository implements \Magento\Bundle\Api\ProductOptionRepositoryInterface
  18. {
  19. /**
  20. * @var \Magento\Catalog\Api\ProductRepositoryInterface
  21. */
  22. protected $productRepository;
  23. /**
  24. * @var Product\Type
  25. */
  26. protected $type;
  27. /**
  28. * @var \Magento\Bundle\Api\Data\OptionInterfaceFactory
  29. */
  30. protected $optionFactory;
  31. /**
  32. * @var \Magento\Bundle\Model\ResourceModel\Option
  33. */
  34. protected $optionResource;
  35. /**
  36. * @var \Magento\Bundle\Api\ProductLinkManagementInterface
  37. */
  38. protected $linkManagement;
  39. /**
  40. * @var Product\OptionList
  41. */
  42. protected $productOptionList;
  43. /**
  44. * @var \Magento\Framework\Api\DataObjectHelper
  45. */
  46. protected $dataObjectHelper;
  47. /**
  48. * @var SaveAction
  49. */
  50. private $optionSave;
  51. /**
  52. * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
  53. * @param Product\Type $type
  54. * @param \Magento\Bundle\Api\Data\OptionInterfaceFactory $optionFactory
  55. * @param \Magento\Bundle\Model\ResourceModel\Option $optionResource
  56. * @param \Magento\Bundle\Api\ProductLinkManagementInterface $linkManagement
  57. * @param Product\OptionList $productOptionList
  58. * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
  59. * @param SaveAction $optionSave
  60. */
  61. public function __construct(
  62. \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
  63. \Magento\Bundle\Model\Product\Type $type,
  64. \Magento\Bundle\Api\Data\OptionInterfaceFactory $optionFactory,
  65. \Magento\Bundle\Model\ResourceModel\Option $optionResource,
  66. \Magento\Bundle\Api\ProductLinkManagementInterface $linkManagement,
  67. \Magento\Bundle\Model\Product\OptionList $productOptionList,
  68. \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
  69. SaveAction $optionSave
  70. ) {
  71. $this->productRepository = $productRepository;
  72. $this->type = $type;
  73. $this->optionFactory = $optionFactory;
  74. $this->optionResource = $optionResource;
  75. $this->linkManagement = $linkManagement;
  76. $this->productOptionList = $productOptionList;
  77. $this->dataObjectHelper = $dataObjectHelper;
  78. $this->optionSave = $optionSave;
  79. }
  80. /**
  81. * @inheritdoc
  82. */
  83. public function get($sku, $optionId)
  84. {
  85. $product = $this->getProduct($sku);
  86. /** @var \Magento\Bundle\Model\Option $option */
  87. $option = $this->type->getOptionsCollection($product)->getItemById($optionId);
  88. if (!$option || !$option->getId()) {
  89. throw new NoSuchEntityException(
  90. __("The option that was requested doesn't exist. Verify the entity and try again.")
  91. );
  92. }
  93. $productLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
  94. /** @var \Magento\Bundle\Api\Data\OptionInterface $optionDataObject */
  95. $optionDataObject = $this->optionFactory->create();
  96. $this->dataObjectHelper->populateWithArray(
  97. $optionDataObject,
  98. $option->getData(),
  99. \Magento\Bundle\Api\Data\OptionInterface::class
  100. );
  101. $optionDataObject->setOptionId($option->getId());
  102. $optionDataObject->setTitle($option->getTitle() === null ? $option->getDefaultTitle() : $option->getTitle());
  103. $optionDataObject->setSku($product->getSku());
  104. $optionDataObject->setProductLinks($productLinks);
  105. return $optionDataObject;
  106. }
  107. /**
  108. * @inheritdoc
  109. */
  110. public function getList($sku)
  111. {
  112. $product = $this->getProduct($sku);
  113. return $this->getListByProduct($product);
  114. }
  115. /**
  116. * Return list of product options
  117. *
  118. * @param ProductInterface $product
  119. * @return \Magento\Bundle\Api\Data\OptionInterface[]
  120. */
  121. public function getListByProduct(ProductInterface $product)
  122. {
  123. return $this->productOptionList->getItems($product);
  124. }
  125. /**
  126. * @inheritdoc
  127. */
  128. public function delete(\Magento\Bundle\Api\Data\OptionInterface $option)
  129. {
  130. try {
  131. $this->optionResource->delete($option);
  132. } catch (\Exception $exception) {
  133. throw new \Magento\Framework\Exception\StateException(
  134. __('The option with "%1" ID can\'t be deleted.', $option->getOptionId()),
  135. $exception
  136. );
  137. }
  138. return true;
  139. }
  140. /**
  141. * @inheritdoc
  142. */
  143. public function deleteById($sku, $optionId)
  144. {
  145. /** @var \Magento\Bundle\Api\Data\OptionInterface $option */
  146. $option = $this->get($sku, $optionId);
  147. $hasBeenDeleted = $this->delete($option);
  148. return $hasBeenDeleted;
  149. }
  150. /**
  151. * @inheritdoc
  152. */
  153. public function save(
  154. \Magento\Catalog\Api\Data\ProductInterface $product,
  155. \Magento\Bundle\Api\Data\OptionInterface $option
  156. ) {
  157. $savedOption = $this->optionSave->save($product, $option);
  158. $productToSave = $this->productRepository->get($product->getSku());
  159. $this->productRepository->save($productToSave);
  160. return $savedOption->getOptionId();
  161. }
  162. /**
  163. * Update option selections
  164. *
  165. * @param \Magento\Catalog\Api\Data\ProductInterface $product
  166. * @param \Magento\Bundle\Api\Data\OptionInterface $option
  167. * @return $this
  168. * @throws InputException
  169. * @throws NoSuchEntityException
  170. * @throws \Magento\Framework\Exception\CouldNotSaveException
  171. */
  172. protected function updateOptionSelection(
  173. \Magento\Catalog\Api\Data\ProductInterface $product,
  174. \Magento\Bundle\Api\Data\OptionInterface $option
  175. ) {
  176. $optionId = $option->getOptionId();
  177. $existingLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
  178. $linksToAdd = [];
  179. $linksToUpdate = [];
  180. $linksToDelete = [];
  181. if (is_array($option->getProductLinks())) {
  182. $productLinks = $option->getProductLinks();
  183. foreach ($productLinks as $productLink) {
  184. if (!$productLink->getId() && !$productLink->getSelectionId()) {
  185. $linksToAdd[] = $productLink;
  186. } else {
  187. $linksToUpdate[] = $productLink;
  188. }
  189. }
  190. /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
  191. $linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
  192. }
  193. foreach ($linksToUpdate as $linkedProduct) {
  194. $this->linkManagement->saveChild($product->getSku(), $linkedProduct);
  195. }
  196. foreach ($linksToDelete as $linkedProduct) {
  197. $this->linkManagement->removeChild(
  198. $product->getSku(),
  199. $option->getOptionId(),
  200. $linkedProduct->getSku()
  201. );
  202. }
  203. foreach ($linksToAdd as $linkedProduct) {
  204. $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
  205. }
  206. return $this;
  207. }
  208. /**
  209. * Retrieve product by SKU
  210. *
  211. * @param string $sku
  212. * @return \Magento\Catalog\Api\Data\ProductInterface
  213. * @throws InputException
  214. * @throws NoSuchEntityException
  215. */
  216. private function getProduct($sku)
  217. {
  218. $product = $this->productRepository->get($sku, true, null, true);
  219. if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
  220. throw new InputException(__('This is implemented for bundle products only.'));
  221. }
  222. return $product;
  223. }
  224. /**
  225. * Computes the difference between given arrays.
  226. *
  227. * @param \Magento\Bundle\Api\Data\LinkInterface[] $firstArray
  228. * @param \Magento\Bundle\Api\Data\LinkInterface[] $secondArray
  229. *
  230. * @return array
  231. */
  232. private function compareLinks(array $firstArray, array $secondArray)
  233. {
  234. $result = [];
  235. $firstArrayIds = [];
  236. $firstArrayMap = [];
  237. $secondArrayIds = [];
  238. foreach ($firstArray as $item) {
  239. $firstArrayIds[] = $item->getId();
  240. $firstArrayMap[$item->getId()] = $item;
  241. }
  242. foreach ($secondArray as $item) {
  243. $secondArrayIds[] = $item->getId();
  244. }
  245. foreach (array_diff($firstArrayIds, $secondArrayIds) as $id) {
  246. $result[] = $firstArrayMap[$id];
  247. }
  248. return $result;
  249. }
  250. }