* @package tao */ class StateMigration extends ConfigurableService { const SERVICE_ID = 'tao/migrationState'; const OPTION_FILESYSTEM = 'fileSystem'; /** * @var FileSystem */ private $fileSystem = null; public function __construct(array $options = []) { if (!isset($options[self::OPTION_FILESYSTEM])) { throw new InvalidService(__("missing config %s for the service %s", self::OPTION_FILESYSTEM, self::class)); } parent::__construct($options); } /** * Store the state of the service call * * @param string $userId * @param string $callId * @return boolean */ public function archive($userId, $callId) { /** @var StateStorage $stateStorage */ $stateStorage = $this->getServiceManager()->get(StateStorage::SERVICE_ID); $state = $stateStorage->get($userId, $callId); return (!is_null($state)) ? $this->getFileSystem()->write($this->generateSerial($userId, $callId), $state) : false; } public function restore($userId, $callId) { $state = $this->getFileSystem()->read($this->generateSerial($userId, $callId)); /** @var StateStorage $stateStorage */ $stateStorage = $this->getServiceManager()->get(StateStorage::SERVICE_ID); return $stateStorage->set($userId, $callId, $state); } public function removeState($userId, $callId) { /** @var StateStorage $stateStorage */ $stateStorage = $this->getServiceManager()->get(StateStorage::SERVICE_ID); $stateStorage->del($userId, $callId); } public function removeBackup($userId, $callId) { return $this->getFileSystem()->delete($this->generateSerial($userId, $callId)); } private function generateSerial($userId, $callId) { return md5($userId . $callId); } /** * * @return FileSystem */ private function getFileSystem() { if (is_null($this->fileSystem)) { /** @var FileSystemService $fileSystemService */ $fileSystemService = $this->getServiceManager()->get(FileSystemService::SERVICE_ID); $this->fileSystem = $fileSystemService->getFileSystem($this->getOption(self::OPTION_FILESYSTEM)); } return $this->fileSystem; } }