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

Minor refactoring for product resource model #126

Merged
merged 1 commit into from
Oct 8, 2019
Merged
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
64 changes: 25 additions & 39 deletions src/module-vsbridge-indexer-catalog/Model/ResourceModel/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
namespace Divante\VsbridgeIndexerCatalog\Model\ResourceModel;

use Divante\VsbridgeIndexerCatalog\Api\Data\CatalogConfigurationInterface;
use Divante\VsbridgeIndexerCatalog\Model\ProductMetaData;
use Divante\VsbridgeIndexerCatalog\Model\ResourceModel\Product\AttributeDataProvider;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Helper as DbHelper;
use Magento\Framework\DB\Select;
use Magento\Store\Model\StoreManagerInterface;
use Divante\VsbridgeIndexerCatalog\Model\ProductMetaData;

/**
* Class Product
Expand Down Expand Up @@ -97,11 +97,8 @@ public function __construct(
*/
public function getProducts($storeId = 1, array $productIds = [], $fromId = 0, $limit = 1000)
{
$select = $this->getConnection()->select()
->from(
['entity' => $this->productMetaData->get()->getEntityTable()],
$this->getRequiredColumns()
);
$select = $this->prepareBaseProductSelect($this->getRequiredColumns(), $storeId);
$select = $this->addProductTypeFilter($select, $storeId);

if (!empty($productIds)) {
$select->where('entity.entity_id IN (?)', $productIds);
Expand All @@ -110,16 +107,34 @@ public function getProducts($storeId = 1, array $productIds = [], $fromId = 0, $
$select->limit($limit);
$select->where('entity.entity_id > ?', $fromId);
$select->order('entity.entity_id ASC');

return $this->getConnection()->fetchAll($select);
}

/**
* @param array $requiredColumns
* @param int $storeId
*
* @return Select
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function prepareBaseProductSelect(array $requiredColumns, int $storeId)
{
$select = $this->getConnection()->select()
->from(
['entity' => $this->productMetaData->get()->getEntityTable()],
$requiredColumns
);

$select = $this->addStatusFilter($select, $storeId);
$select = $this->addWebsiteFilter($select, $storeId);
$select = $this->addProductTypeFilter($select, $storeId);

return $this->getConnection()->fetchAll($select);
return $select;
}

/**
* @return array
* @throws \Exception
*/
private function getRequiredColumns()
{
Expand Down Expand Up @@ -162,24 +177,18 @@ public function loadChildrenProducts(array $parentIds, $storeId)
$columns[] = $linkField;
}

$select = $this->getConnection()->select()->from(
['entity' => $this->productMetaData->get()->getEntityTable()],
$columns
);
$select = $this->prepareBaseProductSelect($columns, $storeId);

$select->join(
['link_table' => $this->resourceConnection->getTableName('catalog_product_super_link')],
'link_table.product_id = entity.entity_id',
[]
);

$select = $this->addStatusFilter($select, $storeId);

$select->where('link_table.parent_id IN (?)', $parentIds);
$select->group('entity_id');

$this->dbHelper->addGroupConcatColumn($select, 'parent_ids', 'parent_id');
$select = $this->addWebsiteFilter($select, $storeId);

return $this->getConnection()->fetchAll($select);
}
Expand Down Expand Up @@ -256,29 +265,6 @@ public function getRelationsByChild(array $childrenIds)
return $this->getConnection()->fetchCol($select);
}

/**
* @param int $storeId
* @param array $productIds
*
* @return array
*
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getEnableProductIds($storeId, array $productIds)
{
$select = $this->getConnection()->select()
->from(['e' => $this->resourceConnection->getTableName('catalog_product_entity')]);
$select->where('e.entity_id IN (?)', $productIds);
$select->order('e.entity_id ASC');
$select = $this->addStatusFilter($select, $storeId);
$select = $this->addWebsiteFilter($select, $storeId);
$select->reset(Select::COLUMNS);
$select->columns(['entity_id']);

return $this->getConnection()->fetchCol($select);
}

/**
* @param \Magento\Framework\DB\Select $select
* @param int $storeId
Expand Down