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

Product indexer - add dependencies. #176

Merged
merged 1 commit into from
Dec 20, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

namespace Divante\VsbridgeIndexerCatalog\Model\Indexer;

use Magento\Framework\Indexer\{Config\DependencyInfoProviderInterface, IndexerRegistry};

/**
* Class ProductProcessor
*/
Expand All @@ -18,6 +20,25 @@ class ProductProcessor extends \Magento\Framework\Indexer\AbstractProcessor
*/
const INDEXER_ID = 'vsbridge_product_indexer';

/**
* @var DependencyInfoProviderInterface
*/
private $dependencyInfoProvider;

/**
* ProductProcessor constructor.
*
* @param DependencyInfoProviderInterface $dependencyInfoProvider
* @param IndexerRegistry $indexerRegistry
*/
public function __construct(
DependencyInfoProviderInterface $dependencyInfoProvider,
IndexerRegistry $indexerRegistry
) {
parent::__construct($indexerRegistry);
$this->dependencyInfoProvider = $dependencyInfoProvider;
}

/**
* Mark Vsbridge Product indexer as invalid
*
Expand All @@ -27,4 +48,50 @@ public function markIndexerAsInvalid()
{
$this->getIndexer()->invalidate();
}

/**
* Run Row reindex
*
* @param int $id
* @param bool $forceReindex
* @return void
*/
public function reindexRow($id, $forceReindex = false)
{
if ($this->hasToReindex()) {
parent::reindexRow($id, $forceReindex);
}
}

/**
* @param int[] $ids
* @param bool $forceReindex
*/
public function reindexList($ids, $forceReindex = false)
{
if ($this->hasToReindex()) {
parent::reindexList($ids, $forceReindex);
}
}

/**
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function hasToReindex(): bool
{
$hasToRun = true;
$dependentIndexerIds = $this->dependencyInfoProvider->getIndexerIdsToRunBefore($this->getIndexerId());

foreach ($dependentIndexerIds as $indexerId) {
$dependentIndexer = $this->indexerRegistry->get($indexerId);

if (!$dependentIndexer->isScheduled()) {
$hasToRun = false;
break;
}
}

return $hasToRun;
}
}
57 changes: 57 additions & 0 deletions src/module-vsbridge-indexer-catalog/Plugin/Mview/ViewPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Divante\VsbridgeIndexerCatalog\Plugin\Mview;

use Divante\VsbridgeIndexerCatalog\Model\Indexer\ProductProcessor;
use Divante\VsbridgeIndexerCatalog\Api\Data\CatalogConfigurationInterface;
use Magento\Framework\Mview\ViewInterface;

/**
* Class ViewPlugin
*/
class ViewPlugin
{
/**
* @var CatalogConfigurationInterface
*/
private $catalogSettings;

/**
* ViewPlugin constructor.
*
* @param CatalogConfigurationInterface $catalogSettings
*/
public function __construct(CatalogConfigurationInterface $catalogSettings)
{
$this->catalogSettings = $catalogSettings;
}

/**
* @param ViewInterface $subject
* @param array $result
*
* @return array
*/
public function afterGetSubscriptions(ViewInterface $subject, array $result): array
{
if ($this->catalogSettings->useCatalogRules() && $this->isVsbridgeProductIndexer($subject)) {
$result['catalogrule_product_price'] = [
'name' => 'catalogrule_product_price',
'column' => 'product_id',
'subscription_model' => null,
];
}

return $result;
}

/**
* @param ViewInterface $subject
*
* @return bool
*/
private function isVsbridgeProductIndexer(ViewInterface $subject): bool
{
return ProductProcessor::INDEXER_ID === $subject->getId();
}
}
4 changes: 4 additions & 0 deletions src/module-vsbridge-indexer-catalog/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
</arguments>
</virtualType>

<type name="Magento\Framework\Mview\ViewInterface">
<plugin name="updateMviewSubscriptions" type="Divante\VsbridgeIndexerCatalog\Plugin\Mview\ViewPlugin"/>
</type>

<type name="Divante\VsbridgeIndexerCatalog\Model\Indexer\Product">
<arguments>
<argument name="indexerHandler" xsi:type="object">Divante\VsbridgeIndexerCatalog\Indexer\ProductIndexerHandlerVirtual</argument>
Expand Down
4 changes: 4 additions & 0 deletions src/module-vsbridge-indexer-catalog/etc/indexer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
class="Divante\VsbridgeIndexerCatalog\Model\Indexer\Product">
<title translate="true">Vsbridge Product Indexer</title>
<description translate="true">Update Products data in Elastic</description>
<dependencies>
<indexer id="catalog_product_price" />
<indexer id="cataloginventory_stock" />
</dependencies>
</indexer>
<indexer id="vsbridge_category_indexer" view_id="vsbridge_category_indexer"
class="Divante\VsbridgeIndexerCatalog\Model\Indexer\Category">
Expand Down