SaveAction.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Bundle\Model\Option;
  8. use Magento\Bundle\Api\Data\OptionInterface;
  9. use Magento\Bundle\Model\ResourceModel\Option;
  10. use Magento\Catalog\Api\Data\ProductInterface;
  11. use Magento\Framework\EntityManager\MetadataPool;
  12. use Magento\Framework\Exception\CouldNotSaveException;
  13. use Magento\Bundle\Model\Product\Type;
  14. use Magento\Bundle\Api\ProductLinkManagementInterface;
  15. /**
  16. * Encapsulates logic for saving a bundle option, including coalescing the parent product's data.
  17. */
  18. class SaveAction
  19. {
  20. /**
  21. * @var Option
  22. */
  23. private $optionResource;
  24. /**
  25. * @var MetadataPool
  26. */
  27. private $metadataPool;
  28. /**
  29. * @var Type
  30. */
  31. private $type;
  32. /**
  33. * @var ProductLinkManagementInterface
  34. */
  35. private $linkManagement;
  36. /**
  37. * @param Option $optionResource
  38. * @param MetadataPool $metadataPool
  39. * @param Type $type
  40. * @param ProductLinkManagementInterface $linkManagement
  41. */
  42. public function __construct(
  43. Option $optionResource,
  44. MetadataPool $metadataPool,
  45. Type $type,
  46. ProductLinkManagementInterface $linkManagement
  47. ) {
  48. $this->optionResource = $optionResource;
  49. $this->metadataPool = $metadataPool;
  50. $this->type = $type;
  51. $this->linkManagement = $linkManagement;
  52. }
  53. /**
  54. * Manage the logic of saving a bundle option, including the coalescence of its parent product data.
  55. *
  56. * @param ProductInterface $bundleProduct
  57. * @param OptionInterface $option
  58. * @return OptionInterface
  59. * @throws CouldNotSaveException
  60. * @throws \Exception
  61. */
  62. public function save(ProductInterface $bundleProduct, OptionInterface $option)
  63. {
  64. $metadata = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class);
  65. $option->setStoreId($bundleProduct->getStoreId());
  66. $parentId = $bundleProduct->getData($metadata->getLinkField());
  67. $option->setParentId($parentId);
  68. $optionId = $option->getOptionId();
  69. $linksToAdd = [];
  70. $optionCollection = $this->type->getOptionsCollection($bundleProduct);
  71. $optionCollection->setIdFilter($option->getOptionId());
  72. $optionCollection->setProductLinkFilter($parentId);
  73. /** @var \Magento\Bundle\Model\Option $existingOption */
  74. $existingOption = $optionCollection->getFirstItem();
  75. if (!$optionId || $existingOption->getParentId() != $parentId) {
  76. //If option ID is empty or existing option's parent ID is different
  77. //we'd need a new ID for the option.
  78. $option->setOptionId(null);
  79. $option->setDefaultTitle($option->getTitle());
  80. if (is_array($option->getProductLinks())) {
  81. $linksToAdd = $option->getProductLinks();
  82. }
  83. } else {
  84. if (!$existingOption->getOptionId()) {
  85. throw new NoSuchEntityException(
  86. __("The option that was requested doesn't exist. Verify the entity and try again.")
  87. );
  88. }
  89. $option->setData(array_merge($existingOption->getData(), $option->getData()));
  90. $this->updateOptionSelection($bundleProduct, $option);
  91. }
  92. try {
  93. $this->optionResource->save($option);
  94. } catch (\Exception $e) {
  95. throw new CouldNotSaveException(__("The option couldn't be saved."), $e);
  96. }
  97. /** @var \Magento\Bundle\Api\Data\LinkInterface $linkedProduct */
  98. foreach ($linksToAdd as $linkedProduct) {
  99. $this->linkManagement->addChild($bundleProduct, $option->getOptionId(), $linkedProduct);
  100. }
  101. $bundleProduct->setIsRelationsChanged(true);
  102. return $option;
  103. }
  104. /**
  105. * Update option selections
  106. *
  107. * @param \Magento\Catalog\Api\Data\ProductInterface $product
  108. * @param \Magento\Bundle\Api\Data\OptionInterface $option
  109. * @return void
  110. */
  111. private function updateOptionSelection(ProductInterface $product, OptionInterface $option)
  112. {
  113. $optionId = $option->getOptionId();
  114. $existingLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
  115. $linksToAdd = [];
  116. $linksToUpdate = [];
  117. $linksToDelete = [];
  118. if (is_array($option->getProductLinks())) {
  119. $productLinks = $option->getProductLinks();
  120. foreach ($productLinks as $productLink) {
  121. if (!$productLink->getId() && !$productLink->getSelectionId()) {
  122. $linksToAdd[] = $productLink;
  123. } else {
  124. $linksToUpdate[] = $productLink;
  125. }
  126. }
  127. /** @var \Magento\Bundle\Api\Data\LinkInterface[] $linksToDelete */
  128. $linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
  129. }
  130. foreach ($linksToUpdate as $linkedProduct) {
  131. $this->linkManagement->saveChild($product->getSku(), $linkedProduct);
  132. }
  133. foreach ($linksToDelete as $linkedProduct) {
  134. $this->linkManagement->removeChild(
  135. $product->getSku(),
  136. $option->getOptionId(),
  137. $linkedProduct->getSku()
  138. );
  139. }
  140. foreach ($linksToAdd as $linkedProduct) {
  141. $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
  142. }
  143. }
  144. /**
  145. * Compute the difference between given arrays.
  146. *
  147. * @param \Magento\Bundle\Api\Data\LinkInterface[] $firstArray
  148. * @param \Magento\Bundle\Api\Data\LinkInterface[] $secondArray
  149. *
  150. * @return array
  151. */
  152. private function compareLinks(array $firstArray, array $secondArray)
  153. {
  154. $result = [];
  155. $firstArrayIds = [];
  156. $firstArrayMap = [];
  157. $secondArrayIds = [];
  158. foreach ($firstArray as $item) {
  159. $firstArrayIds[] = $item->getId();
  160. $firstArrayMap[$item->getId()] = $item;
  161. }
  162. foreach ($secondArray as $item) {
  163. $secondArrayIds[] = $item->getId();
  164. }
  165. foreach (array_diff($firstArrayIds, $secondArrayIds) as $id) {
  166. $result[] = $firstArrayMap[$id];
  167. }
  168. return $result;
  169. }
  170. }