Skip to content

Commit

Permalink
Merge pull request #9 from magento-commerce/imported-magento-commerce…
Browse files Browse the repository at this point in the history
…-data-export-8

[Imported] 27 product variants
  • Loading branch information
RuslanKostiv1 committed Nov 13, 2020
2 parents 60445ea + c8d3f0d commit 738df37
Show file tree
Hide file tree
Showing 60 changed files with 2,193 additions and 92 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/CatalogDataExporter/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"magento/module-directory": "*",
"magento/module-data-exporter": "*",
"magento/module-customer": "*",
"magento/module-downloadable": "*"
"magento/module-downloadable": "*",
"magento/module-indexer": "*"
}
}
12 changes: 0 additions & 12 deletions app/code/Magento/CatalogDataExporter/etc/et_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
provider="Magento\CatalogDataExporter\Model\Provider\ProductMetadata">
<using field="id" />
</field>
<field name="variants" type="ProductVariant" repeated="true"
provider="Magento\CatalogDataExporter\Model\Provider\ProductVariants">
<using field="product_id" />
</field>
</record>
<!-- TODO: delete deprecated "ImageDeprecated" type. use "Image" instead -->
<record name="ImageDeprecated">
Expand Down Expand Up @@ -251,14 +247,6 @@
<field name="qty" type="Float"/>
<field name="price" type="Float"/>
</record>
<record name="ProductVariant">
<!-- variant identifier following the convention :prefix:/:parentId:/:entityId: -->
<field name="id" type="ID" />
<!-- parent_id:option_id/optionValue.uid -->
<field name="option_values" type="String" repeated="true" />
<!-- link to product id that represents variant, for custom option must be null-->
<field name="product_id" type="String"/>
</record>
<record name="Price">
<field name="regularPrice" type="Float"/>
<field name="finalPrice" type="Float"/>
Expand Down
10 changes: 5 additions & 5 deletions app/code/Magento/CatalogExport/Event/Data/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class Entity
{
/**
* @var int
* @var string
*/
private $entityId;

Expand All @@ -26,21 +26,21 @@ class Entity
/**
* Get entity id.
*
* @return int
* @return string
*/
public function getEntityId(): int
public function getEntityId(): string
{
return $this->entityId;
}

/**
* Set entity id.
*
* @param int $entityId
* @param string $entityId
*
* @return void
*/
public function setEntityId(int $entityId): void
public function setEntityId(string $entityId): void
{
$this->entityId = $entityId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct(
*
* @return \Magento\CatalogExport\Event\Data\ChangedEntities
*/
public function build(string $eventType, array $entities, string $scope): ChangedEntities
public function build(string $eventType, array $entities, ?string $scope = null): ChangedEntities
{
$meta = $this->metaFactory->create();
$meta->setScope($scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,23 @@ public function __construct(
public function execute(array $entityData, array $deleteIds) : void
{
foreach ($this->getDeleteEntitiesData($deleteIds) as $storeCode => $entities) {
$scope = $storeCode ?: null;
foreach (\array_chunk($entities, $this->batchSize) as $chunk) {
$this->publishMessage(
$this->deletedEventType,
$chunk,
$storeCode
$scope
);
}
}

foreach ($this->getUpdateEntitiesData($entityData) as $storeCode => $entities) {
$scope = $storeCode ?: null;
foreach (\array_chunk($entities, $this->batchSize) as $chunk) {
$this->publishMessage(
$this->updatedEventType,
$chunk,
$storeCode
$scope
);
}
}
Expand All @@ -135,8 +137,8 @@ private function getDeleteEntitiesData(array $deleteIds): array
$deleted = [];
$feed = $this->feedPool->getFeed($this->feedIndexMetadata->getFeedName());
foreach ($feed->getDeletedByIds($deleteIds) as $entity) {
$deleted[$entity['storeViewCode']][] = [
'entity_id' => (int)$entity[$this->feedIndexMetadata->getFeedIdentity()],
$deleted[$entity['storeViewCode'] ?? null][] = [
'entity_id' => (string)$entity[$this->feedIndexMetadata->getFeedIdentity()],
];
}

Expand All @@ -154,8 +156,8 @@ private function getUpdateEntitiesData(array $entityData): array
{
$entitiesArray = [];
foreach ($entityData as $entity) {
$entitiesArray[$entity['storeViewCode']][] = [
'entity_id' => (int)$entity[$this->feedIndexMetadata->getFeedIdentity()],
$entitiesArray[$entity['storeViewCode'] ?? null][] = [
'entity_id' => (string)$entity[$this->feedIndexMetadata->getFeedIdentity()],
'attributes' => $entity['attributes'] ?? [],
];
}
Expand All @@ -168,11 +170,11 @@ private function getUpdateEntitiesData(array $entityData): array
*
* @param string $eventType
* @param array $entities
* @param string $scope
* @param string|null $scope
*
* @return void
*/
private function publishMessage(string $eventType, array $entities, string $scope): void
private function publishMessage(string $eventType, array $entities, ?string $scope): void
{
$message = $this->messageBuilder->build($eventType, $entities, $scope);

Expand Down
182 changes: 182 additions & 0 deletions app/code/Magento/CatalogExport/Model/ProductVariantRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogExport\Model;

use Magento\CatalogExportApi\Api\Data\ProductVariant;
use Magento\CatalogExportApi\Api\Data\ProductVariantFactory;
use Magento\CatalogExportApi\Api\ProductVariantRepositoryInterface;
use Magento\DataExporter\Model\FeedPool;
use Magento\Framework\App\DeploymentConfig;
use Psr\Log\LoggerInterface;

/**
* Product variant entity repository
*/
class ProductVariantRepository implements ProductVariantRepositoryInterface
{
/**
* Constant value for setting max items in response
*/
private const MAX_ITEMS_IN_RESPONSE = 250;

/**
* @var ProductVariantFactory
*/
private $productVariantFactory;

/**
* @var DtoMapper
*/
private $dtoMapper;

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @var FeedPool
*/
private $feedPool;

/**
* @param FeedPool $feedPool
* @param ProductVariantFactory $productVariantFactory
* @param DtoMapper $dtoMapper
* @param DeploymentConfig $deploymentConfig
* @param LoggerInterface $logger
*/
public function __construct(
FeedPool $feedPool,
ProductVariantFactory $productVariantFactory,
DtoMapper $dtoMapper,
DeploymentConfig $deploymentConfig,
LoggerInterface $logger
) {
$this->feedPool = $feedPool;
$this->dtoMapper = $dtoMapper;
$this->productVariantFactory = $productVariantFactory;
$this->deploymentConfig = $deploymentConfig;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function get(array $ids): array
{
if (count($ids) > $this->getMaxItemsInResponse()) {
throw new \InvalidArgumentException(
'Max items in the response can\'t exceed '
. $this->getMaxItemsInResponse()
. '.'
);
}

$productsVariants = [];
$feedData = $this->feedPool->getFeed('variants')->getFeedByIds($ids);
if (empty($feedData['feed'])) {
$this->logger->error(
\sprintf('Cannot find products variants data in catalog feed with ids "%s"', \implode(',', $ids))
);
return $productsVariants;
}

foreach ($feedData['feed'] as $feedItem) {
$productVariant = $this->productVariantFactory->create();
$feedItem = $this->cleanUpNullValues($feedItem);
$this->dtoMapper->populateWithArray(
$productVariant,
$feedItem,
ProductVariant::class
);
$productsVariants[] = $productVariant;
}
return $productsVariants;
}

/**
* @inheritdoc
*/
public function getByProductIds(array $productIds): array
{
if (count($productIds) > $this->getMaxItemsInResponse()) {
throw new \InvalidArgumentException(
'Max items in the response can\'t exceed '
. $this->getMaxItemsInResponse()
. '.'
);
}

$productsVariants = [];
$feedData = $this->feedPool->getFeed('variants')->getFeedByProductIds($productIds);
if (empty($feedData['feed'])) {
$this->logger->error(
\sprintf(
'Cannot find products variants data in catalog feed with product ids "%s"',
\implode(',', $productIds)
)
);
return $productsVariants;
}

foreach ($feedData['feed'] as $feedItem) {
$productVariant = $this->productVariantFactory->create();
$feedItem = $this->cleanUpNullValues($feedItem);
$this->dtoMapper->populateWithArray(
$productVariant,
$feedItem,
ProductVariant::class
);
$productsVariants[] = $productVariant;
}
return $productsVariants;
}

/**
* Get max items in response
*
* @return int
*/
private function getMaxItemsInResponse(): int
{
try {
$maxItemsInResponse = (int)$this->deploymentConfig->get('catalog_export/max_items_in_response');
} catch (\Exception $e) {
$this->logger->error(
\sprintf('Cannot retrieve catalog export max items in response for product variants. ' . $e)
);
return self::MAX_ITEMS_IN_RESPONSE;
}
return $maxItemsInResponse ?: self::MAX_ITEMS_IN_RESPONSE;
}

/**
* Unset null values in provided array recursively
*
* @param array $array
* @return array
*/
private function cleanUpNullValues(array $array): array
{
$result = [];
foreach ($array as $key => $value) {
if ($value === null || $value === '') {
continue;
}

$result[$key] = is_array($value) ? $this->cleanUpNullValues($value) : $value;
}
return $result;
}
}

0 comments on commit 738df37

Please sign in to comment.