Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Magento 2.2.5 - backward compatibility for versions < 2.2.6
Browse files Browse the repository at this point in the history
Fix issue with PriceTableResolver. Fix issue #53.
  • Loading branch information
Agata Firlejczyk committed Aug 30, 2019
1 parent ee4d8b7 commit d5160db
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
/**
* @package Divante\VsbridgeIndexerCatalog
* @author Agata Firlejczyk <afirlejczyk@divante.pl>
* @copyright 2019 Divante Sp. z o.o.
* @license See LICENSE_DIVANTE.txt for license details.
*/

namespace Divante\VsbridgeIndexerCatalog\Model\Product;

use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
use Magento\Framework\Indexer\DimensionFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;

/**
* Class PriceTableResolverProxy
*/
class PriceTableResolverProxy
{
const DEFAULT_PRICE_INDEXER_TABLE = 'catalog_product_index_price';

/**
* Object Manager instance
*
* @var \Magento\Framework\ObjectManagerInterface
*/
private $objectManager;

/**
* available from Magento 2.6
* @var \Magento\Framework\Indexer\DimensionFactory
*/
private $dimensionFactory;

/**
* available from Magento 2.6
* @var \Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver
*/
private $priceTableResolver;

/**
* @var array
*/
private $priceIndexTableName = [];

/**
* PriceTableResolver constructor.
*
* @param ObjectManagerInterface $objectManager
*/
public function __construct(ObjectManagerInterface $objectManager)
{
$this->objectManager = $objectManager;
}

/**
* @param int $websiteId
* @param int $customerGroupId
*
* @return mixed|string
*/
public function resolve(int $websiteId, int $customerGroupId)
{
if (class_exists('\Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver')) {
$this->createDimensionFactory();
$this->createPriceTableResolver();
$key = $websiteId . '_' . $customerGroupId;

if (!isset($this->priceIndexTableName[$key])) {
$priceIndexTableName = $this->priceTableResolver->resolve(
self::DEFAULT_PRICE_INDEXER_TABLE,
[
$this->dimensionFactory->create(
WebsiteDimensionProvider::DIMENSION_NAME,
(string)$websiteId
),
$this->dimensionFactory->create(
CustomerGroupDimensionProvider::DIMENSION_NAME,
(string)$customerGroupId
),
]
);

$this->priceIndexTableName[$key] = (string)$priceIndexTableName;
}

return $this->priceIndexTableName[$key];
}

return self::DEFAULT_PRICE_INDEXER_TABLE;
}

/**
* @return DimensionFactory
*/
private function createDimensionFactory()
{
if (null === $this->dimensionFactory) {
$this->dimensionFactory = $this->create(\Magento\Framework\Indexer\DimensionFactory::class);
}

return $this->dimensionFactory;
}

/**
* @return \Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver
*/
private function createPriceTableResolver()
{
if (null === $this->priceTableResolver) {
$this->priceTableResolver = $this->create(
\Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver::class
);
}

return $this->priceTableResolver;
}

/**
* @param string $class
*
* @return mixed
*/
private function create($class)
{
return $this->objectManager->create($class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
use Magento\Framework\App\ResourceConnection;
use Magento\Store\Model\StoreManagerInterface;
use Divante\VsbridgeIndexerCatalog\Model\ProductMetaData;
use Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver;
use Magento\Framework\Indexer\DimensionFactory;
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
use Magento\Customer\Model\Indexer\CustomerGroupDimensionProvider;
use Divante\VsbridgeIndexerCatalog\Model\Product\PriceTableResolverProxy;

/**
* Class Prices
Expand All @@ -39,41 +36,28 @@ class Prices
private $productMetaData;

/**
* @var \Magento\Catalog\Model\Indexer\Product\Price\PriceTableResolver
* @var PriceTableResolverProxy
*/
private $priceTableResolver;

/**
* @var \Magento\Framework\Indexer\DimensionFactory
*/
private $dimensionFactory;

/**
* @var array
*/
private $priceIndexTableName = [];

/**
* Prices constructor.
*
* @param ResourceConnection $resourceModel
* @param StoreManagerInterface $storeManager
* @param ProductMetaData $productMetaData
* @param PriceTableResolver $priceTableResolver
* @param DimensionFactory $dimensionFactory
* @param PriceTableResolverProxy $priceTableResolver
*/
public function __construct(
ResourceConnection $resourceModel,
StoreManagerInterface $storeManager,
ProductMetaData $productMetaData,
PriceTableResolver $priceTableResolver,
DimensionFactory $dimensionFactory
PriceTableResolverProxy $priceTableResolver
) {
$this->resource = $resourceModel;
$this->storeManager = $storeManager;
$this->productMetaData = $productMetaData;
$this->priceTableResolver = $priceTableResolver;
$this->dimensionFactory = $dimensionFactory;
}

/**
Expand Down Expand Up @@ -116,27 +100,7 @@ public function loadPriceData(int $storeId, array $productIds): array
*/
private function getPriceIndexTableName(int $websiteId, int $customerGroupId): string
{
$key = $websiteId . '_' . $customerGroupId;

if (!isset($this->priceIndexTableName[$key])) {
$priceIndexTableName = $this->priceTableResolver->resolve(
'catalog_product_index_price',
[
$this->dimensionFactory->create(
WebsiteDimensionProvider::DIMENSION_NAME,
(string)$websiteId
),
$this->dimensionFactory->create(
CustomerGroupDimensionProvider::DIMENSION_NAME,
(string)$customerGroupId
),
]
);

$this->priceIndexTableName[$key] = (string)$priceIndexTableName;
}

return $this->priceIndexTableName[$key];
return $this->priceTableResolver->resolve($websiteId, $customerGroupId);
}

/**
Expand Down

0 comments on commit d5160db

Please sign in to comment.