/** * @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; }
/** * @param string $entityType * @param object $entity * @param array $data * @return object */ public function execute($entityType, $entity, $data = []) { $hydrator = $this->metadataPool->getHydrator($entityType); $entityData = $this->createEntityRow->execute($entityType, array_merge($hydrator->extract($entity), $data)); $entity = $hydrator->hydrate($entity, $entityData); return $entity; }
/** * @param string $entityType * @param object $entity * @param array $data * @return object * @throws \Exception */ public function execute($entityType, $entity, $data = []) { $hydrator = $this->metadataPool->getHydrator($entityType); $entityData = array_merge($hydrator->extract($entity), $data); $actions = $this->extensionPool->getActions($entityType, 'create'); foreach ($actions as $action) { $entityData = $action->execute($entityType, $entityData); } $entity = $hydrator->hydrate($entity, $entityData); return $entity; }
/** * @param string $entityType * @param object $entity * @return object * @throws \Exception */ public function execute($entityType, $entity) { $hydrator = $this->metadataPool->getHydrator($entityType); $entityData = $hydrator->extract($entity); $actions = $this->extensionPool->getActions($entityType, 'read'); foreach ($actions as $action) { $data = $action->execute($entityType, $entityData); $entity = $hydrator->hydrate($entity, $data); } return $entity; }
/** * @param string $entityType * @param object $entity * @return bool|object * @throws \Exception */ public function save($entityType, $entity) { $hydrator = $this->metadataPool->getHydrator($entityType); $metadata = $this->metadataPool->getMetadata($entityType); $entityData = $hydrator->extract($entity); if (!empty($entityData[$metadata->getIdentifierField()]) && $metadata->checkIsEntityExists($entityData[$metadata->getIdentifierField()])) { $operation = $this->orchestratorPool->getWriteOperation($entityType, 'update'); } else { $operation = $this->orchestratorPool->getWriteOperation($entityType, 'create'); } return $operation->execute($entityType, $entity); }
public function testHydrator() { $metadataPool = new MetadataPool($this->entityMetadataFactoryMock, $this->entityHydratorFactoryMock, []); $entityHydrator = $this->getMockBuilder(EntityHydrator::class)->disableOriginalConstructor()->getMock(); $this->entityHydratorFactoryMock->expects($this->once())->method('create')->willReturn($entityHydrator); $this->assertEquals($entityHydrator, $metadataPool->getHydrator('testType')); }
/** * @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')); } }
/** * 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; }
/** * @param string $entityType * @param object $entity * @return bool */ public function execute($entityType, $entity) { $hydrator = $this->metadataPool->getHydrator($entityType); $entityData = $hydrator->extract($entity); return $this->deleteEntityRow->execute($entityType, $entityData); }
/** * @param string $entityType * @param object $entity * @param string $identifier * @return object */ public function execute($entityType, $entity, $identifier) { $hydrator = $this->metadataPool->getHydrator($entityType); $data = $this->readEntityRow->execute($entityType, $identifier); return $hydrator->hydrate($entity, $data); }