Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 13 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SettingsStorage | |
0.00% |
0 / 13 |
|
0.00% |
0 / 6 |
90 | |
0.00% |
0 / 1 |
| set | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| get | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| exists | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| del | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPersistence | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getKey | |
0.00% |
0 / 2 |
|
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) 2019 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\tao\model\service; |
| 22 | |
| 23 | use common_Exception; |
| 24 | use common_persistence_KeyValuePersistence; |
| 25 | use common_persistence_Persistence; |
| 26 | use oat\oatbox\service\ConfigurableService; |
| 27 | use oat\tao\model\settings\SettingsStorageInterface; |
| 28 | |
| 29 | /** |
| 30 | * Persistence for settings |
| 31 | * |
| 32 | * @author Martijn Swinkels <m.swinkels@taotesting.com> |
| 33 | */ |
| 34 | class SettingsStorage extends ConfigurableService implements SettingsStorageInterface |
| 35 | { |
| 36 | public const OPTION_PERSISTENCE = 'persistence'; |
| 37 | public const OPTION_KEY_NAMESPACE = 'key_namespace'; |
| 38 | |
| 39 | /** |
| 40 | * @var common_persistence_KeyValuePersistence |
| 41 | */ |
| 42 | private $persistence; |
| 43 | |
| 44 | /** |
| 45 | * @inheritdoc |
| 46 | */ |
| 47 | public function set($settingId, $data) |
| 48 | { |
| 49 | try { |
| 50 | return $this->getPersistence()->set($this->getKey($settingId), $data); |
| 51 | } catch (common_Exception $e) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @inheritdoc |
| 58 | */ |
| 59 | public function get($settingId) |
| 60 | { |
| 61 | return $this->getPersistence()->get($this->getKey($settingId)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @inheritdoc |
| 66 | */ |
| 67 | public function exists($settingId) |
| 68 | { |
| 69 | return $this->getPersistence()->exists($this->getKey($settingId)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @inheritdoc |
| 74 | */ |
| 75 | public function del($settingId) |
| 76 | { |
| 77 | return $this->getPersistence()->del($this->getKey($settingId)); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @return common_persistence_Persistence |
| 82 | */ |
| 83 | private function getPersistence() |
| 84 | { |
| 85 | if ($this->persistence === null) { |
| 86 | $this->persistence = common_persistence_KeyValuePersistence::getPersistence( |
| 87 | $this->getOption(self::OPTION_PERSISTENCE) |
| 88 | ); |
| 89 | } |
| 90 | return $this->persistence; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @param string $settingId |
| 95 | * @return string |
| 96 | */ |
| 97 | private function getKey($settingId) |
| 98 | { |
| 99 | $namespace = $this->hasOption(self::OPTION_KEY_NAMESPACE) ? $this->getOption(self::OPTION_KEY_NAMESPACE) : ''; |
| 100 | |
| 101 | return $namespace . $settingId; |
| 102 | } |
| 103 | } |