Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
OptionRepository.php
Go to the documentation of this file.
1 <?php
8 namespace Magento\Bundle\Model;
9 
14 
21 {
25  protected $productRepository;
26 
30  protected $type;
31 
35  protected $optionFactory;
36 
40  protected $optionResource;
41 
45  protected $linkManagement;
46 
50  protected $productOptionList;
51 
55  protected $dataObjectHelper;
56 
60  private $optionSave;
61 
72  public function __construct(
74  \Magento\Bundle\Model\Product\Type $type,
75  \Magento\Bundle\Api\Data\OptionInterfaceFactory $optionFactory,
77  \Magento\Bundle\Api\ProductLinkManagementInterface $linkManagement,
78  \Magento\Bundle\Model\Product\OptionList $productOptionList,
79  \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
80  SaveAction $optionSave
81  ) {
82  $this->productRepository = $productRepository;
83  $this->type = $type;
84  $this->optionFactory = $optionFactory;
85  $this->optionResource = $optionResource;
86  $this->linkManagement = $linkManagement;
87  $this->productOptionList = $productOptionList;
88  $this->dataObjectHelper = $dataObjectHelper;
89  $this->optionSave = $optionSave;
90  }
91 
95  public function get($sku, $optionId)
96  {
97  $product = $this->getProduct($sku);
98 
100  $option = $this->type->getOptionsCollection($product)->getItemById($optionId);
101  if (!$option || !$option->getId()) {
102  throw new NoSuchEntityException(
103  __("The option that was requested doesn't exist. Verify the entity and try again.")
104  );
105  }
106 
107  $productLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
108 
110  $optionDataObject = $this->optionFactory->create();
111  $this->dataObjectHelper->populateWithArray(
112  $optionDataObject,
113  $option->getData(),
114  \Magento\Bundle\Api\Data\OptionInterface::class
115  );
116  $optionDataObject->setOptionId($option->getId())
117  ->setTitle($option->getTitle() === null ? $option->getDefaultTitle() : $option->getTitle())
118  ->setSku($product->getSku())
119  ->setProductLinks($productLinks);
120 
121  return $optionDataObject;
122  }
123 
127  public function getList($sku)
128  {
129  $product = $this->getProduct($sku);
130  return $this->getListByProduct($product);
131  }
132 
138  {
139  return $this->productOptionList->getItems($product);
140  }
141 
146  {
147  try {
148  $this->optionResource->delete($option);
149  } catch (\Exception $exception) {
150  throw new \Magento\Framework\Exception\StateException(
151  __('The option with "%1" ID can\'t be deleted.', $option->getOptionId()),
152  $exception
153  );
154  }
155  return true;
156  }
157 
161  public function deleteById($sku, $optionId)
162  {
163  $product = $this->getProduct($sku);
164  $optionCollection = $this->type->getOptionsCollection($product);
165  $optionCollection->setIdFilter($optionId);
166  $hasBeenDeleted = $this->delete($optionCollection->getFirstItem());
167 
168  return $hasBeenDeleted;
169  }
170 
174  public function save(
175  \Magento\Catalog\Api\Data\ProductInterface $product,
176  \Magento\Bundle\Api\Data\OptionInterface $option
177  ) {
178  $savedOption = $this->optionSave->save($product, $option);
179 
180  $productToSave = $this->productRepository->get($product->getSku());
181  $this->productRepository->save($productToSave);
182 
183  return $savedOption->getOptionId();
184  }
185 
193  protected function updateOptionSelection(
194  \Magento\Catalog\Api\Data\ProductInterface $product,
195  \Magento\Bundle\Api\Data\OptionInterface $option
196  ) {
197  $optionId = $option->getOptionId();
198  $existingLinks = $this->linkManagement->getChildren($product->getSku(), $optionId);
199  $linksToAdd = [];
200  $linksToUpdate = [];
201  $linksToDelete = [];
202  if (is_array($option->getProductLinks())) {
203  $productLinks = $option->getProductLinks();
204  foreach ($productLinks as $productLink) {
205  if (!$productLink->getId() && !$productLink->getSelectionId()) {
206  $linksToAdd[] = $productLink;
207  } else {
208  $linksToUpdate[] = $productLink;
209  }
210  }
212  $linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
213  }
214  foreach ($linksToUpdate as $linkedProduct) {
215  $this->linkManagement->saveChild($product->getSku(), $linkedProduct);
216  }
217  foreach ($linksToDelete as $linkedProduct) {
218  $this->linkManagement->removeChild(
219  $product->getSku(),
220  $option->getOptionId(),
221  $linkedProduct->getSku()
222  );
223  }
224  foreach ($linksToAdd as $linkedProduct) {
225  $this->linkManagement->addChild($product, $option->getOptionId(), $linkedProduct);
226  }
227  return $this;
228  }
229 
235  private function getProduct($sku)
236  {
237  $product = $this->productRepository->get($sku, true, null, true);
238  if ($product->getTypeId() != \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE) {
239  throw new InputException(__('This is implemented for bundle products only.'));
240  }
241  return $product;
242  }
243 
252  private function compareLinks(array $firstArray, array $secondArray)
253  {
254  $result = [];
255 
256  $firstArrayIds = [];
257  $firstArrayMap = [];
258 
259  $secondArrayIds = [];
260 
261  foreach ($firstArray as $item) {
262  $firstArrayIds[] = $item->getId();
263 
264  $firstArrayMap[$item->getId()] = $item;
265  }
266 
267  foreach ($secondArray as $item) {
268  $secondArrayIds[] = $item->getId();
269  }
270 
271  foreach (array_diff($firstArrayIds, $secondArrayIds) as $id) {
272  $result[] = $firstArrayMap[$id];
273  }
274 
275  return $result;
276  }
277 }
__construct(\Magento\Catalog\Api\ProductRepositoryInterface $productRepository, \Magento\Bundle\Model\Product\Type $type, \Magento\Bundle\Api\Data\OptionInterfaceFactory $optionFactory, \Magento\Bundle\Model\ResourceModel\Option $optionResource, \Magento\Bundle\Api\ProductLinkManagementInterface $linkManagement, \Magento\Bundle\Model\Product\OptionList $productOptionList, \Magento\Framework\Api\DataObjectHelper $dataObjectHelper, SaveAction $optionSave)
getListByProduct(ProductInterface $product)
$id
Definition: fieldset.phtml:14
__()
Definition: __.php:13
$linkedProduct
save(\Magento\Catalog\Api\Data\ProductInterface $product, \Magento\Bundle\Api\Data\OptionInterface $option)