Backend
1 month ago
Factory.php
3 months ago
LegacyUserSettingsMigration.php
1 month ago
Storage.php
1 year ago
UserScopedSettingsAccessManager.php
1 month ago
UserScopedSettingsAccessManager.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Settings\Storage; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Settings\Storage\Backend\PluginSettingsTable; |
| 13 | class UserScopedSettingsAccessManager |
| 14 | { |
| 15 | /** |
| 16 | * @var array<string, PluginSettingsTable> |
| 17 | */ |
| 18 | private $backends = []; |
| 19 | /** |
| 20 | * @param mixed $defaultValue |
| 21 | * @return mixed |
| 22 | */ |
| 23 | public function get(string $pluginName, string $userLogin, string $key, $defaultValue = null) |
| 24 | { |
| 25 | return $this->getBackend($pluginName, $userLogin)->loadValue($key, $defaultValue); |
| 26 | } |
| 27 | /** |
| 28 | * @param mixed $value |
| 29 | */ |
| 30 | public function set(string $pluginName, string $userLogin, string $key, $value) : void |
| 31 | { |
| 32 | $this->getBackend($pluginName, $userLogin)->saveValue($key, $value); |
| 33 | } |
| 34 | public function getAll(string $pluginName, string $userLogin) : array |
| 35 | { |
| 36 | $this->validatePluginAndLogin($pluginName, $userLogin); |
| 37 | return $this->getBackend($pluginName, $userLogin)->load(); |
| 38 | } |
| 39 | public function setAll(string $pluginName, string $userLogin, array $settings) : void |
| 40 | { |
| 41 | $this->validatePluginAndLogin($pluginName, $userLogin); |
| 42 | $this->getBackend($pluginName, $userLogin)->save($settings); |
| 43 | } |
| 44 | public function delete(string $pluginName, string $userLogin, string $key) : void |
| 45 | { |
| 46 | $this->getBackend($pluginName, $userLogin)->deleteValue($key); |
| 47 | } |
| 48 | public function deleteAll(string $pluginName, string $userLogin) : void |
| 49 | { |
| 50 | $this->validatePluginAndLogin($pluginName, $userLogin); |
| 51 | $this->getBackend($pluginName, $userLogin)->delete(); |
| 52 | } |
| 53 | /** |
| 54 | * @param string[] $settingNames |
| 55 | * @return array<string, array<string, mixed>> |
| 56 | */ |
| 57 | public function getValuesForAllUsers(string $pluginName, array $settingNames) : array |
| 58 | { |
| 59 | if (empty($pluginName) || empty($settingNames)) { |
| 60 | return []; |
| 61 | } |
| 62 | // login is not used by loadValuesForUsers(), value is irrelevant |
| 63 | return $this->getBackend($pluginName, $userLogin = '')->loadValuesForUsers($settingNames); |
| 64 | } |
| 65 | private function validatePluginAndLogin(string $pluginName, string $userLogin) : void |
| 66 | { |
| 67 | if (empty($pluginName)) { |
| 68 | throw new \Exception('No plugin name specified for user scoped settings store'); |
| 69 | } |
| 70 | if (empty($userLogin)) { |
| 71 | throw new \Exception('No username specified for user scoped settings store'); |
| 72 | } |
| 73 | } |
| 74 | /** |
| 75 | * @throws Exception when $pluginName is empty |
| 76 | */ |
| 77 | private function getBackend(string $pluginName, string $userLogin) : PluginSettingsTable |
| 78 | { |
| 79 | if (empty($pluginName)) { |
| 80 | throw new \Exception('No plugin name specified for user scoped settings store'); |
| 81 | } |
| 82 | $id = $pluginName . '#' . $userLogin; |
| 83 | if (empty($this->backends[$id])) { |
| 84 | $this->backends[$id] = new PluginSettingsTable($pluginName, $userLogin); |
| 85 | } |
| 86 | return $this->backends[$id]; |
| 87 | } |
| 88 | } |
| 89 |