Skip to content

Commit

Permalink
Merge pull request #1 from lizardmedia/develop
Browse files Browse the repository at this point in the history
initial work
  • Loading branch information
maciejslawik authored Aug 2, 2018
2 parents fffaec3 + 57719cf commit 8cc7061
Show file tree
Hide file tree
Showing 24 changed files with 6,797 additions and 2 deletions.
29 changes: 29 additions & 0 deletions Api/IndexerProcessorInterface.php
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;
}
23 changes: 23 additions & 0 deletions Api/ReindexRunner/AsyncReindexRunnerInterface.php
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
{
}
30 changes: 30 additions & 0 deletions Api/ReindexRunner/MessageBagInterface.php
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;
}
23 changes: 23 additions & 0 deletions Api/ReindexRunner/SyncReindexRunnerInterface.php
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
{
}
27 changes: 27 additions & 0 deletions Api/ReindexRunnerInterface.php
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;
}
137 changes: 137 additions & 0 deletions Controller/Adminhtml/Indexer/MassReindex.php
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);
}
}
}
43 changes: 43 additions & 0 deletions Exception/ReindexFailureException.php
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;
}
}
58 changes: 58 additions & 0 deletions Model/IndexerProcessor.php
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()
);
}
}
}
}
Loading

0 comments on commit 8cc7061

Please sign in to comment.