Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 36 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ControllerHelper | |
0.00% |
0 / 36 |
|
0.00% |
0 / 3 |
72 | |
0.00% |
0 / 1 |
| getControllers | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| getActions | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| getRequiredRights | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
| 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; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\helpers; |
| 24 | |
| 25 | use oat\oatbox\service\ServiceManager; |
| 26 | use oat\tao\model\controllerMap\Factory; |
| 27 | |
| 28 | /** |
| 29 | * Utility class that focuses on he controllers. |
| 30 | * |
| 31 | * @author Joel Bout <joel@taotesting.com> |
| 32 | * @package tao |
| 33 | */ |
| 34 | class ControllerHelper |
| 35 | { |
| 36 | public const EXTENSION_PREFIX = 'controllerMap_e_'; |
| 37 | public const CONTROLLER_PREFIX = 'controllerMap_c_'; |
| 38 | public const ACTION_PREFIX = 'controllerMap_a_'; |
| 39 | |
| 40 | /** |
| 41 | * Returns al lthe controllers of an extension |
| 42 | * |
| 43 | * @param string $extensionId |
| 44 | * @return array |
| 45 | */ |
| 46 | public static function getControllers($extensionId) |
| 47 | { |
| 48 | try { |
| 49 | $controllerClasses = ServiceManager::getServiceManager() |
| 50 | ->get('generis/cache') |
| 51 | ->get(self::EXTENSION_PREFIX . $extensionId); |
| 52 | } catch (\common_cache_NotFoundException $e) { |
| 53 | $factory = new Factory(); |
| 54 | $controllerClasses = []; |
| 55 | foreach ($factory->getControllers($extensionId) as $controller) { |
| 56 | $controllerClasses[] = $controller->getClassName(); |
| 57 | } |
| 58 | ServiceManager::getServiceManager() |
| 59 | ->get('generis/cache') |
| 60 | ->put($controllerClasses, self::EXTENSION_PREFIX . $extensionId); |
| 61 | } |
| 62 | return $controllerClasses; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Get the list of actions for a controller |
| 67 | * |
| 68 | * @param string $controllerClassName |
| 69 | * @return array |
| 70 | */ |
| 71 | public static function getActions($controllerClassName) |
| 72 | { |
| 73 | try { |
| 74 | $actions = ServiceManager::getServiceManager() |
| 75 | ->get('generis/cache') |
| 76 | ->get(self::CONTROLLER_PREFIX . $controllerClassName); |
| 77 | } catch (\common_cache_NotFoundException $e) { |
| 78 | $factory = new Factory(); |
| 79 | $desc = $factory->getControllerDescription($controllerClassName); |
| 80 | |
| 81 | $actions = []; |
| 82 | foreach ($desc->getActions() as $action) { |
| 83 | $actions[] = $action->getName(); |
| 84 | } |
| 85 | ServiceManager::getServiceManager() |
| 86 | ->get('generis/cache') |
| 87 | ->put($actions, self::CONTROLLER_PREFIX . $controllerClassName); |
| 88 | } |
| 89 | return $actions; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the required rights for the execution of an action |
| 94 | * |
| 95 | * Returns an associative array with the parameter as key |
| 96 | * and the rights as values |
| 97 | * |
| 98 | * @param string $controllerClassName |
| 99 | * @param string $actionName |
| 100 | * @return array |
| 101 | */ |
| 102 | public static function getRequiredRights($controllerClassName, $actionName) |
| 103 | { |
| 104 | try { |
| 105 | $rights = ServiceManager::getServiceManager() |
| 106 | ->get('generis/cache') |
| 107 | ->get(self::ACTION_PREFIX . $controllerClassName . '@' . $actionName); |
| 108 | } catch (\common_cache_NotFoundException $e) { |
| 109 | $factory = new Factory(); |
| 110 | $controller = $factory->getActionDescription($controllerClassName, $actionName); |
| 111 | $rights = $controller->getRequiredRights(); |
| 112 | ServiceManager::getServiceManager() |
| 113 | ->get('generis/cache') |
| 114 | ->put($rights, self::ACTION_PREFIX . $controllerClassName . '@' . $actionName); |
| 115 | } |
| 116 | return $rights; |
| 117 | } |
| 118 | } |