Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.75% |
45 / 48 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| NotifyImportService | |
93.75% |
45 / 48 |
|
71.43% |
5 / 7 |
11.03 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| withMaxTries | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withRetryDelay | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| addResource | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| withAliases | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| notify | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| doNotify | |
96.00% |
24 / 25 |
|
0.00% |
0 / 1 |
4 | |||
| 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) 2022 (original work) Open Assessment Technologies SA. |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\tao\model\StatisticalMetadata\Import\Processor; |
| 24 | |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\tao\model\exceptions\UserErrorException; |
| 27 | use oat\tao\model\metadata\compiler\ResourceMetadataCompilerInterface; |
| 28 | use oat\tao\model\Observer\Subject; |
| 29 | use oat\tao\model\StatisticalMetadata\DataStore\Compiler\StatisticalJsonResourceMetadataCompiler; |
| 30 | use oat\tao\model\StatisticalMetadata\Import\Observer\ObserverFactory; |
| 31 | use Psr\Log\LoggerInterface; |
| 32 | use Throwable; |
| 33 | |
| 34 | class NotifyImportService |
| 35 | { |
| 36 | private const DEFAULT_MAX_TRIES = 10; |
| 37 | private const DEFAULT_RETRY_DELAY = 1000000; |
| 38 | |
| 39 | /** @var LoggerInterface */ |
| 40 | private $logger; |
| 41 | |
| 42 | /** @var ResourceMetadataCompilerInterface */ |
| 43 | private $resourceMetadataCompiler; |
| 44 | |
| 45 | /** @var int */ |
| 46 | private $maxTries = self::DEFAULT_MAX_TRIES; |
| 47 | |
| 48 | /** @var int */ |
| 49 | private $retryDelay = self::DEFAULT_RETRY_DELAY; |
| 50 | |
| 51 | /** @var core_kernel_classes_Resource[] */ |
| 52 | private $resources = []; |
| 53 | |
| 54 | /** @var array */ |
| 55 | private $aliases = []; |
| 56 | |
| 57 | /** @var ObserverFactory */ |
| 58 | private $observerFactory; |
| 59 | |
| 60 | public function __construct( |
| 61 | LoggerInterface $logger, |
| 62 | ResourceMetadataCompilerInterface $jsonMetadataCompiler, |
| 63 | ObserverFactory $observerFactory |
| 64 | ) { |
| 65 | $this->logger = $logger; |
| 66 | $this->resourceMetadataCompiler = $jsonMetadataCompiler; |
| 67 | $this->observerFactory = $observerFactory; |
| 68 | } |
| 69 | |
| 70 | public function withMaxTries(int $maxTries): self |
| 71 | { |
| 72 | $this->maxTries = $maxTries; |
| 73 | |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | public function withRetryDelay(int $retryDelay): self |
| 78 | { |
| 79 | $this->retryDelay = $retryDelay; |
| 80 | |
| 81 | return $this; |
| 82 | } |
| 83 | |
| 84 | public function addResource(core_kernel_classes_Resource $resource): self |
| 85 | { |
| 86 | $this->resources[$resource->getUri()] = $resource; |
| 87 | |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | public function withAliases(array $aliases): self |
| 92 | { |
| 93 | $this->aliases = $aliases; |
| 94 | |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @throws UserErrorException |
| 100 | */ |
| 101 | public function notify(): void |
| 102 | { |
| 103 | try { |
| 104 | $this->doNotify(); |
| 105 | } catch (Throwable $exception) { |
| 106 | $this->logger->error( |
| 107 | sprintf( |
| 108 | 'Error while syncing statistical data: "%s"', |
| 109 | $exception->getMessage() |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | throw new UserErrorException( |
| 114 | __('Unable to sync statistical data. Please, contact the system administrator for more details') |
| 115 | ); |
| 116 | } finally { |
| 117 | $this->resources = []; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @throws Throwable |
| 123 | */ |
| 124 | private function doNotify(): void |
| 125 | { |
| 126 | $data = []; |
| 127 | $resourceIds = []; |
| 128 | |
| 129 | if ($this->resourceMetadataCompiler instanceof StatisticalJsonResourceMetadataCompiler) { |
| 130 | $this->resourceMetadataCompiler->withAliases($this->aliases); |
| 131 | } |
| 132 | |
| 133 | foreach ($this->resources as $resource) { |
| 134 | $resourceIds[] = $resource->getUri(); |
| 135 | $data[] = $this->resourceMetadataCompiler->compile($resource); |
| 136 | } |
| 137 | |
| 138 | $tries = 0; |
| 139 | |
| 140 | do { |
| 141 | try { |
| 142 | $tries++; |
| 143 | |
| 144 | $this->observerFactory |
| 145 | ->create() |
| 146 | ->update(new Subject($data)); |
| 147 | |
| 148 | return; |
| 149 | } catch (Throwable $exception) { |
| 150 | usleep($this->retryDelay); |
| 151 | |
| 152 | $this->logger->error( |
| 153 | sprintf( |
| 154 | 'Error (try: %s) while syncing statistical data: "%s", resources: "%s"', |
| 155 | $tries, |
| 156 | $exception->getMessage(), |
| 157 | implode(', ', $resourceIds) |
| 158 | ) |
| 159 | ); |
| 160 | } |
| 161 | } while ($tries < $this->maxTries); |
| 162 | |
| 163 | throw $exception; |
| 164 | } |
| 165 | } |