Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| StateMigration | |
0.00% |
0 / 20 |
|
0.00% |
0 / 7 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| archive | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| restore | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| removeState | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| removeBackup | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateSerial | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileSystem | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 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 | namespace oat\tao\model\state; |
| 23 | |
| 24 | use oat\oatbox\filesystem\FileSystem; |
| 25 | use oat\oatbox\filesystem\FileSystemService; |
| 26 | use oat\oatbox\service\exception\InvalidService; |
| 27 | use oat\oatbox\service\ConfigurableService; |
| 28 | |
| 29 | /** |
| 30 | * Persistence for the item delivery service |
| 31 | * |
| 32 | * @access public |
| 33 | * @author Antoine Robin Bout, <antoine@taotesting.com> |
| 34 | * @package tao |
| 35 | */ |
| 36 | class StateMigration extends ConfigurableService |
| 37 | { |
| 38 | public const SERVICE_ID = 'tao/migrationState'; |
| 39 | |
| 40 | public const OPTION_FILESYSTEM = 'fileSystem'; |
| 41 | |
| 42 | /** |
| 43 | * @var FileSystem |
| 44 | */ |
| 45 | private $fileSystem = null; |
| 46 | |
| 47 | public function __construct(array $options = []) |
| 48 | { |
| 49 | if (!isset($options[self::OPTION_FILESYSTEM])) { |
| 50 | throw new InvalidService(__("missing config %s for the service %s", self::OPTION_FILESYSTEM, self::class)); |
| 51 | } |
| 52 | parent::__construct($options); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Store the state of the service call |
| 57 | * |
| 58 | * @param string $userId |
| 59 | * @param string $callId |
| 60 | * @return boolean |
| 61 | */ |
| 62 | public function archive($userId, $callId) |
| 63 | { |
| 64 | |
| 65 | /** @var StateStorage $stateStorage */ |
| 66 | $stateStorage = $this->getServiceManager()->get(StateStorage::SERVICE_ID); |
| 67 | |
| 68 | $state = $stateStorage->get($userId, $callId); |
| 69 | |
| 70 | return (!is_null($state)) |
| 71 | ? $this->getFileSystem()->write($this->generateSerial($userId, $callId), $state) |
| 72 | : false; |
| 73 | } |
| 74 | |
| 75 | public function restore($userId, $callId) |
| 76 | { |
| 77 | |
| 78 | $state = $this->getFileSystem()->read($this->generateSerial($userId, $callId)); |
| 79 | |
| 80 | /** @var StateStorage $stateStorage */ |
| 81 | $stateStorage = $this->getServiceManager()->get(StateStorage::SERVICE_ID); |
| 82 | |
| 83 | return $stateStorage->set($userId, $callId, $state); |
| 84 | } |
| 85 | |
| 86 | public function removeState($userId, $callId) |
| 87 | { |
| 88 | /** @var StateStorage $stateStorage */ |
| 89 | $stateStorage = $this->getServiceManager()->get(StateStorage::SERVICE_ID); |
| 90 | |
| 91 | $stateStorage->del($userId, $callId); |
| 92 | } |
| 93 | |
| 94 | public function removeBackup($userId, $callId) |
| 95 | { |
| 96 | $this->getFileSystem()->delete($this->generateSerial($userId, $callId)); |
| 97 | } |
| 98 | |
| 99 | private function generateSerial($userId, $callId) |
| 100 | { |
| 101 | return md5($userId . $callId); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * |
| 106 | * @return FileSystem |
| 107 | */ |
| 108 | private function getFileSystem() |
| 109 | { |
| 110 | if (is_null($this->fileSystem)) { |
| 111 | /** @var FileSystemService $fileSystemService */ |
| 112 | $fileSystemService = $this->getServiceManager()->get(FileSystemService::SERVICE_ID); |
| 113 | $this->fileSystem = $fileSystemService->getFileSystem($this->getOption(self::OPTION_FILESYSTEM)); |
| 114 | } |
| 115 | |
| 116 | return $this->fileSystem; |
| 117 | } |
| 118 | } |