Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| LogEntryRepository | |
0.00% |
0 / 46 |
|
0.00% |
0 / 3 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| fetch | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
56 | |||
| prepareFilters | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2019 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoEventLog\model\export\implementation; |
| 22 | |
| 23 | use common_session_SessionManager; |
| 24 | use DateTime; |
| 25 | use DateTimeImmutable; |
| 26 | use DateTimeZone; |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\taoEventLog\model\eventLog\LoggerService; |
| 29 | use oat\taoEventLog\model\export\LogEntryRepositoryInterface; |
| 30 | |
| 31 | class LogEntryRepository implements LogEntryRepositoryInterface |
| 32 | { |
| 33 | /** @var LoggerService $loggerService */ |
| 34 | private $loggerService; |
| 35 | /** |
| 36 | * @var array |
| 37 | */ |
| 38 | private $filters; |
| 39 | /** |
| 40 | * @var string |
| 41 | */ |
| 42 | private $sortColumn; |
| 43 | /** |
| 44 | * @var string |
| 45 | */ |
| 46 | private $sortOrder; |
| 47 | |
| 48 | /** |
| 49 | * @param array $filters |
| 50 | * @param string $sortColumn |
| 51 | * @param string $sortOrder |
| 52 | */ |
| 53 | public function __construct(array $filters = [], $sortColumn = null, $sortOrder = null) |
| 54 | { |
| 55 | $this->loggerService = ServiceManager::getServiceManager()->get(LoggerService::SERVICE_ID); |
| 56 | $this->filters = $filters; |
| 57 | $this->sortColumn = $sortColumn; |
| 58 | $this->sortOrder = $sortOrder; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return \Generator |
| 63 | * |
| 64 | * @throws \common_exception_Error |
| 65 | */ |
| 66 | public function fetch() |
| 67 | { |
| 68 | $internalLimit = $this->loggerService->hasOption(LoggerService::OPTION_FETCH_LIMIT) |
| 69 | ? $this->loggerService->getOption(LoggerService::OPTION_FETCH_LIMIT) |
| 70 | : 500; |
| 71 | |
| 72 | $limit = $this->loggerService->getOption(LoggerService::OPTION_EXPORTABLE_QUANTITY); |
| 73 | |
| 74 | $options = [ |
| 75 | 'sort' => $this->sortColumn, |
| 76 | 'order' => $this->sortOrder, |
| 77 | ]; |
| 78 | |
| 79 | $preparedFilters = $this->prepareFilters($this->filters); |
| 80 | |
| 81 | $lastId = null; |
| 82 | |
| 83 | $fetched = 0; |
| 84 | |
| 85 | do { |
| 86 | $extendedPreparedFilters = (null !== $lastId) |
| 87 | ? array_merge($preparedFilters, [['id', '<', $lastId]]) |
| 88 | : $preparedFilters; |
| 89 | |
| 90 | $leftToFetch = $limit - $fetched; |
| 91 | $options['limit'] = $leftToFetch < $internalLimit ? $leftToFetch : $internalLimit; |
| 92 | |
| 93 | $logs = $this->loggerService->search($extendedPreparedFilters, $options); |
| 94 | |
| 95 | $count = count($logs); |
| 96 | |
| 97 | if ($count > 0) { |
| 98 | $fetched += $count; |
| 99 | $lastId = $logs[$count - 1]['id']; |
| 100 | |
| 101 | foreach ($logs as $log) { |
| 102 | yield $log; |
| 103 | } |
| 104 | } |
| 105 | } while ($count > 0 && $fetched < $limit); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * @param array $filters |
| 110 | * |
| 111 | * @return array |
| 112 | * |
| 113 | * @throws \common_exception_Error |
| 114 | * @throws \Exception |
| 115 | */ |
| 116 | private function prepareFilters(array $filters = []) |
| 117 | { |
| 118 | /** @var \common_session_Session $session */ |
| 119 | $session = common_session_SessionManager::getSession(); |
| 120 | $timeZone = new DateTimeZone($session->getTimeZone()); |
| 121 | $utc = new DateTimeZone('UTC'); |
| 122 | |
| 123 | $result = []; |
| 124 | |
| 125 | foreach ($filters as $name => $value) { |
| 126 | if (!empty($value)) { |
| 127 | switch ($name) { |
| 128 | case 'from': |
| 129 | $from = new DateTimeImmutable($filters['from'], $timeZone); |
| 130 | $result[] = ['occurred', '>', $from->setTimezone($utc)->format(DateTime::ISO8601)]; |
| 131 | break; |
| 132 | case 'to': |
| 133 | $to = new DateTimeImmutable($filters['to'], $timeZone); |
| 134 | $result[] = ['occurred', '<=', $to->setTimezone($utc)->format(DateTime::ISO8601)]; |
| 135 | break; |
| 136 | default: |
| 137 | $result[] = [$name, 'LIKE', "%$value%"]; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return $result; |
| 143 | } |
| 144 | } |