Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
24.07% |
13 / 54 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| EntryPointService | |
24.07% |
13 / 54 |
|
0.00% |
0 / 8 |
298.56 | |
0.00% |
0 / 1 |
| overrideEntryPoint | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| activateEntryPoint | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| deactivateEntryPoint | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
20 | |||
| addEntryPoint | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| removeEntryPoint | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
8.02 | |||
| getEntryPoints | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getRegistry | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| registerEntryPoint | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\entryPoint; |
| 23 | |
| 24 | use oat\oatbox\service\ConfigurableService; |
| 25 | use oat\oatbox\service\ServiceManager; |
| 26 | |
| 27 | /** |
| 28 | * |
| 29 | * Registry to store client library paths that will be provide to requireJs |
| 30 | * |
| 31 | * @author Lionel Lecaque, lionel@taotesting.com |
| 32 | */ |
| 33 | class EntryPointService extends ConfigurableService |
| 34 | { |
| 35 | public const SERVICE_ID = 'tao/entrypoint'; |
| 36 | |
| 37 | public const OPTION_ENTRYPOINTS = 'existing'; |
| 38 | |
| 39 | public const OPTION_PRELOGIN = 'prelogin'; |
| 40 | |
| 41 | public const OPTION_POSTLOGIN = 'postlogin'; |
| 42 | |
| 43 | /** |
| 44 | * Replace the entrypoint with the id provided |
| 45 | * |
| 46 | * @param string $id |
| 47 | * @param Entrypoint $e |
| 48 | */ |
| 49 | public function overrideEntryPoint($id, Entrypoint $e) |
| 50 | { |
| 51 | $entryPoints = $this->getOption(self::OPTION_ENTRYPOINTS); |
| 52 | $entryPoints[$id] = $e; |
| 53 | $this->setOption(self::OPTION_ENTRYPOINTS, $entryPoints); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Activate an existing entry point for a specific target |
| 58 | * |
| 59 | * @param string $entryId |
| 60 | * @param string $target |
| 61 | * @throws \common_exception_InconsistentData |
| 62 | * @return boolean success |
| 63 | */ |
| 64 | public function activateEntryPoint($entryId, $target) |
| 65 | { |
| 66 | $success = false; |
| 67 | $entryPoints = $this->getOption(self::OPTION_ENTRYPOINTS); |
| 68 | if (!isset($entryPoints[$entryId])) { |
| 69 | throw new \common_exception_InconsistentData('Unknown entrypoint ' . $entryId); |
| 70 | } |
| 71 | $actives = $this->hasOption($target) ? $this->getOption($target) : []; |
| 72 | if (!in_array($entryId, $actives)) { |
| 73 | $actives[] = $entryId; |
| 74 | $this->setOption($target, $actives); |
| 75 | $success = true; |
| 76 | } |
| 77 | |
| 78 | return $success; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Dectivate an existing entry point for a specific target |
| 83 | * |
| 84 | * @param string $entryId |
| 85 | * @param string $target |
| 86 | * @throws \common_exception_InconsistentData |
| 87 | * @return boolean success |
| 88 | */ |
| 89 | public function deactivateEntryPoint($entryId, $target = self::OPTION_POSTLOGIN) |
| 90 | { |
| 91 | $success = false; |
| 92 | $entryPoints = $this->getOption(self::OPTION_ENTRYPOINTS); |
| 93 | if (!isset($entryPoints[$entryId])) { |
| 94 | throw new \common_exception_InconsistentData('Unknown entrypoint ' . $entryId); |
| 95 | } |
| 96 | $actives = $this->hasOption($target) ? $this->getOption($target) : []; |
| 97 | if (in_array($entryId, $actives)) { |
| 98 | $actives = array_diff($actives, [$entryId]); |
| 99 | $this->setOption($target, $actives); |
| 100 | $success = true; |
| 101 | } else { |
| 102 | \common_Logger::w('Tried to desactivate inactive entry point ' . $entryId); |
| 103 | } |
| 104 | return $success; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Add an Entrypoint and activate it if a target is specified |
| 110 | * |
| 111 | * @param Entrypoint $e |
| 112 | * @param string $target |
| 113 | */ |
| 114 | public function addEntryPoint(Entrypoint $e, $target = null) |
| 115 | { |
| 116 | $entryPoints = $this->getOption(self::OPTION_ENTRYPOINTS); |
| 117 | $entryPoints[$e->getId()] = $e; |
| 118 | $this->setOption(self::OPTION_ENTRYPOINTS, $entryPoints); |
| 119 | |
| 120 | if (!is_null($target)) { |
| 121 | $this->activateEntryPoint($e->getId(), $target); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Remove entrypoint |
| 127 | * |
| 128 | * @param $entryId |
| 129 | * @throws \common_exception_InconsistentData |
| 130 | */ |
| 131 | public function removeEntryPoint($entryId) |
| 132 | { |
| 133 | $entryPoints = $this->getOption(self::OPTION_ENTRYPOINTS); |
| 134 | if (!isset($entryPoints[$entryId])) { |
| 135 | throw new \common_exception_InconsistentData('Unknown entrypoint ' . $entryId); |
| 136 | } |
| 137 | |
| 138 | // delete entrypoint from all options entries |
| 139 | $options = $this->getOptions(); |
| 140 | foreach ($options as $section => $option) { |
| 141 | $sectionIsArray = false; |
| 142 | foreach ($option as $key => $val) { |
| 143 | if ($key === $entryId || $val == $entryId) { |
| 144 | unset($options[$section][$key]); |
| 145 | } |
| 146 | |
| 147 | if ($val == $entryId) { |
| 148 | $sectionIsArray = true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if ($sectionIsArray) { |
| 153 | $options[$section] = array_values($options[$section]); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | $this->setOptions($options); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Get all entrypoints for a designated target |
| 162 | * |
| 163 | * @param string $target |
| 164 | * @return Entrypoint[] |
| 165 | */ |
| 166 | public function getEntryPoints($target = self::OPTION_POSTLOGIN) |
| 167 | { |
| 168 | $ids = $this->hasOption($target) ? $this->getOption($target) : []; |
| 169 | $existing = $this->getOption(self::OPTION_ENTRYPOINTS); |
| 170 | |
| 171 | if ($target === self::OPTION_ENTRYPOINTS) { |
| 172 | return $existing; |
| 173 | } |
| 174 | |
| 175 | $entryPoints = []; |
| 176 | foreach ($ids as $id) { |
| 177 | $entryPoints[$id] = $existing[$id]; |
| 178 | } |
| 179 | return $entryPoints; |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Legacy function for backward compatibilitiy |
| 184 | * |
| 185 | * @return EntryPointService |
| 186 | * @deprecated |
| 187 | */ |
| 188 | public static function getRegistry() |
| 189 | { |
| 190 | return ServiceManager::getServiceManager()->get(self::SERVICE_ID); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Legacy function for backward compatibilitiy |
| 195 | * |
| 196 | * @param Entrypoint $e |
| 197 | * @param string $target |
| 198 | * @deprecated |
| 199 | */ |
| 200 | public function registerEntryPoint(Entrypoint $e) |
| 201 | { |
| 202 | $this->addEntryPoint($e, self::OPTION_POSTLOGIN); |
| 203 | $this->getServiceManager()->register(self::SERVICE_ID, $this); |
| 204 | } |
| 205 | } |