6 declare(strict_types=1);
26 private $optionResource;
31 private $metadataPool;
41 private $linkManagement;
55 $this->optionResource = $optionResource;
56 $this->metadataPool = $metadataPool;
72 $metadata = $this->metadataPool->getMetadata(\
Magento\Catalog\Api\Data\ProductInterface::class);
76 $option->setParentId($parentId);
86 if (!
$optionId || $existingOption->getParentId() != $parentId) {
91 if (is_array(
$option->getProductLinks())) {
92 $linksToAdd =
$option->getProductLinks();
95 if (!$existingOption->getOptionId()) {
96 throw new NoSuchEntityException(
97 __(
"The option that was requested doesn't exist. Verify the entity and try again.")
101 $option->setData(array_merge($existingOption->getData(),
$option->getData()));
106 $this->optionResource->save(
$option);
107 }
catch (\Exception $e) {
108 throw new CouldNotSaveException(
__(
"The option couldn't be saved."), $e);
128 private function updateOptionSelection(ProductInterface
$product, OptionInterface
$option)
131 $existingLinks = $this->linkManagement->getChildren(
$product->getSku(),
$optionId);
135 if (is_array(
$option->getProductLinks())) {
136 $productLinks =
$option->getProductLinks();
145 $linksToDelete = $this->compareLinks($existingLinks, $linksToUpdate);
151 $this->linkManagement->removeChild(
170 private function compareLinks(array $firstArray, array $secondArray)
177 $secondArrayIds = [];
179 foreach ($firstArray as
$item) {
180 $firstArrayIds[] =
$item->getId();
185 foreach ($secondArray as
$item) {
186 $secondArrayIds[] =
$item->getId();
189 foreach (array_diff($firstArrayIds, $secondArrayIds) as
$id) {
__construct(Option $optionResource, MetadataPool $metadataPool, Type $type, ProductLinkManagementInterface $linkManagement)