xref: /plugin/twofactor/Settings.php (revision b6119621988e00fd0cbd3059406f4ed18460274b)
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     */
26    public function __construct($module, $user)
27    {
28        $this->attribute = plugin_load('helper', 'attribute');
29        if ($this->attribute === null) throw new \RuntimeException('attribute plugin not found');
30
31        $this->providerID = $module;
32        $this->user = $user;
33    }
34
35    /**
36     * Check if a setting exists
37     *
38     * @param string $key Settings key
39     * @return bool
40     */
41    public function has($key)
42    {
43        return $this->attribute->exists($this->providerID, $key, $this->user);
44    }
45
46    /**
47     * Get a stored setting
48     *
49     * @param string $key Settings key
50     * @param mixed $default Default to return when no setting available
51     * @return mixed
52     */
53    public function get($key, $default = null)
54    {
55        $success = false;
56        $data = $this->attribute->get($this->providerID, $key, $success, $this->user);
57        if (!$success) return $default;
58        return $data;
59    }
60
61    /**
62     * Store a settings value
63     *
64     * @param string $key Settings key
65     * @param mixed $value Value to store
66     * @return bool
67     */
68    public function set($key, $value)
69    {
70        return $this->attribute->set($this->providerID, $key, $value, $this->user);
71    }
72
73    /**
74     * Delete a settings value
75     *
76     * @param string $key Settings key
77     * @return bool
78     */
79    public function delete($key)
80    {
81        return $this->attribute->del($this->providerID, $key, $this->user);
82    }
83
84    /**
85     * Remove all settings
86     *
87     * @return bool
88     */
89    public function purge()
90    {
91        return $this->attribute->purge($this->providerID, $this->user);
92    }
93
94}
95