Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| DeliveryThemeDetailsProvider | |
0.00% |
0 / 59 |
|
0.00% |
0 / 11 |
812 | |
0.00% |
0 / 1 |
| getDeliveryExecutionId | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| getThemeId | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| isHeadless | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryIdFromSession | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryThemeId | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryThemeIdFromCache | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getDeliveryThemeIdFromDb | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| storeDeliveryThemeIdToCache | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getCacheKey | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCachePersistence | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| getCacheTtl | |
0.00% |
0 / 5 |
|
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) 2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoDeliveryRdf\model\theme; |
| 23 | |
| 24 | use common_exception_Error; |
| 25 | use common_exception_NotImplemented; |
| 26 | use common_persistence_KeyValuePersistence; |
| 27 | use common_persistence_KvDriver; |
| 28 | use common_Serializable; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use oat\oatbox\service\ConfigurableService; |
| 31 | use oat\tao\model\theme\ThemeDetailsProviderInterface; |
| 32 | use oat\taoDelivery\model\execution\DeliveryExecution; |
| 33 | use PHPSession; |
| 34 | |
| 35 | class DeliveryThemeDetailsProvider extends ConfigurableService implements ThemeDetailsProviderInterface |
| 36 | { |
| 37 | /** |
| 38 | * The delivery theme id uri. |
| 39 | */ |
| 40 | public const DELIVERY_THEME_ID_URI = 'http://www.tao.lu/Ontologies/TAODelivery.rdf#ThemeName'; |
| 41 | |
| 42 | /** |
| 43 | * The default ttl of the cache persistence. |
| 44 | */ |
| 45 | public const CACHE_PERSISTENCE_DEFAULT_TTL = null; |
| 46 | |
| 47 | /** |
| 48 | * The cache persistence config option. |
| 49 | */ |
| 50 | public const OPTION_CACHE_PERSISTENCE = 'cachePersistence'; |
| 51 | |
| 52 | /** |
| 53 | * The cache persistence ttl config option. |
| 54 | */ |
| 55 | public const OPTION_CACHE_PERSISTENCE_TTL = 'cachePersistenceTtl'; |
| 56 | |
| 57 | /** |
| 58 | * @var common_persistence_KvDriver |
| 59 | */ |
| 60 | private $cachePersistence; |
| 61 | |
| 62 | /** |
| 63 | * @var string |
| 64 | */ |
| 65 | private $themeId = ''; |
| 66 | |
| 67 | /** |
| 68 | * Returns the delivery execution identifier. |
| 69 | * |
| 70 | * @return string |
| 71 | */ |
| 72 | protected function getDeliveryExecutionId() |
| 73 | { |
| 74 | $request = \Context::getInstance()->getRequest(); |
| 75 | |
| 76 | $deliveryExecutionId = ''; |
| 77 | if ($request->hasParameter('deliveryExecution')) { |
| 78 | $deliveryExecutionId = \tao_helpers_Uri::decode( |
| 79 | $request->getParameter('deliveryExecution') |
| 80 | ); |
| 81 | } elseif ($request->hasParameter('testServiceCallId')) { |
| 82 | $deliveryExecutionId = \tao_helpers_Uri::decode( |
| 83 | $request->getParameter('testServiceCallId') |
| 84 | ); |
| 85 | } elseif ($request->hasParameter('serviceCallId')) { |
| 86 | $deliveryExecutionId = \tao_helpers_Uri::decode( |
| 87 | $request->getParameter('serviceCallId') |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | return $deliveryExecutionId; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @inheritdoc |
| 96 | */ |
| 97 | public function getThemeId() |
| 98 | { |
| 99 | $deliveryExecutionId = $this->getDeliveryExecutionId(); |
| 100 | |
| 101 | if (empty($this->themeId)) { |
| 102 | if (!empty($deliveryExecutionId)) { |
| 103 | $deliveryId = $this->getDeliveryIdFromSession($deliveryExecutionId); |
| 104 | if ($deliveryId !== false) { |
| 105 | $this->themeId = $this->getDeliveryThemeId($deliveryId); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $this->themeId; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tells if the page has to be headless: without header and footer. |
| 115 | * |
| 116 | * @return bool|mixed |
| 117 | */ |
| 118 | public function isHeadless() |
| 119 | { |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns the deliveryId from session. |
| 125 | * |
| 126 | * @param $deliveryExecutionId |
| 127 | * |
| 128 | * @return mixed |
| 129 | */ |
| 130 | public function getDeliveryIdFromSession($deliveryExecutionId) |
| 131 | { |
| 132 | if (PHPSession::singleton()->hasAttribute(DeliveryExecution::getDeliveryIdSessionKey($deliveryExecutionId))) { |
| 133 | return PHPSession::singleton()->getAttribute( |
| 134 | DeliveryExecution::getDeliveryIdSessionKey($deliveryExecutionId) |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the delivery theme id. |
| 143 | * |
| 144 | * @param $deliveryId |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | public function getDeliveryThemeId($deliveryId) |
| 149 | { |
| 150 | $themeId = $this->getDeliveryThemeIdFromCache($deliveryId); |
| 151 | if ($themeId === false) { |
| 152 | $themeId = $this->getDeliveryThemeIdFromDb($deliveryId); |
| 153 | $this->storeDeliveryThemeIdToCache($deliveryId, $themeId); |
| 154 | } |
| 155 | |
| 156 | return $themeId; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Returns the delivery theme id from cache or FALSE when it does not exist. |
| 161 | * |
| 162 | * @param $deliveryId |
| 163 | * |
| 164 | * @return bool|common_Serializable |
| 165 | */ |
| 166 | public function getDeliveryThemeIdFromCache($deliveryId) |
| 167 | { |
| 168 | if ($this->getCachePersistence() === null) { |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | return $this->getCachePersistence()->get($this->getCacheKey($deliveryId)); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Returns delivery theme id from database. |
| 177 | * |
| 178 | * @param $deliveryId |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public function getDeliveryThemeIdFromDb($deliveryId) |
| 183 | { |
| 184 | try { |
| 185 | $delivery = new core_kernel_classes_Resource($deliveryId); |
| 186 | $property = $delivery->getProperty(static::DELIVERY_THEME_ID_URI); |
| 187 | |
| 188 | return (string)$delivery->getOnePropertyValue($property); |
| 189 | } catch (common_exception_Error $e) { |
| 190 | return ''; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Stores the delivery theme id to cache. |
| 196 | * |
| 197 | * @param $deliveryId |
| 198 | * @param $themeId |
| 199 | * |
| 200 | * @return bool |
| 201 | */ |
| 202 | public function storeDeliveryThemeIdToCache($deliveryId, $themeId) |
| 203 | { |
| 204 | try { |
| 205 | if ($this->getCachePersistence() !== null) { |
| 206 | return $this |
| 207 | ->getCachePersistence() |
| 208 | ->set($this->getCacheKey($deliveryId), $themeId, $this->getCacheTtl()); |
| 209 | } |
| 210 | } catch (common_exception_NotImplemented $e) { |
| 211 | } |
| 212 | |
| 213 | return false; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Returns the cache key. |
| 218 | * |
| 219 | * @param $deliveryId |
| 220 | * |
| 221 | * @return string |
| 222 | */ |
| 223 | public function getCacheKey($deliveryId) |
| 224 | { |
| 225 | return 'deliveryThemeId:' . $deliveryId; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Returns the cache persistence. |
| 230 | * |
| 231 | * @return common_persistence_KvDriver |
| 232 | */ |
| 233 | protected function getCachePersistence() |
| 234 | { |
| 235 | if (is_null($this->cachePersistence) && $this->hasOption(static::OPTION_CACHE_PERSISTENCE)) { |
| 236 | $persistenceOption = $this->getOption(static::OPTION_CACHE_PERSISTENCE); |
| 237 | $this->cachePersistence = (is_object($persistenceOption)) |
| 238 | ? $persistenceOption |
| 239 | : common_persistence_KeyValuePersistence::getPersistence($persistenceOption); |
| 240 | } |
| 241 | |
| 242 | return $this->cachePersistence; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Returns the cache persistence's ttl. |
| 247 | * |
| 248 | * @return int|null |
| 249 | */ |
| 250 | public function getCacheTtl() |
| 251 | { |
| 252 | if ($this->hasOption(static::OPTION_CACHE_PERSISTENCE_TTL)) { |
| 253 | $cacheTtl = $this->getOption(static::OPTION_CACHE_PERSISTENCE_TTL); |
| 254 | if (!is_null($cacheTtl)) { |
| 255 | return $cacheTtl; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return static::CACHE_PERSISTENCE_DEFAULT_TTL; |
| 260 | } |
| 261 | } |