Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 22 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_service_StateStorage | |
0.00% |
0 / 22 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
| singleton | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPersistence | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| set | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| get | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| has | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| del | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getSerial | |
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | use oat\oatbox\service\ServiceManager; |
| 23 | use oat\oatbox\service\ConfigurableService; |
| 24 | use oat\tao\model\state\StateStorage; |
| 25 | |
| 26 | /** |
| 27 | * Persistence for the item delivery service |
| 28 | * |
| 29 | * @access public |
| 30 | * @author Joel Bout, <joel@taotesting.com> |
| 31 | * @package tao |
| 32 | */ |
| 33 | class tao_models_classes_service_StateStorage extends ConfigurableService implements StateStorage |
| 34 | { |
| 35 | /** |
| 36 | * @var string name of former hardcoded persistence |
| 37 | * @deprecated |
| 38 | */ |
| 39 | public const PERSISTENCE_ID = 'serviceState'; |
| 40 | |
| 41 | public const KEY_NAMESPACE = 'tao:state:'; |
| 42 | |
| 43 | public const OPTION_PERSISTENCE = 'persistence'; |
| 44 | |
| 45 | /** |
| 46 | * @deprecated |
| 47 | */ |
| 48 | public static function singleton() |
| 49 | { |
| 50 | return ServiceManager::getServiceManager()->getServiceManager()->get(StateStorage::SERVICE_ID); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Persistence to store service states to |
| 55 | * |
| 56 | * @var common_persistence_KeyValuePersistence |
| 57 | */ |
| 58 | private $persistence; |
| 59 | |
| 60 | /** |
| 61 | * protected constructor to ensure singleton pattern |
| 62 | */ |
| 63 | protected function getPersistence() |
| 64 | { |
| 65 | if (is_null($this->persistence)) { |
| 66 | $this->persistence = common_persistence_KeyValuePersistence::getPersistence( |
| 67 | $this->getOption(self::OPTION_PERSISTENCE) |
| 68 | ); |
| 69 | } |
| 70 | return $this->persistence; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Store the state of the service call |
| 75 | * |
| 76 | * @param string $userId |
| 77 | * @param string $callId |
| 78 | * @param string $data |
| 79 | * @return boolean |
| 80 | */ |
| 81 | public function set($userId, $callId, $data) |
| 82 | { |
| 83 | $key = $this->getSerial($userId, $callId); |
| 84 | return $this->getPersistence()->set($key, $data); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Retore the state of the service call |
| 89 | * Returns null if no state is found |
| 90 | * |
| 91 | * @param string $userId |
| 92 | * @param string $callId |
| 93 | * @return string |
| 94 | */ |
| 95 | public function get($userId, $callId) |
| 96 | { |
| 97 | $key = $this->getSerial($userId, $callId); |
| 98 | $returnValue = $this->getPersistence()->get($key); |
| 99 | if ($returnValue === false && !$this->has($userId, $callId)) { |
| 100 | $returnValue = null; |
| 101 | } |
| 102 | return $returnValue; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Whenever or not a state for this service call exists |
| 107 | * |
| 108 | * @param string $userId |
| 109 | * @param string $callId |
| 110 | * @return boolean |
| 111 | */ |
| 112 | public function has($userId, $callId) |
| 113 | { |
| 114 | $key = $this->getSerial($userId, $callId); |
| 115 | return $this->getPersistence()->exists($key); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Remove the state for this service call |
| 120 | * |
| 121 | * @param string $userId |
| 122 | * @param string $callId |
| 123 | * @return boolean |
| 124 | */ |
| 125 | public function del($userId, $callId) |
| 126 | { |
| 127 | $key = $this->getSerial($userId, $callId); |
| 128 | return $this->getPersistence()->del($key); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Generate a storage key using the provide user and serial |
| 133 | * |
| 134 | * @param string $userId |
| 135 | * @param string $callId |
| 136 | * @return string |
| 137 | */ |
| 138 | private function getSerial($userId, $callId) |
| 139 | { |
| 140 | if (is_object($userId)) { |
| 141 | common_Logger::w('Object as userid: ' . get_class($userId)); |
| 142 | if ($userId instanceof core_kernel_classes_Resource) { |
| 143 | $userId = $userId->getUri(); |
| 144 | } |
| 145 | } |
| 146 | return self::KEY_NAMESPACE . $userId . '_' . $callId; |
| 147 | ; |
| 148 | } |
| 149 | } |