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

MSI: Checkout integration #295

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions app/code/Magento/Catalog/Model/LocatorService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model;

class LocatorService
{
/**
* @var \Magento\Framework\EntityManager\MetadataPool
*/
private $metadataPool;

/**
* LocatorService constructor.
*
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
*/
public function __construct(
\Magento\Framework\EntityManager\MetadataPool $metadataPool
) {
$this->metadataPool = $metadataPool;
}

/**
* @return string
*/
public function getProductLinkField() : string
{
return $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
->getLinkField();
}

/**
* @param array $compareArray
* @param int $limit
*
* @return array
*/
public function truncateToLimit(array $compareArray, int $limit) : array
{
if (count($compareArray) > $limit) {
$compareArray = array_slice($compareArray, round($limit / -2));
}

return $compareArray;
}

/**
* @param string $sku
*
* @return string
*/
public function skuProcess(string $sku) : string
{
return strtolower(trim($sku));
}
}

58 changes: 26 additions & 32 deletions app/code/Magento/Catalog/Model/ProductIdLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/

namespace Magento\Catalog\Model;
use Magento\Catalog\Model\LocatorService;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\ObjectManager;

/**
* Product ID locator provides all product IDs by SKUs.
Expand All @@ -19,14 +22,7 @@ class ProductIdLocator implements \Magento\Catalog\Model\ProductIdLocatorInterfa
private $idsLimit;

/**
* Metadata pool.
*
* @var \Magento\Framework\EntityManager\MetadataPool
*/
private $metadataPool;

/**
* @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
* @var CollectionFactory
*/
private $collectionFactory;

Expand All @@ -38,18 +34,26 @@ class ProductIdLocator implements \Magento\Catalog\Model\ProductIdLocatorInterfa
private $idsBySku = [];

/**
* @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
* @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory
* @param string $limitIdsBySkuValues
* @var LocatorService
*/
private $locatorService;

/**
* ProductIdLocator constructor.
*
* @param CollectionFactory $collectionFactory
* @param $idsLimit
* @param \Magento\Catalog\Model\LocatorService|null $locatorService
*/
public function __construct(
\Magento\Framework\EntityManager\MetadataPool $metadataPool,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $collectionFactory,
$idsLimit
CollectionFactory $collectionFactory,
$idsLimit,
LocatorService $locatorService = null
) {
$this->metadataPool = $metadataPool;
$this->collectionFactory = $collectionFactory;
$this->idsLimit = (int)$idsLimit;
$this->locatorService = $locatorService
?: ObjectManager::getInstance()->get(LocatorService::class);
}

/**
Expand All @@ -69,34 +73,24 @@ public function retrieveProductIdsBySkus(array $skus)
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->collectionFactory->create();
$collection->addFieldToFilter(\Magento\Catalog\Api\Data\ProductInterface::SKU, ['in' => $neededSkus]);
$linkField = $this->metadataPool->getMetadata(\Magento\Catalog\Api\Data\ProductInterface::class)
->getLinkField();
$linkField = $this->locatorService->getProductLinkField();

foreach ($collection as $item) {
$this->idsBySku[strtolower(trim($item->getSku()))][$item->getData($linkField)] = $item->getTypeId();
$this->idsBySku[$this->locatorService->skuProcess($item->getSku())][$item->getData($linkField)]
= $item->getTypeId();
}
}

$productIds = [];
foreach ($skus as $sku) {
$unifiedSku = strtolower(trim($sku));
$unifiedSku = $this->locatorService->skuProcess($sku);
if (isset($this->idsBySku[$unifiedSku])) {
$productIds[$sku] = $this->idsBySku[$unifiedSku];
}
}
$this->truncateToLimit();
return $productIds;
}

/**
* Cleanup IDs by SKU cache more than some limit.
*
* @return void
*/
private function truncateToLimit()
{
if (count($this->idsBySku) > $this->idsLimit) {
$this->idsBySku = array_slice($this->idsBySku, round($this->idsLimit / -2));
}
$this->idsBySku = $this->locatorService->truncateToLimit($this->idsBySku, $this->idsLimit);

return $productIds;
}
}
102 changes: 102 additions & 0 deletions app/code/Magento/Catalog/Model/ProductSkuLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product;
use Magento\Framework\App\ObjectManager;

/**
* Product SKU locator provides all product SKUs by IDs.
*/
class ProductSkuLocator implements \Magento\Catalog\Model\ProductSkuLocatorInterface
{
const CATALOG_PRODUCT_TABLE_NAME = 'catalog_product_entity';

/**
* Limit values for array SKUs by IDs.
*
* @var int
*/
private $skusLimit;

/**
* IDs by SKU cache.
*
* @var array
*/
private $skuByIds = [];

/**
* @var ResourceModel\Product
*/
private $productResource;

/**
* @var LocatorService
*/
private $locatorService;

/**
* SkuLocator constructor.
*
* @param Product $productResource
* @param $skusLimit
* @param LocatorService|null $locatorService
*/
public function __construct(
Product $productResource,
$skusLimit,
LocatorService $locatorService = null
) {
$this->productResource = $productResource;
$this->skusLimit = (int)$skusLimit;
$this->locatorService = $locatorService
?: ObjectManager::getInstance()->get(LocatorService::class);;
}

/**
* {@inheritdoc}
*/
public function retrieveSkusByProductIds(array $productIds): array
{
$resultProductIds = [];
$neededIds = [];
foreach ($productIds as $productId) {
if (isset($this->skuByIds[$productId])) {
$resultProductIds[$productId] = (string)$this->skuByIds[$productId];
} else {
$neededIds[] = $productId;
}
}

if (!empty($neededIds)) {
$items = array_column(
$this->productResource->getProductsSku($neededIds),
ProductInterface::SKU, 'entity_id'
);

$this->updateSkusCache($items);
$resultProductIds += $items;
}

return $this->locatorService->truncateToLimit($resultProductIds, $this->skusLimit);
}

/**
* @param array $additionalItems
* @return ProductSkuLocator
*/
private function updateSkusCache(array $additionalItems): ProductSkuLocator
{
$this->skuByIds += $additionalItems;

return $this;
}
}

25 changes: 25 additions & 0 deletions app/code/Magento/Catalog/Model/ProductSkuLocatorInterface.php
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\Catalog\Model;

/**
* Product ID locator provides all product SKUs by ProductIds.
* @api
* @since 101.1.0
*/
interface ProductSkuLocatorInterface
{
/**
* Will return associative array of product skus as key and type as value grouped by ProductIds.
*
* @param array $productIds
* @return array
* @since 101.1.0
*/
public function retrieveSkusByProductIds(array $productIds) : array;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/**
* Copyright :copyright: Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Test\Integration\Locator;

use Magento\Catalog\Model\ProductIdLocator;
use Magento\Catalog\Model\ProductSkuLocator;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Catalog\Api\ProductRepositoryInterface;
use PHPUnit\Framework\TestCase;

class GetProductSkuByProductIdLocatorTest extends TestCase
{
/**
* @var ProductSkuLocator
*/
private $productSkuLocator;

/**
* @var ProductIdLocator
*/
private $productIdLocator;

/**
* @var
*/
private $productRepository;

/**
* @var array
*/
private $productSkus = ['SKU-1', 'SKU-2', 'SKU-3'];

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->productSkuLocator = Bootstrap::getObjectManager()->get(ProductSkuLocator::class);
$this->productIdLocator = Bootstrap::getObjectManager()->get(ProductIdLocator::class);
$this->productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class);
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
*/
public function testGetSkuByProductId()
{
$expectedProductSkus = $this->getProductSkusToCompare();
$executeResult = $this->productSkuLocator->retrieveSkusByProductIds(array_flip($expectedProductSkus));

self::assertEquals($expectedProductSkus, $executeResult);
}

/**
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
*/
public function testGetProductIdBySku()
{
$expectedProductIds = $this->getProductIdsToCompare();
$executeResult = $this->productIdLocator->retrieveProductIdsBySkus($this->productSkus);

self::assertEquals($executeResult, $expectedProductIds);
}

/**
* @return array
*/
private function getProductSkusToCompare()
{
$productsSkus = [];
foreach ($this->productSkus as $sku) {
$product = $this->productRepository->get($sku);
$productsSkus[$product->getId()] = $sku;
}

return $productsSkus;
}

/**
* @return array
*/
private function getProductIdsToCompare()
{
$productsIds = [];
foreach ($this->productSkus as $sku) {
$product = $this->productRepository->get($sku);
$productsIds[$sku] = [$product->getId() => 'simple'];
}

return $productsIds;
}
}
Loading