Ejemplo n.º 1
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotSaveException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     /**
      * @var $entity \Magento\Catalog\Api\Data\ProductLinkInterface
      */
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $links = [];
     $extensions = $this->dataObjectProcessor->buildOutputDataArray($entity->getExtensionAttributes(), 'Magento\\Catalog\\Api\\Data\\ProductLinkExtensionInterface');
     $extensions = is_array($extensions) ? $extensions : [];
     $data = $entity->__toArray();
     foreach ($extensions as $attributeCode => $attribute) {
         $data[$attributeCode] = $attribute;
     }
     unset($data['extension_attributes']);
     $data['product_id'] = $linkedProduct->getId();
     $links[$linkedProduct->getId()] = $data;
     try {
         $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
         $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
         $productData = $prodyctHydrator->extract($product);
         $this->linkResource->saveProductLinks($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $links, $linkTypesToId[$entity->getLinkType()]);
     } catch (\Exception $exception) {
         throw new CouldNotSaveException(__('Invalid data provided for linked products'));
     }
     return $entity;
 }
Ejemplo n.º 2
0
 /**
  * @param string $entityType
  * @param object $entity
  * @return object
  * @throws CouldNotDeleteException
  * @throws NoSuchEntityException
  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  */
 public function execute($entityType, $entity)
 {
     $linkedProduct = $this->productRepository->get($entity->getLinkedProductSku());
     $product = $this->productRepository->get($entity->getSku());
     $linkTypesToId = $this->linkTypeProvider->getLinkTypes();
     $prodyctHydrator = $this->metadataPool->getHydrator(ProductInterface::class);
     $productData = $prodyctHydrator->extract($product);
     $linkId = $this->linkResource->getProductLinkId($productData[$this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()], $linkedProduct->getId(), $linkTypesToId[$entity->getLinkType()]);
     if (!$linkId) {
         throw new NoSuchEntityException(__('Product with SKU %1 is not linked to product with SKU %2', $entity->getLinkedProductSku(), $entity->getSku()));
     }
     try {
         $this->linkResource->deleteProductLink($linkId);
     } catch (\Exception $exception) {
         throw new CouldNotDeleteException(__('Invalid data provided for linked products'));
     }
 }
Ejemplo n.º 3
0
 /**
  * Check is current entity has changes, by comparing current object state with stored snapshot
  *
  * @param string $entityType
  * @param object $entity
  * @return bool
  */
 public function isModified($entityType, $entity)
 {
     $metadata = $this->metadataPool->getMetadata($entityType);
     $hydrator = $this->metadataPool->getHydrator($entityType);
     $entityData = $hydrator->extract($entity);
     if (empty($entityData[$metadata->getIdentifierField()])) {
         return true;
     }
     $identifier = $entityData[$metadata->getIdentifierField()];
     if (!isset($this->snapshotData[$entityType][$identifier])) {
         return true;
     }
     foreach ($this->snapshotData[$entityType][$identifier] as $field => $value) {
         if (isset($entityData[$field]) && $entityData[$field] != $value) {
             return true;
         }
     }
     return false;
 }