File size: 3,108 Bytes
800aa60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Settings\Storage;

use Exception;
use Piwik\Settings\Storage\Backend\PluginSettingsTable;

class UserScopedSettingsAccessManager
{
    /**
     * @var array<string, PluginSettingsTable>
     */
    private $backends = [];

    /**
     * @param mixed $defaultValue
     * @return mixed
     */
    public function get(string $pluginName, string $userLogin, string $key, $defaultValue = null)
    {
        return $this->getBackend($pluginName, $userLogin)->loadValue($key, $defaultValue);
    }

    /**
     * @param mixed $value
     */
    public function set(string $pluginName, string $userLogin, string $key, $value): void
    {
        $this->getBackend($pluginName, $userLogin)->saveValue($key, $value);
    }

    public function getAll(string $pluginName, string $userLogin): array
    {
        $this->validatePluginAndLogin($pluginName, $userLogin);
        return $this->getBackend($pluginName, $userLogin)->load();
    }

    public function setAll(string $pluginName, string $userLogin, array $settings): void
    {
        $this->validatePluginAndLogin($pluginName, $userLogin);
        $this->getBackend($pluginName, $userLogin)->save($settings);
    }

    public function delete(string $pluginName, string $userLogin, string $key): void
    {
        $this->getBackend($pluginName, $userLogin)->deleteValue($key);
    }

    public function deleteAll(string $pluginName, string $userLogin): void
    {
        $this->validatePluginAndLogin($pluginName, $userLogin);
        $this->getBackend($pluginName, $userLogin)->delete();
    }

    /**
     * @param string[] $settingNames
     * @return array<string, array<string, mixed>>
     */
    public function getValuesForAllUsers(string $pluginName, array $settingNames): array
    {
        if (empty($pluginName) || empty($settingNames)) {
            return [];
        }

        // login is not used by loadValuesForUsers(), value is irrelevant
        return $this->getBackend($pluginName, $userLogin = '')->loadValuesForUsers($settingNames);
    }

    private function validatePluginAndLogin(string $pluginName, string $userLogin): void
    {
        if (empty($pluginName)) {
            throw new \Exception('No plugin name specified for user scoped settings store');
        }

        if (empty($userLogin)) {
            throw new \Exception('No username specified for user scoped settings store');
        }
    }

    /**
     * @throws Exception when $pluginName is empty
     */
    private function getBackend(string $pluginName, string $userLogin): PluginSettingsTable
    {
        if (empty($pluginName)) {
            throw new \Exception('No plugin name specified for user scoped settings store');
        }

        $id = $pluginName . '#' . $userLogin;
        if (empty($this->backends[$id])) {
            $this->backends[$id] = new PluginSettingsTable($pluginName, $userLogin);
        }
        return $this->backends[$id];
    }
}