Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.34% |
50 / 53 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
| PreviewerRegistryService | |
94.34% |
50 / 53 |
|
66.67% |
6 / 9 |
22.09 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getRegistry | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| setRegistry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAdapters | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| registerAdapter | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
| unregisterAdapter | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| getPlugins | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| registerPlugin | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
| unregisterPlugin | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
3 | |||
| 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\preview; |
| 24 | |
| 25 | use oat\oatbox\AbstractRegistry; |
| 26 | use oat\tao\model\modules\DynamicModule; |
| 27 | use oat\tao\model\ClientLibConfigRegistry; |
| 28 | use oat\tao\model\service\InjectionAwareService; |
| 29 | |
| 30 | /** |
| 31 | * Class PreviewerRegistryService |
| 32 | * |
| 33 | * @package oat\tao\model\preview |
| 34 | */ |
| 35 | class PreviewerRegistryService extends InjectionAwareService implements PreviewerRegistryServiceInterface |
| 36 | { |
| 37 | private const PREVIEWERS_KEY = 'previewers'; |
| 38 | private const PLUGINS_KEY = 'plugins'; |
| 39 | |
| 40 | /** @var string */ |
| 41 | private $registryEntryKey; |
| 42 | |
| 43 | /** @var AbstractRegistry */ |
| 44 | private $registry; |
| 45 | |
| 46 | /** |
| 47 | * PreviewerRegistryService constructor. |
| 48 | * |
| 49 | * @param string $registryEntryKey |
| 50 | */ |
| 51 | public function __construct(string $registryEntryKey) |
| 52 | { |
| 53 | parent::__construct(); |
| 54 | |
| 55 | $this->registryEntryKey = $registryEntryKey; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @return AbstractRegistry |
| 60 | */ |
| 61 | public function getRegistry(): AbstractRegistry |
| 62 | { |
| 63 | if (!isset($this->registry)) { |
| 64 | $this->registry = ClientLibConfigRegistry::getRegistry(); |
| 65 | } |
| 66 | |
| 67 | return $this->registry; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param AbstractRegistry $registry |
| 72 | */ |
| 73 | public function setRegistry(AbstractRegistry $registry): void |
| 74 | { |
| 75 | $this->registry = $registry; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return array |
| 80 | */ |
| 81 | public function getAdapters(): array |
| 82 | { |
| 83 | $registry = $this->getRegistry(); |
| 84 | |
| 85 | if ($registry->isRegistered($this->registryEntryKey)) { |
| 86 | $config = $registry->get($this->registryEntryKey); |
| 87 | } |
| 88 | |
| 89 | return $config[self::PREVIEWERS_KEY] ?? []; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param DynamicModule $module |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | public function registerAdapter(DynamicModule $module): bool |
| 98 | { |
| 99 | if ($module === null || empty($module->getModule())) { |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | $registry = $this->getRegistry(); |
| 104 | |
| 105 | if ($registry->isRegistered($this->registryEntryKey)) { |
| 106 | $config = $registry->get($this->registryEntryKey); |
| 107 | } |
| 108 | |
| 109 | $config[self::PREVIEWERS_KEY][$module->getModule()] = $module->toArray(); |
| 110 | $registry->set($this->registryEntryKey, $config); |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * @param string $moduleId |
| 117 | * |
| 118 | * @return bool |
| 119 | */ |
| 120 | public function unregisterAdapter(string $moduleId): bool |
| 121 | { |
| 122 | $registry = $this->getRegistry(); |
| 123 | |
| 124 | if ($registry->isRegistered($this->registryEntryKey)) { |
| 125 | $config = $registry->get($this->registryEntryKey); |
| 126 | } |
| 127 | |
| 128 | if (isset($config[self::PREVIEWERS_KEY][$moduleId])) { |
| 129 | unset($config[self::PREVIEWERS_KEY][$moduleId]); |
| 130 | $registry->set($this->registryEntryKey, $config); |
| 131 | |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return array |
| 140 | */ |
| 141 | public function getPlugins(): array |
| 142 | { |
| 143 | $registry = $this->getRegistry(); |
| 144 | |
| 145 | if ($registry->isRegistered($this->registryEntryKey)) { |
| 146 | $config = $registry->get($this->registryEntryKey); |
| 147 | } |
| 148 | |
| 149 | return $config[self::PLUGINS_KEY] ?? []; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * @param DynamicModule $module |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | public function registerPlugin(DynamicModule $module): bool |
| 158 | { |
| 159 | if ($module === null || empty($module->getModule())) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | $this->unregisterPlugin($module->getModule()); |
| 164 | |
| 165 | $registry = $this->getRegistry(); |
| 166 | |
| 167 | if ($registry->isRegistered($this->registryEntryKey)) { |
| 168 | $config = $registry->get($this->registryEntryKey); |
| 169 | } |
| 170 | |
| 171 | $config[self::PLUGINS_KEY][] = $module->toArray(); |
| 172 | $registry->set($this->registryEntryKey, $config); |
| 173 | |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @param string $module |
| 179 | * |
| 180 | * @return bool |
| 181 | */ |
| 182 | public function unregisterPlugin(string $module): bool |
| 183 | { |
| 184 | $registry = $this->getRegistry(); |
| 185 | |
| 186 | if ($registry->isRegistered($this->registryEntryKey)) { |
| 187 | $config = $registry->get($this->registryEntryKey); |
| 188 | } |
| 189 | |
| 190 | $result = false; |
| 191 | |
| 192 | if (isset($config[self::PLUGINS_KEY])) { |
| 193 | $config[self::PLUGINS_KEY] = array_filter( |
| 194 | $config[self::PLUGINS_KEY], |
| 195 | static function (array $plugin) use ($module, &$result): bool { |
| 196 | $result = $plugin['module'] === $module; |
| 197 | |
| 198 | return !$result; |
| 199 | } |
| 200 | ); |
| 201 | $registry->set($this->registryEntryKey, $config); |
| 202 | } |
| 203 | |
| 204 | return $result; |
| 205 | } |
| 206 | } |