* @package tao */ class tao_models_classes_service_StateStorage extends ConfigurableService implements StateStorage { /** * @var string name of former hardcoded persistence * @deprecated */ const PERSISTENCE_ID = 'serviceState'; const KEY_NAMESPACE = 'tao:state:'; const OPTION_PERSISTENCE = 'persistence'; /** * @deprecated */ public static function singleton() { return ServiceManager::getServiceManager()->getServiceManager()->get(StateStorage::SERVICE_ID); } /** * Persistence to store service states to * * @var common_persistence_KeyValuePersistence */ private $persistence; /** * protected constructor to ensure singleton pattern */ protected function getPersistence() { if (is_null($this->persistence)) { $this->persistence = common_persistence_KeyValuePersistence::getPersistence($this->getOption(self::OPTION_PERSISTENCE)); } return $this->persistence; } /** * Store the state of the service call * * @param string $userId * @param string $callId * @param string $data * @return boolean */ public function set($userId, $callId, $data) { $key = $this->getSerial($userId, $callId); return $this->getPersistence()->set($key, $data); } /** * Retore the state of the service call * Returns null if no state is found * * @param string $userId * @param string $callId * @return string */ public function get($userId, $callId) { $key = $this->getSerial($userId, $callId); $returnValue = $this->getPersistence()->get($key); if ($returnValue === false && !$this->has($userId, $callId)) { $returnValue = null; } return $returnValue; } /** * Whenever or not a state for this service call exists * * @param string $userId * @param string $callId * @return boolean */ public function has($userId, $callId) { $key = $this->getSerial($userId, $callId); return $this->getPersistence()->exists($key); } /** * Remove the state for this service call * * @param string $userId * @param string $callId * @return boolean */ public function del($userId, $callId) { $key = $this->getSerial($userId, $callId); return $this->getPersistence()->del($key); } /** * Generate a storage key using the provide user and serial * * @param string $userId * @param string $callId * @return string */ private function getSerial($userId, $callId) { if (is_object($userId)) { common_Logger::w('Object as userid: ' . get_class($userId)); if ($userId instanceof core_kernel_classes_Resource) { $userId = $userId->getUri(); } } return self::KEY_NAMESPACE . $userId . '_' . $callId; ; } }