Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.74% |
54 / 57 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| ItemPreviewerService | |
94.74% |
54 / 57 |
|
62.50% |
5 / 8 |
22.07 | |
0.00% |
0 / 1 |
| setRegistry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getRegistry | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| getAdapters | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getPlugins | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| registerAdapter | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
4.02 | |||
| unregisterAdapter | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| registerPlugin | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
| unregisterPlugin | |
100.00% |
15 / 15 |
|
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) 2018 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoItems\model\preview; |
| 22 | |
| 23 | use oat\oatbox\AbstractRegistry; |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\tao\model\ClientLibConfigRegistry; |
| 26 | use oat\tao\model\modules\DynamicModule; |
| 27 | |
| 28 | /** |
| 29 | * Manage item previewers |
| 30 | * |
| 31 | * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com> |
| 32 | * |
| 33 | * @deprecated Use oat\taoItems\model\preview\ItemPreviewerRegistryServiceInterface instead |
| 34 | */ |
| 35 | class ItemPreviewerService extends ConfigurableService |
| 36 | { |
| 37 | public const SERVICE_ID = 'taoItems/ItemPreviewer'; |
| 38 | private const REGISTRY_ENTRY_KEY = 'taoItems/previewer/factory'; |
| 39 | private const PREVIEWERS_KEY = 'previewers'; |
| 40 | private const PLUGINS_KEY = 'plugins'; |
| 41 | |
| 42 | private $registry; |
| 43 | |
| 44 | /** |
| 45 | * Sets the registry that contains the list of adapters |
| 46 | * @param AbstractRegistry $registry |
| 47 | */ |
| 48 | public function setRegistry(AbstractRegistry $registry) |
| 49 | { |
| 50 | $this->registry = $registry; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Gets the registry that contains the list of adapters |
| 55 | * @return AbstractRegistry |
| 56 | */ |
| 57 | public function getRegistry() |
| 58 | { |
| 59 | if (!$this->registry) { |
| 60 | $this->registry = ClientLibConfigRegistry::getRegistry(); |
| 61 | } |
| 62 | return $this->registry; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Gets the list of adapters |
| 67 | * @return array |
| 68 | */ |
| 69 | public function getAdapters() |
| 70 | { |
| 71 | $registry = $this->getRegistry(); |
| 72 | $config = []; |
| 73 | if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { |
| 74 | $config = $registry->get(self::REGISTRY_ENTRY_KEY); |
| 75 | } |
| 76 | |
| 77 | return $config[self::PREVIEWERS_KEY] ?? []; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Gets the list of plugins |
| 82 | * @return array |
| 83 | */ |
| 84 | public function getPlugins() |
| 85 | { |
| 86 | $registry = $this->getRegistry(); |
| 87 | $config = []; |
| 88 | if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { |
| 89 | $config = $registry->get(self::REGISTRY_ENTRY_KEY); |
| 90 | } |
| 91 | |
| 92 | return $config[self::PLUGINS_KEY] ?? []; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Registers a previewer adapter |
| 97 | * @param DynamicModule $module the adapter to register |
| 98 | * @return boolean true if registered |
| 99 | */ |
| 100 | public function registerAdapter(DynamicModule $module): bool |
| 101 | { |
| 102 | if (null === $module || empty($module->getModule())) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | $registry = $this->getRegistry(); |
| 107 | $config = []; |
| 108 | if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { |
| 109 | $config = $registry->get(self::REGISTRY_ENTRY_KEY); |
| 110 | } |
| 111 | |
| 112 | $config[self::PREVIEWERS_KEY][$module->getModule()] = $module->toArray(); |
| 113 | $registry->set(self::REGISTRY_ENTRY_KEY, $config); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Unregisters a previewer adapter |
| 119 | * @param string $moduleId |
| 120 | * @return boolean true if unregistered |
| 121 | */ |
| 122 | public function unregisterAdapter($moduleId): bool |
| 123 | { |
| 124 | $registry = $this->getRegistry(); |
| 125 | $config = []; |
| 126 | if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { |
| 127 | $config = $registry->get(self::REGISTRY_ENTRY_KEY); |
| 128 | } |
| 129 | |
| 130 | if (isset($config[self::PREVIEWERS_KEY]) && isset($config[self::PREVIEWERS_KEY][$moduleId])) { |
| 131 | unset($config[self::PREVIEWERS_KEY][$moduleId]); |
| 132 | $registry->set(self::REGISTRY_ENTRY_KEY, $config); |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Registers a previewer plugin |
| 140 | * @param DynamicModule $module the plugin to register |
| 141 | * @return boolean true if registered |
| 142 | */ |
| 143 | public function registerPlugin(DynamicModule $module): bool |
| 144 | { |
| 145 | if (null === $module || empty($module->getModule())) { |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | $this->unregisterPlugin($module->getModule()); |
| 150 | |
| 151 | $registry = $this->getRegistry(); |
| 152 | $config = []; |
| 153 | if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { |
| 154 | $config = $registry->get(self::REGISTRY_ENTRY_KEY); |
| 155 | } |
| 156 | |
| 157 | $config[self::PLUGINS_KEY][] = $module->toArray(); |
| 158 | $registry->set(self::REGISTRY_ENTRY_KEY, $config); |
| 159 | return true; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Unregisters a previewer plugin |
| 164 | * @param string $moduleId |
| 165 | * @return boolean true if unregistered |
| 166 | */ |
| 167 | public function unregisterPlugin($moduleId): bool |
| 168 | { |
| 169 | $registry = $this->getRegistry(); |
| 170 | $config = []; |
| 171 | if ($registry->isRegistered(self::REGISTRY_ENTRY_KEY)) { |
| 172 | $config = $registry->get(self::REGISTRY_ENTRY_KEY); |
| 173 | } |
| 174 | |
| 175 | $result = false; |
| 176 | if (isset($config[self::PLUGINS_KEY])) { |
| 177 | $config[self::PLUGINS_KEY] = array_filter( |
| 178 | $config[self::PLUGINS_KEY], |
| 179 | static function (array $plugin) use ($moduleId, &$result): bool { |
| 180 | $result = $plugin['module'] == $moduleId; |
| 181 | return !$result; |
| 182 | } |
| 183 | ); |
| 184 | $registry->set(self::REGISTRY_ENTRY_KEY, $config); |
| 185 | } |
| 186 | return $result; |
| 187 | } |
| 188 | } |