1 | <?php |
||
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 |
||
99 | |||
100 | /** |
||
101 | * @return Redirect |
||
102 | */ |
||
103 | private function getRedirect(): Redirect |
||
107 | |||
108 | /** |
||
109 | * @param $indexerIds |
||
110 | * @return bool |
||
111 | */ |
||
112 | private function validateParam($indexerIds): bool |
||
116 | |||
117 | /** |
||
118 | * @param array $indexerIds |
||
119 | * @return array |
||
120 | */ |
||
121 | private function castValuesToString(array $indexerIds): array |
||
127 | |||
128 | /** |
||
129 | * @return void |
||
130 | */ |
||
131 | private function displayMessagesAboutRunningIndexers(): void |
||
137 | |||
138 | /** |
||
139 | * @param ReindexFailureAggregateException $exception |
||
140 | * @return void |
||
141 | */ |
||
142 | private function displayErrors(ReindexFailureAggregateException $exception): void |
||
151 | } |
||
152 |