Skip to content

Commit

Permalink
Improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
kandy committed May 11, 2020
1 parent 00f1377 commit 6587c57
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@
namespace Magento\Catalog\Model\Indexer\Product\Flat;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Helper\Product\Flat\Indexer;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\EntityManager\MetadataPool;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\App\ObjectManager;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Framework\DB\Select;

/**
* Class for building flat index
Expand All @@ -27,22 +38,22 @@ class FlatTableBuilder
const XML_NODE_MAX_INDEX_COUNT = 'catalog/product/flat/max_index_count';

/**
* @var \Magento\Catalog\Helper\Product\Flat\Indexer
* @var Indexer
*/
protected $_productIndexerHelper;

/**
* @var \Magento\Framework\DB\Adapter\AdapterInterface
* @var AdapterInterface
*/
protected $_connection;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface $config
* @var ScopeConfigInterface $config
*/
protected $_config;

/**
* @var \Magento\Store\Model\StoreManagerInterface
* @var StoreManagerInterface
*/
protected $_storeManager;

Expand All @@ -52,23 +63,23 @@ class FlatTableBuilder
protected $_tableData;

/**
* @var \Magento\Framework\App\ResourceConnection
* @var ResourceConnection
*/
protected $resource;

/**
* @param \Magento\Catalog\Helper\Product\Flat\Indexer $productIndexerHelper
* @param Indexer $productIndexerHelper
* @param ResourceConnection $resource
* @param \Magento\Framework\App\Config\ScopeConfigInterface $config
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param ScopeConfigInterface $config
* @param StoreManagerInterface $storeManager
* @param TableDataInterface $tableData
*/
public function __construct(
\Magento\Catalog\Helper\Product\Flat\Indexer $productIndexerHelper,
\Magento\Framework\App\ResourceConnection $resource,
\Magento\Framework\App\Config\ScopeConfigInterface $config,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\Indexer\Product\Flat\TableDataInterface $tableData
Indexer $productIndexerHelper,
ResourceConnection $resource,
ScopeConfigInterface $config,
StoreManagerInterface $storeManager,
TableDataInterface $tableData
) {
$this->_productIndexerHelper = $productIndexerHelper;
$this->resource = $resource;
Expand Down Expand Up @@ -114,7 +125,7 @@ public function build($storeId, $changedIds, $valueFieldSuffix, $tableDropSuffix
*
* @param int|string $storeId
* @return void
* @throws \Magento\Framework\Exception\LocalizedException
* @throws LocalizedException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
Expand All @@ -128,7 +139,7 @@ protected function _createTemporaryFlatTable($storeId)
self::XML_NODE_MAX_INDEX_COUNT
);
if ($maxIndex && count($indexesNeed) > $maxIndex) {
throw new \Magento\Framework\Exception\LocalizedException(
throw new LocalizedException(
__(
'The Flat Catalog module has a limit of %2$d filterable and/or sortable attributes.'
. 'Currently there are %1$d of them.'
Expand All @@ -141,7 +152,7 @@ protected function _createTemporaryFlatTable($storeId)

$indexKeys = [];
$indexProps = array_values($indexesNeed);
$upperPrimaryKey = strtoupper(\Magento\Framework\DB\Adapter\AdapterInterface::INDEX_TYPE_PRIMARY);
$upperPrimaryKey = strtoupper(AdapterInterface::INDEX_TYPE_PRIMARY);
foreach ($indexProps as $i => $indexProp) {
$indexName = $this->_connection->getIndexName(
$this->_getTemporaryTableName($this->_productIndexerHelper->getFlatTableName($storeId)),
Expand All @@ -164,7 +175,7 @@ protected function _createTemporaryFlatTable($storeId)
}
$indexesNeed = array_combine($indexKeys, $indexProps);

/** @var $table \Magento\Framework\DB\Ddl\Table */
/** @var $table Table */
$table = $this->_connection->newTable(
$this->_getTemporaryTableName($this->_productIndexerHelper->getFlatTableName($storeId))
);
Expand Down Expand Up @@ -212,7 +223,7 @@ protected function _createTemporaryFlatTable($storeId)
* @param string $valueFieldSuffix
* @return void
* @throws LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws NoSuchEntityException
*/
protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldSuffix)
{
Expand All @@ -228,8 +239,8 @@ protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldS
$websiteId = (int)$this->_storeManager->getStore($storeId)->getWebsiteId();

unset($tables[$entityTableName]);

$allColumns = array_values(
$allColumns = [];
$allColumns[] = array_values(
array_unique(
array_merge(['entity_id', $linkField, 'type_id', 'attribute_set_id'], $columnsList)
)
Expand Down Expand Up @@ -278,7 +289,7 @@ protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldS
sprintf('e.%1$s = %2$s.%1$s', $linkField, $temporaryTableName),
$columnsNames
);
$allColumns = array_merge($allColumns, $columnsNames);
$allColumns[] = $columnsNames;

foreach ($columnsNames as $name) {
$columnValueName = $name . $valueFieldSuffix;
Expand All @@ -292,10 +303,10 @@ protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldS
sprintf('e.%1$s = %2$s.%1$s', $linkField, $temporaryValueTableName),
$columnValueNames
);
$allColumns = array_merge($allColumns, $columnValueNames);
$allColumns[] = $columnValueNames;
}
}
$sql = $select->insertFromSelect($temporaryFlatTableName, $allColumns, false);
$sql = $select->insertFromSelect($temporaryFlatTableName, array_merge(...$allColumns), false);
$this->_connection->query($sql);
}

Expand Down Expand Up @@ -368,8 +379,12 @@ protected function _updateTemporaryTableByStoreValues(
)->where($columnValue . ' IS NOT NULL');
if (!empty($changedIds)) {
$select->where(
$this->_connection->quoteInto('et.entity_id IN (?)', $changedIds, \Zend_Db::BIGINT_TYPE))
;
$this->_connection->quoteInto(
'et.entity_id IN (?)',
$changedIds,
\Zend_Db::BIGINT_TYPE
)
);
}
$sql = $select->crossUpdateFromSelect(['et' => $temporaryFlatTableName]);
$this->_connection->query($sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Magento\Store\Model\Store;

/**
* Class TableBuilder
* Prepare temporary tables structure for product flat indexer
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ protected function getIndexTargetTable()
}

/**
* Return Product ID field name
*
* @return string
*/
protected function getProductIdFieldName()
Expand Down Expand Up @@ -553,6 +555,7 @@ private function getProductsTypes(array $changedIds = [])

/**
* Get parent products types
*
* Used for add composite products to reindex if we have only simple products in changed ids set
*
* @param array $productsIds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,18 @@ protected function _joinLinks()
$linkField = $this->getLinkField();
if ($this->productIds) {
if ($this->_isStrongMode) {
$this->getSelect()->where('links.product_id in (?)', $this->productIds, \Zend_Db::BIGINT_TYPE);
$this->getSelect()->where(
'links.product_id in (?)',
$this->productIds,
\Zend_Db::BIGINT_TYPE
);
} else {
$joinType = 'joinLeft';
$joinCondition[] = $connection->quoteInto('links.product_id in (?)', $this->productIds, \Zend_Db::BIGINT_TYPE);
$joinCondition[] = $connection->quoteInto(
'links.product_id in (?)',
$this->productIds,
\Zend_Db::BIGINT_TYPE
);
}
if (count($this->productIds) === 1) {
$this->addFieldToFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ public function getRealMemoryUsage()
*/
protected function _getUnixProcessMemoryUsage($pid)
{
if (!$this->isMacOS()
&& ($content = @file_get_contents('/proc/' . (int) $pid . '/status'))
&& \preg_match('/VmRSS:\s*(\d* \w)/mi', $content, $m)
&& !empty($m[1])
) {
return self::convertToBytes($m[1]);
}
// RSS - resident set size, the non-swapped physical memory
$command = 'ps --pid %s --format rss --no-headers';
if ($this->isMacOS()) {
Expand Down

0 comments on commit 6587c57

Please sign in to comment.