Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product variants #413

Merged
merged 26 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -0,0 +1,90 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogMessageBroker\Model;

use Magento\CatalogMessageBroker\HttpClient\RestClient;
use Magento\CatalogExport\Event\Data\Entity;
use Psr\Log\LoggerInterface;

/**
* @inheritdoc
*/
class FetchProductVariants implements FetchProductVariantsInterface
{
/**
* Route to Export API product variants retrieval
*/
private const EXPORT_API_GET_VARIANTS = '/V1/catalog-export/product-variants';

/**
* @var RestClient
*/
private $restClient;

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

/**
* @param RestClient $restClient
* @param LoggerInterface $logger
*/
public function __construct(
RestClient $restClient,
LoggerInterface $logger
) {
$this->restClient = $restClient;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function execute(array $entities): array
{
$data = $this->prepareRequestData($entities);
try {
$variants = $this->restClient->get(
self::EXPORT_API_GET_VARIANTS,
$data
);

} catch (\Throwable $e) {
$this->logger->error(
\sprintf(
'Cannot load product variants via "%s" with ids "%s"',
self::EXPORT_API_GET_VARIANTS,
\implode(',', \array_map(function (Entity $entity) {
return $entity->getEntityId();
}, $entities))
),
['exception' => $e]
);
return [];
}

return $variants;
}

/**
* Prepare client request data
*
* @param Entity[] $entities
*
* @return array
*/
private function prepareRequestData(array $entities): array
{
$variants = [];
foreach ($entities as $entity) {
$variants['ids'][] = $entity->getEntityId();
}
return $variants;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogMessageBroker\Model;

use Magento\CatalogExport\Event\Data\Entity;

/**
* Fetch product variants data
*/
interface FetchProductVariantsInterface
{
/**
* Fetch product variants data
*
* @param Entity[] $entities
*
* @return array
*/
public function execute(array $entities): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(array $entities, string $scope): void
public function execute(array $entities, ?string $scope = null): void
{
$ids = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(array $entities, string $scope): void
public function execute(array $entities, ?string $scope = null): void
{
$categoriesData = $this->fetchCategories->execute($entities, $scope);
$attributesArray = $this->getAttributesArray($entities);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ interface ConsumerEventInterface
* Execute consumers by ids for specified scope
*
* @param Entity[] $entities
* @param string $scope
* @param string|null $scope
*
* @return void
*/
public function execute(array $entities, string $scope): void;
public function execute(array $entities, ?string $scope = null): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(array $entities, string $scope): void
public function execute(array $entities, ?string $scope = null): void
{
$ids = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function execute(array $entities, string $scope): void
public function execute(array $entities, ?string $scope = null): void
{
$productsData = $this->fetchProducts->execute($entities, $scope);
$attributesArray = $this->getAttributesArray($entities);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogMessageBroker\Model\MessageBus\ProductVariants;

use Magento\CatalogStorefrontApi\Api\VariantServiceServerInterface;
use Magento\CatalogStorefrontApi\Api\Data\DeleteVariantsRequestInterfaceFactory;
use Magento\CatalogMessageBroker\Model\MessageBus\ConsumerEventInterface;
use Psr\Log\LoggerInterface;

/**
* Delete product variants from storage
*/
class DeleteProductVariantsConsumer implements ConsumerEventInterface
{
/**
* @var DeleteVariantsRequestInterfaceFactory
*/
private $deleteVariantsRequestInterfaceFactory;

/**
* @var VariantServiceServerInterface
*/
private $variantsServer;

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

/**
* @param DeleteVariantsRequestInterfaceFactory $deleteVariantsRequestInterfaceFactory
* @param VariantServiceServerInterface $variantsServer
* @param LoggerInterface $logger
*/
public function __construct(
DeleteVariantsRequestInterfaceFactory $deleteVariantsRequestInterfaceFactory,
VariantServiceServerInterface $variantsServer,
LoggerInterface $logger
) {
$this->deleteVariantsRequestInterfaceFactory = $deleteVariantsRequestInterfaceFactory;
$this->variantsServer = $variantsServer;
$this->logger = $logger;
}

/**
* @inheritdoc
*/
public function execute(array $entities, ?string $scope = null): void
{
$ids = [];
foreach ($entities as $entity) {
$ids[] = $entity->getEntityId();
}

$deleteVariantsRequest = $this->deleteVariantsRequestInterfaceFactory->create();
$deleteVariantsRequest->setId($ids);

try {
$importResult = $this->variantsServer->deleteProductVariants($deleteVariantsRequest);
if ($importResult->getStatus() === false) {
$this->logger->error(
sprintf('Product variants deletion has failed: "%s"', $importResult->getMessage())
);
}
} catch (\Throwable $e) {
$this->logger->critical(sprintf('Exception while deleting product variants: "%s"', $e));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogMessageBroker\Model\MessageBus\ProductVariants;

use Magento\CatalogExport\Event\Data\ChangedEntities;
use Magento\CatalogExport\Event\Data\Entity;
use Magento\CatalogMessageBroker\Model\MessageBus\ConsumerEventInterfaceFactory;
use Psr\Log\LoggerInterface;

/**
* Process product variants messages
*/
class ProductVariantsConsumer
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @var ConsumerEventInterfaceFactory
*/
private $consumerEventFactory;

/**
* @param LoggerInterface $logger
* @param ConsumerEventInterfaceFactory $consumerEventFactory
*/
public function __construct(
LoggerInterface $logger,
ConsumerEventInterfaceFactory $consumerEventFactory
) {
$this->logger = $logger;
$this->consumerEventFactory = $consumerEventFactory;
}

/**
* Process message
*
* @param ChangedEntities $message
* @return void
*/
public function processMessage(ChangedEntities $message): void
{
try {
$eventType = $message->getMeta() ? $message->getMeta()->getEventType() : null;
$entities = $message->getData() ? $message->getData()->getEntities() : null;

if (empty($entities)) {
throw new \InvalidArgumentException('Product variants data is missing in payload');
}

$variantsEvent = $this->consumerEventFactory->create($eventType);
$variantsEvent->execute($entities);
} catch (\Throwable $e) {
$this->logger->error(
\sprintf(
'Unable to process collected product variants data. Event type: "%s", ids: "%s"',
$eventType ?? '',
\implode(',', \array_map(function (Entity $entity) {
return $entity->getEntityId();
}, $entities ?? []))
),
['exception' => $e]
);
}
}
}
Loading