Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WebsourceManager | |
0.00% |
0 / 25 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getWebsource | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| addWebsource | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| removeWebsource | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; under version 2 |
| 8 | * of the License (non-upgradable). |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | * Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 20 | * |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | namespace oat\tao\model\websource; |
| 25 | |
| 26 | use common_ext_ExtensionsManager; |
| 27 | use common_Exception; |
| 28 | use oat\oatbox\service\ConfigurableService; |
| 29 | use oat\oatbox\service\ServiceManager; |
| 30 | |
| 31 | /** |
| 32 | * Websource manager |
| 33 | * |
| 34 | * @access public |
| 35 | * @author Joel Bout, <joel@taotesting.com> |
| 36 | * @package tao |
| 37 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 38 | */ |
| 39 | class WebsourceManager extends ConfigurableService |
| 40 | { |
| 41 | public const CONFIG_PREFIX = 'websource_'; |
| 42 | |
| 43 | /** |
| 44 | * @return ConfigurableService |
| 45 | * @deprecated Use ServiceLocator::get(WebsourceManager::class) instead |
| 46 | */ |
| 47 | public static function singleton() |
| 48 | { |
| 49 | return ServiceManager::getServiceManager()->get(WebsourceManager::class); |
| 50 | } |
| 51 | |
| 52 | private $websources = []; |
| 53 | |
| 54 | public function getWebsource($key) |
| 55 | { |
| 56 | if (!isset($this->websources[$key])) { |
| 57 | $ext = common_ext_ExtensionsManager::singleton()->getExtensionById('tao'); |
| 58 | $conf = $ext->getConfig(self::CONFIG_PREFIX . $key); |
| 59 | if (!is_array($conf) || !isset($conf['className'])) { |
| 60 | throw new WebsourceNotFound('Undefined websource "' . $key . '"'); |
| 61 | } |
| 62 | $className = $conf['className']; |
| 63 | $options = isset($conf['options']) ? $conf['options'] : []; |
| 64 | $this->websources[$key] = new $className($options); |
| 65 | } |
| 66 | return $this->websources[$key]; |
| 67 | } |
| 68 | |
| 69 | public function addWebsource($websource) |
| 70 | { |
| 71 | $key = $websource->getId(); |
| 72 | if (is_null($key)) { |
| 73 | throw new common_Exception('Missing identifier for websource'); |
| 74 | } |
| 75 | $this->websources[$websource->getId()] = $websource; |
| 76 | $conf = [ |
| 77 | 'className' => get_class($websource), |
| 78 | 'options' => $websource->getOptions() |
| 79 | ]; |
| 80 | $ext = common_ext_ExtensionsManager::singleton()->getExtensionById('tao'); |
| 81 | $ext->setConfig(self::CONFIG_PREFIX . $key, $conf); |
| 82 | } |
| 83 | |
| 84 | public function removeWebsource($websource) |
| 85 | { |
| 86 | if (!isset($this->websources[$websource->getId()])) { |
| 87 | throw new common_Exception('Attempting to remove inexistent ' . $websource->getId()); |
| 88 | } |
| 89 | unset($this->websources[$websource->getId()]); |
| 90 | $ext = common_ext_ExtensionsManager::singleton()->getExtensionById('tao'); |
| 91 | $conf = $ext->unsetConfig(self::CONFIG_PREFIX . $websource->getId()); |
| 92 | } |
| 93 | } |