-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from lizardmedia/develop
initial work
- Loading branch information
Showing
24 changed files
with
6,797 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: IndexerProcessorInterface.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Api; | ||
|
||
use LizardMedia\AdminIndexer\Exception\ReindexFailureException; | ||
|
||
/** | ||
* Interface IndexerProcessorInterface | ||
* @package LizardMedia\AdminIndexer\Api | ||
*/ | ||
interface IndexerProcessorInterface | ||
{ | ||
/** | ||
* @param string ...$indexerIds | ||
* @return void | ||
* @throws ReindexFailureException | ||
*/ | ||
public function process(string ...$indexerIds): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: AsyncReindexRunnerInterface.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Api\ReindexRunner; | ||
|
||
use LizardMedia\AdminIndexer\Api\ReindexRunnerInterface; | ||
|
||
/** | ||
* Interface AsyncReindexRunnerInterface | ||
* @package LizardMedia\AdminIndexer\Api\ReindexRunner | ||
*/ | ||
interface AsyncReindexRunnerInterface extends ReindexRunnerInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: MessageBagInterface.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Api\ReindexRunner; | ||
|
||
/** | ||
* Class MessageBagInterface | ||
* @package LizardMedia\AdminIndexer\Api\ReindexRunner | ||
*/ | ||
interface MessageBagInterface | ||
{ | ||
/** | ||
* @param string $message | ||
*/ | ||
public function addMessage(string $message) : void; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getMessages(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: SyncReindexRunnerInterface.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Api\ReindexRunner; | ||
|
||
use LizardMedia\AdminIndexer\Api\ReindexRunnerInterface; | ||
|
||
/** | ||
* Interface SyncReindexRunnerInterface | ||
* @package LizardMedia\AdminIndexer\Api\ReindexRunner | ||
*/ | ||
interface SyncReindexRunnerInterface extends ReindexRunnerInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: ReindexRunnerInterface.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Api; | ||
|
||
/** | ||
* Interface ReindexRunnerInterface | ||
* @package LizardMedia\AdminIndexer\Api | ||
*/ | ||
interface ReindexRunnerInterface | ||
{ | ||
/** | ||
* @param string $indexerId | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function run(string $indexerId): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: MassReindex.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Controller\Adminhtml\Indexer; | ||
|
||
use LizardMedia\AdminIndexer\Api\IndexerProcessorInterface; | ||
use LizardMedia\AdminIndexer\Api\ReindexRunner\MessageBagInterface; | ||
use LizardMedia\AdminIndexer\Exception\ReindexFailureException; | ||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\Controller\Result\Redirect; | ||
use Magento\Framework\Controller\Result\RedirectFactory; | ||
|
||
/** | ||
* Class MassReindex | ||
* @package LizardMedia\AdminIndexer\Controller\Adminhtml\Indexer | ||
*/ | ||
class MassReindex extends Action | ||
{ | ||
/** | ||
* @const string | ||
*/ | ||
const ADMIN_RESOURCE = 'LizardMedia_AdminIndexer::indexer'; | ||
|
||
/** | ||
* @var MessageBagInterface | ||
*/ | ||
private $messageBag; | ||
|
||
/** | ||
* @var RedirectFactory | ||
*/ | ||
private $redirectFactory; | ||
|
||
/** | ||
* @var IndexerProcessorInterface | ||
*/ | ||
private $indexerProcessor; | ||
|
||
/** | ||
* MassReindex constructor. | ||
* @param MessageBagInterface $messageBag | ||
* @param Context $context | ||
* @param RedirectFactory $redirectFactory | ||
* @param IndexerProcessorInterface $indexerProcessor | ||
*/ | ||
public function __construct( | ||
MessageBagInterface $messageBag, | ||
Context $context, | ||
RedirectFactory $redirectFactory, | ||
IndexerProcessorInterface $indexerProcessor | ||
) { | ||
parent::__construct($context); | ||
|
||
$this->messageBag = $messageBag; | ||
$this->redirectFactory = $redirectFactory; | ||
$this->indexerProcessor = $indexerProcessor; | ||
} | ||
|
||
/** | ||
* @return Redirect | ||
*/ | ||
public function execute() : Redirect | ||
{ | ||
$indexerIds = $this->getRequest()->getParam('indexer_ids'); | ||
|
||
if (!$this->validateParam($indexerIds)) { | ||
$this->messageManager->addErrorMessage(__('Please select at least one index.')); | ||
return $this->getRedirect(); | ||
} | ||
$indexerIds = $this->castValuesToString($indexerIds); | ||
|
||
try { | ||
$this->indexerProcessor->process(...$indexerIds); | ||
$this->displayMessages(); | ||
} catch (ReindexFailureException $exception) { | ||
$this->messageManager->addErrorMessage(__('Reindex failed on indexer %1.', $exception->getIndexerName())); | ||
} | ||
|
||
return $this->getRedirect(); | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
protected function _isAllowed(): bool | ||
{ | ||
return $this->_authorization->isAllowed(self::ADMIN_RESOURCE); | ||
} | ||
|
||
/** | ||
* @return Redirect | ||
*/ | ||
private function getRedirect(): Redirect | ||
{ | ||
return $this->redirectFactory->create()->setPath('indexer/indexer/list'); | ||
} | ||
|
||
/** | ||
* @param $indexerIds | ||
* @return bool | ||
*/ | ||
private function validateParam($indexerIds): bool | ||
{ | ||
return is_array($indexerIds) && !empty($indexerIds); | ||
} | ||
|
||
/** | ||
* @param array $indexerIds | ||
* @return array | ||
*/ | ||
private function castValuesToString(array $indexerIds): array | ||
{ | ||
return array_filter($indexerIds, function ($indexerId) { | ||
return (string)$indexerId; | ||
}); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
private function displayMessages(): void | ||
{ | ||
foreach ($this->messageBag->getMessages() as $message) { | ||
$this->messageManager->addNoticeMessage($message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: ReindexFailureException.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Exception; | ||
|
||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Phrase; | ||
|
||
class ReindexFailureException extends LocalizedException | ||
{ | ||
private $indexerName; | ||
|
||
/** | ||
* ReindexFailureException constructor. | ||
* @param Phrase $phrase | ||
* @param string $indexerName | ||
* @param \Exception|null $cause | ||
* @param int $code | ||
*/ | ||
public function __construct(Phrase $phrase, string $indexerName, \Exception $cause = null, int $code = 0) | ||
{ | ||
parent::__construct($phrase, $cause, $code); | ||
|
||
$this->indexerName = $indexerName; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getIndexerName(): string | ||
{ | ||
return $this->indexerName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* File: IndexerProcessor.php | ||
* | ||
* @author Bartosz Kubicki [email protected]> | ||
* @author Paweł Papke <[email protected]> | ||
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) | ||
*/ | ||
|
||
namespace LizardMedia\AdminIndexer\Model; | ||
|
||
use LizardMedia\AdminIndexer\Api\IndexerProcessorInterface; | ||
use LizardMedia\AdminIndexer\Api\ReindexRunnerInterface; | ||
use LizardMedia\AdminIndexer\Exception\ReindexFailureException; | ||
|
||
/** | ||
* Class IndexerProcessor | ||
* @package LizardMedia\AdminIndexer\Model | ||
*/ | ||
class IndexerProcessor implements IndexerProcessorInterface | ||
{ | ||
/** | ||
* @var ReindexRunnerInterface | ||
*/ | ||
private $reindexRunner; | ||
|
||
/** | ||
* IndexerProcessor constructor. | ||
* @param ReindexRunnerInterface $reindexRunner | ||
*/ | ||
public function __construct( | ||
ReindexRunnerInterface $reindexRunner | ||
) { | ||
$this->reindexRunner = $reindexRunner; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function process(string ...$indexerIds): void | ||
{ | ||
foreach ($indexerIds as $indexerId) { | ||
try { | ||
$this->reindexRunner->run($indexerId); | ||
} catch (\Exception $exception) { | ||
throw new ReindexFailureException( | ||
__($exception->getMessage()), | ||
$indexerId, | ||
$exception, | ||
$exception->getCode() | ||
); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.