xref: /plugin/twofactor/Settings.php (revision a635cb20bd5697d0bf04fcdee3b6cd14ce1590cd)
1<?php
2
3namespace dokuwiki\plugin\twofactor;
4
5/**
6 * Encapsulate the attribute plugin for per user/provider storage
7 *
8 * @todo maybe do our own settings saving with backwards compatibility to attribute?
9 */
10class Settings
11{
12
13    /** @var \helper_plugin_attribute */
14    protected $attribute;
15
16    /** @var string Identifier of the provider these settings are for */
17    protected $providerID;
18
19    /** @var string Login of the user these settings are for */
20    protected $user;
21
22    /**
23     * @param string $module Name of the provider
24     * @param string $user User login
25     * @throws \Exception
26     */
27    public function __construct($module, $user)
28    {
29        $this->attribute = plugin_load('helper', 'attribute');
30        if ($this->attribute === null) throw new \Exception('attribute plugin not found');
31
32        $this->providerID = $module;
33        $this->user = $user;
34    }
35
36    /**
37     * Check if a setting exists
38     *
39     * @param string $key Settings key
40     * @return bool
41     */
42    public function has($key)
43    {
44        return $this->attribute->exists($this->providerID, $key, $this->user);
45    }
46
47    /**
48     * Get a stored setting
49     *
50     * @param string $key Settings key
51     * @param mixed $default Default to return when no setting available
52     * @return mixed
53     */
54    public function get($key, $default = null)
55    {
56        $success = false;
57        $data = $this->attribute->get($this->providerID, $key, $success, $this->user);
58        if (!$success) return $default;
59        return $data;
60    }
61
62    /**
63     * Store a settings value
64     *
65     * @param string $key Settings key
66     * @param mixed $value Value to store
67     * @return bool
68     */
69    public function set($key, $value)
70    {
71        return $this->attribute->set($this->providerID, $key, $value, $this->user);
72    }
73
74    /**
75     * Delete a settings value
76     *
77     * @param string $key Settings key
78     * @return bool
79     */
80    public function delete($key)
81    {
82        return $this->attribute->del($this->providerID, $key, $this->user);
83    }
84
85    /**
86     * Remove all settings
87     *
88     * @return bool
89     */
90    public function purge()
91    {
92        return $this->attribute->purge($this->providerID, $this->user);
93    }
94
95}
96