Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
31.82% |
14 / 44 |
|
33.33% |
3 / 9 |
CRAP | |
0.00% |
0 / 1 |
| AbstractModuleService | |
31.82% |
14 / 44 |
|
33.33% |
3 / 9 |
120.70 | |
0.00% |
0 / 1 |
| setRegistry | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getAllModules | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getModule | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
3.14 | |||
| loadModule | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 | |||
| activateModule | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| deactivateModule | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| registerModules | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| registerModulesByCategories | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| 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) 2016-2017 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\modules; |
| 22 | |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | |
| 25 | /** |
| 26 | * Manage module modules. Should be overridden to provide the right ModuleRegistry instance and a SERVICE_ID constant. |
| 27 | * |
| 28 | * @author Bertrand Chevrier <bertrand@taotesting.com> |
| 29 | * @author Jean-Sébastien Conan <jean-sebastien@taotesting.com> |
| 30 | */ |
| 31 | abstract class AbstractModuleService extends ConfigurableService |
| 32 | { |
| 33 | /** |
| 34 | * @var AbstractModuleRegistry |
| 35 | */ |
| 36 | private $registry; |
| 37 | |
| 38 | /** |
| 39 | * Registry setter |
| 40 | * @param AbstractModuleRegistry $registry |
| 41 | */ |
| 42 | public function setRegistry(AbstractModuleRegistry $registry) |
| 43 | { |
| 44 | $this->registry = $registry; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates a module object from data array |
| 49 | * @param $data |
| 50 | * @return DynamicModule |
| 51 | * @throws \common_exception_InconsistentData |
| 52 | */ |
| 53 | protected function createFromArray($data) |
| 54 | { |
| 55 | return DynamicModule::fromArray($data); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Retrieve the list of all available modules (from the registry) |
| 60 | * |
| 61 | * @return DynamicModule[] the available modules |
| 62 | */ |
| 63 | public function getAllModules() |
| 64 | { |
| 65 | $modules = array_map(function ($value) { |
| 66 | return $this->loadModule($value); |
| 67 | }, $this->registry->getMap()); |
| 68 | |
| 69 | return array_filter($modules, function ($module) { |
| 70 | return !is_null($module); |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Retrieve the given module from the registry |
| 76 | * |
| 77 | * @param string $id the identifier of the module to retrieve |
| 78 | * @return DynamicModule|null the module |
| 79 | */ |
| 80 | public function getModule($id) |
| 81 | { |
| 82 | foreach ($this->registry->getMap() as $module) { |
| 83 | if ($module['id'] == $id) { |
| 84 | return $this->loadModule($module); |
| 85 | } |
| 86 | } |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Load a module from the given data |
| 92 | * @param array $data |
| 93 | * @return DynamicModule|null |
| 94 | */ |
| 95 | private function loadModule(array $data) |
| 96 | { |
| 97 | $module = null; |
| 98 | try { |
| 99 | $module = $this->createFromArray($data); |
| 100 | } catch (\common_exception_InconsistentData $dataException) { |
| 101 | \common_Logger::w('Got inconsistent module data, skipping.'); |
| 102 | } |
| 103 | return $module; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Change the state of a module to active |
| 108 | * |
| 109 | * @param DynamicModule $module the module to activate |
| 110 | * @return boolean true if activated |
| 111 | */ |
| 112 | public function activateModule(DynamicModule $module) |
| 113 | { |
| 114 | if (!is_null($module)) { |
| 115 | $module->setActive(true); |
| 116 | return $this->registry->register($module); |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Change the state of a module to inactive |
| 124 | * |
| 125 | * @param DynamicModule $module the module to deactivate |
| 126 | * @return boolean true if deactivated |
| 127 | */ |
| 128 | public function deactivateModule(DynamicModule $module) |
| 129 | { |
| 130 | if (!is_null($module)) { |
| 131 | $module->setActive(false); |
| 132 | return $this->registry->register($module); |
| 133 | } |
| 134 | |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Register a list of modules |
| 140 | * @param array $modules |
| 141 | * @return int The number of registered modules |
| 142 | * @throws \common_exception_InconsistentData |
| 143 | */ |
| 144 | public function registerModules(array $modules) |
| 145 | { |
| 146 | $count = 0; |
| 147 | foreach ($modules as $module) { |
| 148 | if (is_array($module)) { |
| 149 | $module = $this->createFromArray($module); |
| 150 | } |
| 151 | $this->registry->register($module); |
| 152 | $count++; |
| 153 | } |
| 154 | return $count; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Register a list of modules gathered by categories |
| 159 | * @param array $modules |
| 160 | * @return int The number of registered modules |
| 161 | * @throws \common_exception_InconsistentData |
| 162 | * @throws \common_exception_InvalidArgumentType |
| 163 | */ |
| 164 | public function registerModulesByCategories(array $modules) |
| 165 | { |
| 166 | $count = 0; |
| 167 | foreach ($modules as $categoryModules) { |
| 168 | if (is_array($categoryModules)) { |
| 169 | $count += $this->registerModules($categoryModules); |
| 170 | } else { |
| 171 | throw new \common_exception_InvalidArgumentType( |
| 172 | self::class, |
| 173 | __FUNCTION__, |
| 174 | 0, |
| 175 | 'array', |
| 176 | $categoryModules |
| 177 | ); |
| 178 | } |
| 179 | } |
| 180 | return $count; |
| 181 | } |
| 182 | } |