restoreProcessor = $restoreProcessor; $this->restoreValidator = $restoreValidator; $this->filter = $filter; $this->batch = $batch; $this->collectionFactory = $collectionFactory; $this->logEntryFactory = $logEntryFactory; $this->entryRepository = $entryRepository; $this->logger = $logger; } public function execute() { $this->filter->applySelectionOnTargetProvider(); $collection = $this->filter->getCollection($this->collectionFactory->create()); $successRestore = $skippedRestore = $failedRestore = []; /** @var \Magento\Framework\Api\Search\DocumentInterface $logEntryDocument */ foreach ($this->batch->getItems($collection, 10) as $batch) { foreach ($batch as $logEntryDocument) { $logEntry = $this->entryRepository->getById((int)$logEntryDocument->getId()); if ($this->restoreValidator->isValid($logEntry)) { try { $this->restoreProcessor->restoreChanges($logEntry); $successRestore[] = $logEntry->getId(); } catch (LocalizedException $e) { $failedRestore[] = $logEntry->getId(); } catch (\Exception $e) { $failedRestore[] = $logEntry->getId(); $this->logger->critical($e); } } else { $skippedRestore[] = $logEntry->getId(); } } } if (!empty($successRestore)) { $this->messageManager->addSuccessMessage( __('A total of %1 record(s) have been restored.', count($successRestore)) ); } if (!empty($skippedRestore)) { $this->messageManager->addWarningMessage( __('The restoring of records (%1) is not allowed.', implode(', ', $skippedRestore)) ); } if (!empty($failedRestore)) { $this->messageManager->addErrorMessage( __('Failed to restore the records (%1).', implode(', ', $failedRestore)) ); } return $this->resultRedirectFactory->create()->setPath('*/*/index'); } }