1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* File: MassReindex.php |
7
|
|
|
* |
8
|
|
|
* @author Bartosz Kubicki [email protected]> |
9
|
|
|
* @author Paweł Papke <[email protected]> |
10
|
|
|
* @copyright Copyright (C) 2018 Lizard Media (http://lizardmedia.pl) |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace LizardMedia\AdminIndexer\Controller\Adminhtml\Indexer; |
14
|
|
|
|
15
|
|
|
use LizardMedia\AdminIndexer\Api\IndexerProcessorInterface; |
16
|
|
|
use LizardMedia\AdminIndexer\Api\ReindexRunner\MessageBagInterface; |
17
|
|
|
use LizardMedia\AdminIndexer\Exception\ReindexFailureAggregateException; |
18
|
|
|
use Magento\Backend\App\Action; |
19
|
|
|
use Magento\Backend\App\Action\Context; |
20
|
|
|
use Magento\Framework\Controller\Result\Redirect; |
21
|
|
|
use Magento\Framework\Controller\Result\RedirectFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class MassReindex |
25
|
|
|
* @package LizardMedia\AdminIndexer\Controller\Adminhtml\Indexer |
26
|
|
|
*/ |
27
|
|
|
class MassReindex extends Action |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @const string |
31
|
|
|
*/ |
32
|
|
|
const ADMIN_RESOURCE = 'LizardMedia_AdminIndexer::indexer'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var MessageBagInterface |
36
|
|
|
*/ |
37
|
|
|
private $messageBag; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RedirectFactory |
41
|
|
|
*/ |
42
|
|
|
private $redirectFactory; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var IndexerProcessorInterface |
46
|
|
|
*/ |
47
|
|
|
private $indexerProcessor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* MassReindex constructor. |
51
|
|
|
* @param MessageBagInterface $messageBag |
52
|
|
|
* @param Context $context |
53
|
|
|
* @param RedirectFactory $redirectFactory |
54
|
|
|
* @param IndexerProcessorInterface $indexerProcessor |
55
|
|
|
*/ |
56
|
|
|
public function __construct( |
57
|
|
|
MessageBagInterface $messageBag, |
58
|
|
|
Context $context, |
59
|
|
|
RedirectFactory $redirectFactory, |
60
|
|
|
IndexerProcessorInterface $indexerProcessor |
61
|
|
|
) { |
62
|
|
|
parent::__construct($context); |
63
|
|
|
$this->messageBag = $messageBag; |
64
|
|
|
$this->redirectFactory = $redirectFactory; |
65
|
|
|
$this->indexerProcessor = $indexerProcessor; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return Redirect |
70
|
|
|
*/ |
71
|
|
|
public function execute(): Redirect |
72
|
|
|
{ |
73
|
|
|
$indexerIds = $this->getRequest()->getParam('indexer_ids'); |
74
|
|
|
|
75
|
|
|
if (!$this->validateParam($indexerIds)) { |
76
|
|
|
$this->messageManager->addErrorMessage(__('Please select at least one index.')); |
77
|
|
|
return $this->getRedirect(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$indexerIds = $this->castValuesToString($indexerIds); |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
$this->indexerProcessor->process(...$indexerIds); |
84
|
|
|
$this->displayMessagesAboutRunningIndexers(); |
85
|
|
|
} catch (ReindexFailureAggregateException $exception) { |
86
|
|
|
$this->displayErrors($exception); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->getRedirect(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
protected function _isAllowed(): bool |
96
|
|
|
{ |
97
|
|
|
return $this->_authorization->isAllowed(self::ADMIN_RESOURCE); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Redirect |
102
|
|
|
*/ |
103
|
|
|
private function getRedirect(): Redirect |
104
|
|
|
{ |
105
|
|
|
return $this->redirectFactory->create()->setPath('indexer/indexer/list'); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param $indexerIds |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
private function validateParam($indexerIds): bool |
113
|
|
|
{ |
114
|
|
|
return is_array($indexerIds) && !empty($indexerIds); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $indexerIds |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
private function castValuesToString(array $indexerIds): array |
122
|
|
|
{ |
123
|
|
|
return array_filter($indexerIds, function ($indexerId) { |
124
|
|
|
return (string) $indexerId; |
125
|
|
|
}); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return void |
130
|
|
|
*/ |
131
|
|
|
private function displayMessagesAboutRunningIndexers(): void |
132
|
|
|
{ |
133
|
|
|
foreach ($this->messageBag->getMessages() as $message) { |
134
|
|
|
$this->messageManager->addNoticeMessage($message); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param ReindexFailureAggregateException $exception |
140
|
|
|
* @return void |
141
|
|
|
*/ |
142
|
|
|
private function displayErrors(ReindexFailureAggregateException $exception): void |
143
|
|
|
{ |
144
|
|
|
$this->messageManager->addErrorMessage($exception->getMessage()); |
145
|
|
|
$errors = $exception->getErrors(); |
146
|
|
|
|
147
|
|
|
foreach ($errors as $error) { |
148
|
|
|
$this->messageManager->addErrorMessage($error->getMessage()); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|