loggerService = ServiceManager::getServiceManager()->get(LoggerService::SERVICE_ID); $this->filters = $filters; $this->sortColumn = $sortColumn; $this->sortOrder = $sortOrder; } /** * @return \Generator * * @throws \common_exception_Error */ public function fetch() { $internalLimit = $this->loggerService->hasOption(LoggerService::OPTION_FETCH_LIMIT) ? $this->loggerService->getOption(LoggerService::OPTION_FETCH_LIMIT) : 500; $limit = $this->loggerService->getOption(LoggerService::OPTION_EXPORTABLE_QUANTITY); $options = [ 'sort' => $this->sortColumn, 'order' => $this->sortOrder, ]; $preparedFilters = $this->prepareFilters($this->filters); $lastId = null; $fetched = 0; do { $extendedPreparedFilters = (null !== $lastId) ? array_merge($preparedFilters, [['id', '<', $lastId]]) : $preparedFilters; $leftToFetch = $limit - $fetched; $options['limit'] = $leftToFetch < $internalLimit ? $leftToFetch : $internalLimit; $logs = $this->loggerService->search($extendedPreparedFilters, $options); $count = count($logs); if ($count > 0) { $fetched += $count; $lastId = $logs[$count - 1]['id']; foreach ($logs as $log) { yield $log; } } } while ($count > 0 && $fetched < $limit); } /** * @param array $filters * * @return array * * @throws \common_exception_Error * @throws \Exception */ private function prepareFilters(array $filters = []) { /** @var \common_session_Session $session */ $session = common_session_SessionManager::getSession(); $timeZone = new DateTimeZone($session->getTimeZone()); $utc = new DateTimeZone('UTC'); $result = []; foreach ($filters as $name => $value) { if (!empty($value)) { switch ($name) { case 'from': $from = new DateTimeImmutable($filters['from'], $timeZone); $result[] = ['occurred', '>', $from->setTimezone($utc)->format(DateTime::ISO8601)]; break; case 'to': $to = new DateTimeImmutable($filters['to'], $timeZone); $result[] = ['occurred', '<=', $to->setTimezone($utc)->format(DateTime::ISO8601)]; break; default: $result[] = [$name, 'LIKE', "%$value%"]; } } } return $result; } }