Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
50.00% |
23 / 46 |
|
16.67% |
1 / 6 |
CRAP | |
0.00% |
0 / 1 |
| AbstractTaskLogBroker | |
50.00% |
23 / 46 |
|
16.67% |
1 / 6 |
30.00 | |
0.00% |
0 / 1 |
| getStatus | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getTableName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| add | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| updateStatus | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| addReport | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getReport | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| search | |
66.67% |
6 / 9 |
|
0.00% |
0 / 1 |
2.15 | |||
| count | |
50.00% |
3 / 6 |
|
0.00% |
0 / 1 |
2.50 | |||
| getSearchQuery | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
2.00 | |||
| getCountQuery | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getQueryBuilder | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getPersistence | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getStats | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| archive | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| cancel | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| archiveCollection | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| cancelCollection | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| deleteById | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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) 2020 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\taskQueue\TaskLog\Broker; |
| 24 | |
| 25 | use common_exception_Error; |
| 26 | use common_persistence_Persistence as Persistence; |
| 27 | use common_report_Report as Report; |
| 28 | use Doctrine\DBAL\Query\QueryBuilder; |
| 29 | use Exception; |
| 30 | use oat\oatbox\log\LoggerAwareTrait; |
| 31 | use oat\oatbox\PhpSerializable; |
| 32 | use oat\oatbox\reporting\Report as NewReport; |
| 33 | use oat\tao\model\taskQueue\Task\TaskInterface; |
| 34 | use oat\tao\model\taskQueue\TaskLog\CollectionInterface; |
| 35 | use oat\tao\model\taskQueue\TaskLog\Entity\EntityInterface; |
| 36 | use oat\tao\model\taskQueue\TaskLog\TaskLogCollection; |
| 37 | use oat\tao\model\taskQueue\TaskLog\TaskLogFilter; |
| 38 | use oat\tao\model\taskQueue\TaskLog\TasksLogsStats; |
| 39 | use Psr\Log\LoggerAwareInterface; |
| 40 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 41 | |
| 42 | abstract class AbstractTaskLogBroker implements |
| 43 | TaskLogBrokerInterface, |
| 44 | PhpSerializable, |
| 45 | LoggerAwareInterface, |
| 46 | RdsTaskLogBrokerInterface |
| 47 | { |
| 48 | use ServiceLocatorAwareTrait; |
| 49 | use LoggerAwareTrait; |
| 50 | |
| 51 | |
| 52 | /** @var string|null */ |
| 53 | protected $containerName; |
| 54 | |
| 55 | /** @var Persistence */ |
| 56 | private $persistence; |
| 57 | |
| 58 | /** |
| 59 | * @inheritdoc |
| 60 | */ |
| 61 | public function getStatus(string $taskId): string |
| 62 | { |
| 63 | $qb = $this->getQueryBuilder() |
| 64 | ->select(self::COLUMN_STATUS) |
| 65 | ->from($this->getTableName()) |
| 66 | ->andWhere(self::COLUMN_ID . ' = :id') |
| 67 | ->setParameter('id', $taskId); |
| 68 | |
| 69 | return (string)$qb->execute()->fetchColumn(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @inheritdoc |
| 74 | */ |
| 75 | abstract public function getTableName(): string; |
| 76 | |
| 77 | /** |
| 78 | * @inheritdoc |
| 79 | */ |
| 80 | abstract public function add(TaskInterface $task, string $status, string $label = null): void; |
| 81 | |
| 82 | /** |
| 83 | * @inheritdoc |
| 84 | */ |
| 85 | abstract public function updateStatus(string $taskId, string $newStatus, string $prevStatus = null): int; |
| 86 | |
| 87 | /** |
| 88 | * @inheritdoc |
| 89 | */ |
| 90 | abstract public function addReport(string $taskId, Report $report, string $newStatus = null): int; |
| 91 | |
| 92 | /** |
| 93 | * @inheritdoc |
| 94 | * @throws common_exception_Error |
| 95 | */ |
| 96 | public function getReport(string $taskId): ?Report |
| 97 | { |
| 98 | $qb = $this->getQueryBuilder() |
| 99 | ->select(self::COLUMN_REPORT) |
| 100 | ->from($this->getTableName()) |
| 101 | ->andWhere(self::COLUMN_ID . ' = :id') |
| 102 | ->setParameter('id', $taskId); |
| 103 | |
| 104 | if ( |
| 105 | ($reportJson = $qb->execute()->fetchColumn()) |
| 106 | && ($reportData = json_decode($reportJson, true)) !== null |
| 107 | && json_last_error() === JSON_ERROR_NONE |
| 108 | ) { |
| 109 | // if we have a valid JSON string and no JSON error, let's restore the report object |
| 110 | return NewReport::jsonUnserialize($reportData); |
| 111 | } |
| 112 | |
| 113 | return null; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * @inheritdoc |
| 118 | */ |
| 119 | public function search(TaskLogFilter $filter): iterable |
| 120 | { |
| 121 | try { |
| 122 | $qb = $this->getSearchQuery($filter); |
| 123 | $collection = TaskLogCollection::createFromArray( |
| 124 | $qb->execute()->fetchAll(), |
| 125 | $this->getPersistence()->getPlatForm()->getDateTimeFormatString() |
| 126 | ); |
| 127 | } catch (Exception $exception) { |
| 128 | $this->logError('Searching for task logs failed with MSG: ' . $exception->getMessage()); |
| 129 | $collection = TaskLogCollection::createEmptyCollection(); |
| 130 | } |
| 131 | |
| 132 | return $collection; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @inheritdoc |
| 137 | */ |
| 138 | public function count(TaskLogFilter $filter): int |
| 139 | { |
| 140 | try { |
| 141 | return (int)$this->getCountQuery($filter) |
| 142 | ->execute() |
| 143 | ->fetchColumn(); |
| 144 | } catch (Exception $e) { |
| 145 | $this->logError('Counting task logs failed with MSG: ' . $e->getMessage()); |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | private function getSearchQuery(TaskLogFilter $filter): QueryBuilder |
| 152 | { |
| 153 | $qb = $this->getQueryBuilder() |
| 154 | ->select($filter->getColumns()) |
| 155 | ->from($this->getTableName()) |
| 156 | ->setMaxResults($filter->getLimit()) |
| 157 | ->setFirstResult($filter->getOffset()); |
| 158 | |
| 159 | if ($filter->getSortBy()) { |
| 160 | $qb->orderBy($filter->getSortBy(), $filter->getSortOrder()); |
| 161 | } else { |
| 162 | $qb->orderBy(TaskLogBrokerInterface::COLUMN_CREATED_AT, 'DESC'); |
| 163 | } |
| 164 | |
| 165 | $filter->applyFilters($qb); |
| 166 | return $qb; |
| 167 | } |
| 168 | |
| 169 | private function getCountQuery(TaskLogFilter $filter): QueryBuilder |
| 170 | { |
| 171 | $qb = $this->getQueryBuilder() |
| 172 | ->select('COUNT(*)') |
| 173 | ->from($this->getTableName()); |
| 174 | |
| 175 | $filter->applyFilters($qb); |
| 176 | return $qb; |
| 177 | } |
| 178 | |
| 179 | abstract protected function getQueryBuilder(): QueryBuilder; |
| 180 | |
| 181 | abstract protected function getPersistence(): Persistence; |
| 182 | |
| 183 | abstract public function getStats(TaskLogFilter $filter): TasksLogsStats; |
| 184 | |
| 185 | /** |
| 186 | * @inheritdoc |
| 187 | */ |
| 188 | abstract public function archive(EntityInterface $entity): bool; |
| 189 | |
| 190 | /** |
| 191 | * @inheritdoc |
| 192 | */ |
| 193 | abstract public function cancel(EntityInterface $entity): bool; |
| 194 | |
| 195 | /** |
| 196 | * @inheritdoc |
| 197 | */ |
| 198 | abstract public function archiveCollection(CollectionInterface $collection): int; |
| 199 | |
| 200 | /** |
| 201 | * @inheritdoc |
| 202 | */ |
| 203 | abstract public function cancelCollection(CollectionInterface $collection): int; |
| 204 | |
| 205 | /** |
| 206 | * @inheritdoc |
| 207 | */ |
| 208 | abstract public function deleteById(string $taskId): bool; |
| 209 | } |