dateTime = $dateTime; $this->logEntryRepository = $logEntryRepository; $this->criteriaBuilderFactory = $criteriaBuilderFactory; $this->exportFactory = $exportFactory; $this->config = $config; } /** * Rotate log entries older than the given lifetime value. * * @param int $lifetime The lifetime in seconds * @return void * @throws \Magento\Framework\Exception\CouldNotDeleteException * @throws \Magento\Framework\Exception\FileSystemException * @throws \Magento\Framework\Exception\NotFoundException * @throws \Magento\Framework\Exception\LocalizedException */ public function rotate($lifetime) { $clearAfter = $this->dateTime->formatDate(time() - $lifetime); /** @var SearchCriteriaBuilder $findCriteriaBuilder */ $findCriteriaBuilder = $this->criteriaBuilderFactory->create(); $findCriteriaBuilder->addFilter(LogEntryInterface::FIELD_REQUEST_DATE, $clearAfter, 'lteq'); $findCriteriaBuilder->setPageSize(100); $findCriteria = $findCriteriaBuilder->create(); while(($entries = $this->logEntryRepository->getList($findCriteria)) && $entries->getTotalCount()) { /** @var LogEntrySearchResultsInterface $entries */ if ($this->config->getRotationAction() === RotationAction::TYPE_EXPORT) { $this->export($entries); } $entityIds = array_map( function (LogEntryInterface $logEntry) { return $logEntry->getId(); }, $entries->getItems() ); /** @var SearchCriteriaBuilder $deleteCriteriaBuilder */ $deleteCriteriaBuilder = $this->criteriaBuilderFactory->create(); $deleteCriteriaBuilder->addFilter(LogEntryInterface::FIELD_ID, $entityIds, 'in'); $deleteCriteria = $deleteCriteriaBuilder->create(); $this->logEntryRepository->deleteByCriteria($deleteCriteria); unset($entries, $entityIds, $deleteCriteria); } } /** * Export the given log entry set. * * @param LogEntrySearchResultsInterface $entries * @return void * @throws \Magento\Framework\Exception\FileSystemException * @throws \Magento\Framework\Exception\NotFoundException */ private function export(LogEntrySearchResultsInterface $entries) { /** @var LogEntryExport $export */ $export = $this->exportFactory->create(); $export->open(); $export->writeHeader(); /** @var \Vertex\Tax\Api\Data\LogEntryInterface $entry */ foreach ($entries->getItems() as $entry) { $export->write($entry); } $export->close(); } }