Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/SamJUK/magento2-reindex int…
Browse files Browse the repository at this point in the history
…o SamJUK-master
  • Loading branch information
srenon committed Dec 28, 2020
2 parents 5c49c64 + 6bb5009 commit 133f6b8
Show file tree
Hide file tree
Showing 18 changed files with 469 additions and 7 deletions.
21 changes: 21 additions & 0 deletions Api/ReindexInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/

namespace MagePal\Reindex\Api;

/**
* Handles the implementation of the reindex logic
*
* @api
*/
interface ReindexInterface
{
/**
* @param array|null $indexIds
*/
public function reindex(array $indexIds = null) : void;
}
21 changes: 21 additions & 0 deletions Api/StrategyInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/

namespace MagePal\Reindex\Api;

/**
* Handles strategy for reindexing from backend. Process now, defer to message queue etc.
*
* @api
*/
interface StrategyInterface
{
/**
* @param array|null $indexIds
*/
public function process(array $indexIds = null) : void;
}
19 changes: 19 additions & 0 deletions Api/SubscriberInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* Copyright © MagePal LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/

namespace MagePal\Reindex\Api;

/**
* @api
*/
interface SubscriberInterface
{
/**
* @param array $indices
*/
public function processMessage(array $indices) : void;
}
36 changes: 36 additions & 0 deletions Block/Adminhtml/System/Config/Source/Strategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © MagePal, LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\Reindex\Block\Adminhtml\System\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class Strategy implements OptionSourceInterface
{
/** @var array */
private $strategies;

/**
* Strategy constructor.
* @param array $strategies
*/
public function __construct(array $strategies)
{
$this->strategies = $strategies;
}

/**
* @return array
*/
public function toOptionArray() : array
{
$options = [];
foreach ($this->strategies as $key => $label) {
$options[] = ['value' => $key, 'label' => $label];
}
return $options;
}
}
23 changes: 16 additions & 7 deletions Controller/Adminhtml/Indexer/ReindexOnTheFly.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,34 @@

use Exception;
use Magento\Backend\App\Action\Context;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Indexer\IndexerInterface;
use Magento\Indexer\Model\IndexerFactory;
use MagePal\Reindex\Controller\Adminhtml\Indexer;
use MagePal\Reindex\Api\StrategyInterface;

class ReindexOnTheFly extends Indexer
{

/** @var IndexerInterface */
protected $indexerFactory;

/** @var StrategyInterface */
private $reindexStrategy;

/**
* Index constructor.
* @param Context $context
* @param IndexerFactory $indexerFactory
*/
public function __construct(
Context $context,
IndexerFactory $indexerFactory
IndexerFactory $indexerFactory,
StrategyInterface $reindexStrategy
) {
$this->indexerFactory = $indexerFactory;
$this->reindexStrategy = $reindexStrategy;
parent::__construct($context);
}

Expand All @@ -41,13 +49,14 @@ public function execute()
$this->messageManager->addErrorMessage(__('Please select indexers.'));
} else {
try {
foreach ($indexerIds as $indexerId) {
$indexer = $this->indexerFactory->create();
$indexer->load($indexerId)->reindexAll();
}

$this->reindexStrategy->process($indexerIds);
$this->messageManager->addSuccessMessage(
__('Reindex %1 indexer(s).', count($indexerIds))
__('Reindex triggered for %1 indexer(s).', count($indexerIds))
);
} catch (InputException | LocalizedException $e) {
$this->messageManager->addExceptionMessage(
$e,
__("We couldn't reindex because of an error: {$e->getMessage()}")
);
} catch (Exception $e) {
$this->messageManager->addExceptionMessage(
Expand Down
38 changes: 38 additions & 0 deletions Model/Reindex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © MagePal, LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\Reindex\Model;

use Magento\Indexer\Model\IndexerFactory;
use MagePal\Reindex\Api\ReindexInterface;

class Reindex implements ReindexInterface
{
/** @var IndexerFactory */
private $indexerFactory;

/**
* Reindex constructor.
* @param IndexerFactory $indexerFactory
*/
public function __construct(IndexerFactory $indexerFactory)
{
$this->indexerFactory = $indexerFactory;
}

/**
* Implements synchronous reindexing
*
* @param array|null $indexIds
*/
public function reindex(array $indexIds = null) : void
{
foreach ($indexIds as $index) {
$indexer = $this->indexerFactory->create();
$indexer->load($index)->reindexAll();
}
}
}
38 changes: 38 additions & 0 deletions Model/Strategies/Deferred.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © MagePal, LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\Reindex\Model\Strategies;

use MagePal\Reindex\Api\StrategyInterface;
use Magento\Framework\MessageQueue\PublisherInterface;

class Deferred implements StrategyInterface
{
const STRATEGY_KEY = 'deferred';
const TOPIC_NAME = 'magepal.reindex';

/** @var PublisherInterface */
private $publisher;

/**
* Deferred constructor.
* @param PublisherInterface $publisher
*/
public function __construct(PublisherInterface $publisher)
{
$this->publisher = $publisher;
}

/**
* Push the indexIDs to our message queue to processed by another process
*
* @param array|null $indexIds
*/
public function process(array $indexIds = null) : void
{
$this->publisher->publish(self::TOPIC_NAME, $indexIds);
}
}
37 changes: 37 additions & 0 deletions Model/Strategies/Standard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © MagePal, LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\Reindex\Model\Strategies;

use MagePal\Reindex\Api\StrategyInterface;
use MagePal\Reindex\Model\Reindex;

class Standard implements StrategyInterface
{
const STRATEGY_KEY = 'standard';

/** @var Reindex */
private $reindexService;

/**
* Standard constructor.
* @param Reindex $reindexService
*/
public function __construct(Reindex $reindexService)
{
$this->reindexService = $reindexService;
}

/**
* Handle the reindex within the current process
*
* @param array|null $indexIds
*/
public function process(array $indexIds = null) : void
{
$this->reindexService->reindex($indexIds);
}
}
33 changes: 33 additions & 0 deletions Model/Strategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © MagePal, LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\Reindex\Model;

use MagePal\Reindex\Api\StrategyInterface;

class Strategy implements StrategyInterface
{
/** @var StrategyResolver */
private $resolver;

/**
* Strategy constructor.
* @param StrategyResolver $resolver
*/
public function __construct(StrategyResolver $resolver)
{
$this->resolver = $resolver;
}

/**
* @param array|null $indexIds
* @throws \Magento\Framework\Exception\InputException
*/
public function process(array $indexIds = null) : void
{
$this->resolver->resolveActive()->process($indexIds);
}
}
72 changes: 72 additions & 0 deletions Model/StrategyResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Copyright © MagePal, LLC. All rights reserved.
* See COPYING.txt for license details.
* http://www.magepal.com | [email protected]
*/
namespace MagePal\Reindex\Model;

use MagePal\Reindex\Api\StrategyInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\InputException;

class StrategyResolver
{
const XML_STRATEGY_PATH = 'magepal_reindex/about/strategy';

/** @var ScopeConfigInterface */
private $scopeConfig;

/** @var array */
private $strategies;

/**
* StrategyResolver constructor.
* @param ScopeConfigInterface $scopeConfigInterface
* @param array $strategies
*/
public function __construct(
ScopeConfigInterface $scopeConfigInterface,
array $strategies
) {
$this->scopeConfig = $scopeConfigInterface;
$this->strategies = $strategies;
}

/**
* Resolve a strategy key to its correct class
*
* @param string $strategy
* @return StrategyInterface
* @throws InputException
*/
public function resolve(string $strategy) : StrategyInterface
{
if (!array_key_exists($strategy, $this->strategies)) {
throw new InputException(__("Invalid Strategy Key: $strategy"));
}

return $this->strategies[$strategy];
}

/**
* Handle resolving the current active strategy from system config to a class
*
* @return StrategyInterface
* @throws InputException
*/
public function resolveActive() : StrategyInterface
{
return $this->resolve($this->getActiveStrategy());
}

/**
* Get the active indexation strategy key from the backend
*
* @return string
*/
public function getActiveStrategy() : string
{
return $this->scopeConfig->getValue(self::XML_STRATEGY_PATH);
}
}
Loading

0 comments on commit 133f6b8

Please sign in to comment.