Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| core_kernel_users_Cache | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| retrieveIncludedRoles | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
| cacheIncludedRoles | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| removeIncludedRoles | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| areIncludedRolesInCache | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| buildIncludedRolesSerial | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| flush | |
0.00% |
0 / 1 |
|
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | use oat\oatbox\service\ServiceManager; |
| 28 | use oat\oatbox\cache\SimpleCache; |
| 29 | |
| 30 | /** |
| 31 | * A facade aiming at helping client code to put User data in the Cache memory. |
| 32 | * |
| 33 | * @access public |
| 34 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 35 | * @package generis |
| 36 | |
| 37 | */ |
| 38 | class core_kernel_users_Cache |
| 39 | { |
| 40 | // --- ASSOCIATIONS --- |
| 41 | |
| 42 | |
| 43 | // --- ATTRIBUTES --- |
| 44 | |
| 45 | /** |
| 46 | * Short description of attribute SERIAL_PREFIX_INCLUDED_ROLES |
| 47 | * |
| 48 | * @access public |
| 49 | * @var string |
| 50 | */ |
| 51 | public const SERIAL_PREFIX_INCLUDED_ROLES = 'roles-ir'; |
| 52 | // --- OPERATIONS --- |
| 53 | |
| 54 | /** |
| 55 | * Retrieve roles included in a given Generis Role from the Cache memory. |
| 56 | * |
| 57 | * @access public |
| 58 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 59 | * |
| 60 | * @param core_kernel_classes_Resource $role A Generis Role Resource. |
| 61 | * |
| 62 | * @return array |
| 63 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 64 | * @throws common_Exception |
| 65 | * @throws core_kernel_users_CacheException |
| 66 | */ |
| 67 | public static function retrieveIncludedRoles(core_kernel_classes_Resource $role) |
| 68 | { |
| 69 | $returnValue = []; |
| 70 | $serial = self::buildIncludedRolesSerial($role); |
| 71 | $fromCache = ServiceManager::getServiceManager()->get(SimpleCache::SERVICE_ID)->get($serial); |
| 72 | // array of URIs. |
| 73 | if (is_null($fromCache)) { |
| 74 | $roleUri = $role->getUri(); |
| 75 | $msg = "Includes roles related to Role with URI '${roleUri}' is not in the Cache memory."; |
| 76 | throw new core_kernel_users_CacheException($msg); |
| 77 | } |
| 78 | |
| 79 | foreach ($fromCache as $uri) { |
| 80 | $returnValue[$uri] = new core_kernel_classes_Resource($uri); |
| 81 | } |
| 82 | |
| 83 | return (array) $returnValue; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Put roles included in a Generis Role in the Cache memory. |
| 88 | * |
| 89 | * @access public |
| 90 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 91 | * |
| 92 | * @param core_kernel_classes_Resource $role |
| 93 | * @param array $includedRoles |
| 94 | * |
| 95 | * @return bool |
| 96 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 97 | * @throws common_Exception |
| 98 | * @throws core_kernel_users_CacheException |
| 99 | */ |
| 100 | public static function cacheIncludedRoles(core_kernel_classes_Resource $role, $includedRoles) |
| 101 | { |
| 102 | // Make a simple array of URIs with the included roles. |
| 103 | $toCache = []; |
| 104 | foreach ($includedRoles as $resource) { |
| 105 | $toCache[] = $resource->getUri(); |
| 106 | } |
| 107 | |
| 108 | $serial = self::buildIncludedRolesSerial($role); |
| 109 | /** @var SimpleCache $cache */ |
| 110 | $cache = ServiceManager::getServiceManager()->get(SimpleCache::SERVICE_ID); |
| 111 | try { |
| 112 | $cache->set($serial, $toCache); |
| 113 | $returnValue = true; |
| 114 | } catch (common_Exception $e) { |
| 115 | $roleUri = $role->getUri(); |
| 116 | $msg = "An error occurred while writing included roles in the cache memory for Role '${roleUri}': "; |
| 117 | $msg .= $e->getMessage(); |
| 118 | throw new core_kernel_users_CacheException($msg); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | return (bool) $returnValue; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Remove roles included in a Generis Role from the Cache memory. |
| 127 | * |
| 128 | * @access public |
| 129 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 130 | * @param core_kernel_classes_Resource $role A Generis Role as a Resource. |
| 131 | * @return boolean |
| 132 | */ |
| 133 | public static function removeIncludedRoles(core_kernel_classes_Resource $role) |
| 134 | { |
| 135 | $serial = self::buildIncludedRolesSerial($role); |
| 136 | ServiceManager::getServiceManager()->get('generis/cache')->remove($serial); |
| 137 | ; |
| 138 | |
| 139 | // -- note: the cache might exist even if it was successfully |
| 140 | // removed due to race conditions. |
| 141 | // $returnValue = (file_exists(GENERIS_CACHE_PATH . $serial)) ? false : true; |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns true if the roles included in a given Generis Role are in the |
| 147 | * memory. |
| 148 | * |
| 149 | * @access public |
| 150 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 151 | * @param core_kernel_classes_Resource $role The Generis Role you want to check if its included roles are in the |
| 152 | * cache memory. |
| 153 | * @return boolean |
| 154 | */ |
| 155 | public static function areIncludedRolesInCache(core_kernel_classes_Resource $role) |
| 156 | { |
| 157 | |
| 158 | $serial = self::buildIncludedRolesSerial($role); |
| 159 | return ServiceManager::getServiceManager()->get('generis/cache')->has($serial); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Build a serial aiming at identifying the includes roles of a given |
| 164 | * Role in the Cache memory. |
| 165 | * |
| 166 | * @access private |
| 167 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 168 | * @param core_kernel_classes_Resource $role The role you want to create a serial for. |
| 169 | * @return string |
| 170 | */ |
| 171 | private static function buildIncludedRolesSerial(core_kernel_classes_Resource $role) |
| 172 | { |
| 173 | return self::SERIAL_PREFIX_INCLUDED_ROLES . urlencode($role->getUri()); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Removes all entries related to included roles from the Cache memory. |
| 178 | * |
| 179 | * @access public |
| 180 | * @author Jerome Bogaerts, <jerome.bogaerts@tudor.lu> |
| 181 | * @return void |
| 182 | */ |
| 183 | public static function flush() |
| 184 | { |
| 185 | ServiceManager::getServiceManager()->get('generis/cache')->purge(); |
| 186 | } |
| 187 | } |