1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,159 @@ |
1 |
+<?php |
|
2 |
+/** |
|
3 |
+ * @copyright 2021 Double Bastion LLC <www.doublebastion.com> |
|
4 |
+ * |
|
5 |
+ * @author Double Bastion LLC |
|
6 |
+ * |
|
7 |
+ * @license GNU AGPL version 3 or any later version |
|
8 |
+ * |
|
9 |
+ * This program is free software; you can redistribute it and/or |
|
10 |
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
|
11 |
+ * License as published by the Free Software Foundation; either |
|
12 |
+ * version 3 of the License, or any later version. |
|
13 |
+ * |
|
14 |
+ * This program is distributed in the hope that it will be useful, |
|
15 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
|
18 |
+ * |
|
19 |
+ * You should have received a copy of the GNU Affero General Public |
|
20 |
+ * License along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
+ * |
|
22 |
+ */ |
|
23 |
+ |
|
24 |
+declare(strict_types=1); |
|
25 |
+ |
|
26 |
+namespace OCA\SMSRelentless; |
|
27 |
+ |
|
28 |
+use OCA\SMSRelentless\AppInfo\Application; |
|
29 |
+ |
|
30 |
+use OCP\IConfig; |
|
31 |
+ |
|
32 |
+ |
|
33 |
+class AppConfig { |
|
34 |
+ private $appName = 'sms_relentless'; |
|
35 |
+ |
|
36 |
+ /** @var IConfig */ |
|
37 |
+ private $config; |
|
38 |
+ |
|
39 |
+ /** |
|
40 |
+ * AppConfig constructor. |
|
41 |
+ * |
|
42 |
+ * @param IConfig $config |
|
43 |
+ */ |
|
44 |
+ public function __construct(IConfig $config) { |
|
45 |
+ $this->config = $config; |
|
46 |
+ } |
|
47 |
+ |
|
48 |
+ /** |
|
49 |
+ * Get all setting values as an array |
|
50 |
+ * @return array |
|
51 |
+ */ |
|
52 |
+ public function getAllValues() { |
|
53 |
+ $keys = array_keys($this->defaults); |
|
54 |
+ $values = array_map([$this, 'getAppValue'], $keys); |
|
55 |
+ $preparedKeys = array_map([$this, 'camelCase'], $keys); |
|
56 |
+ return array_combine($preparedKeys, $values); |
|
57 |
+ } |
|
58 |
+ |
|
59 |
+ /** |
|
60 |
+ * Get a value by key |
|
61 |
+ * @param string $key |
|
62 |
+ * @return string |
|
63 |
+ */ |
|
64 |
+ public function getAppValue($key) { |
|
65 |
+ $defaultValue = null; |
|
66 |
+ if (array_key_exists($key, $this->defaults)){ |
|
67 |
+ $defaultValue = $this->defaults[$key]; |
|
68 |
+ } |
|
69 |
+ return $this->config->getAppValue($this->appName, $key, $defaultValue); |
|
70 |
+ } |
|
71 |
+ |
|
72 |
+ /** |
|
73 |
+ * Set a value by key |
|
74 |
+ * @param string $key |
|
75 |
+ * @param string $value |
|
76 |
+ */ |
|
77 |
+ public function setAppValue($key, $value) { |
|
78 |
+ $this->config->setAppValue($this->appName, $key, $value); |
|
79 |
+ } |
|
80 |
+ |
|
81 |
+ /** |
|
82 |
+ * Set a value with magic __call invocation |
|
83 |
+ * @param string $key |
|
84 |
+ * @param array $args |
|
85 |
+ * @throws \BadFunctionCallException |
|
86 |
+ */ |
|
87 |
+ protected function setter($key, $args) { |
|
88 |
+ if (array_key_exists($key, $this->defaults)) { |
|
89 |
+ $this->setAppValue($key, $args[0]); |
|
90 |
+ } else { |
|
91 |
+ throw new \BadFunctionCallException($key . ' is not a valid key'); |
|
92 |
+ } |
|
93 |
+ } |
|
94 |
+ |
|
95 |
+ /** |
|
96 |
+ * Get a value with magic __call invocation |
|
97 |
+ * @param string $key |
|
98 |
+ * @return string |
|
99 |
+ * @throws \BadFunctionCallException |
|
100 |
+ */ |
|
101 |
+ protected function getter($key) { |
|
102 |
+ if (array_key_exists($key, $this->defaults)) { |
|
103 |
+ return $this->getAppValue($key); |
|
104 |
+ } |
|
105 |
+ |
|
106 |
+ throw new \BadFunctionCallException($key . ' is not a valid key'); |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ /** |
|
110 |
+ * Translates property_name into propertyName |
|
111 |
+ * @param string $property |
|
112 |
+ * @return string |
|
113 |
+ */ |
|
114 |
+ protected function camelCase($property){ |
|
115 |
+ $split = explode('_', $property); |
|
116 |
+ $ucFirst = implode('', array_map('ucfirst', $split)); |
|
117 |
+ return lcfirst($ucFirst); |
|
118 |
+ } |
|
119 |
+ |
|
120 |
+ /** |
|
121 |
+ * Does all the someConfig to some_config magic |
|
122 |
+ * @param string $property |
|
123 |
+ * @return string |
|
124 |
+ */ |
|
125 |
+ protected function propertyToKey($property){ |
|
126 |
+ $parts = preg_split('/(?=[A-Z])/', $property); |
|
127 |
+ $column = null; |
|
128 |
+ |
|
129 |
+ foreach($parts as $part){ |
|
130 |
+ if($column === null){ |
|
131 |
+ $column = $part; |
|
132 |
+ } else { |
|
133 |
+ $column .= '_' . lcfirst($part); |
|
134 |
+ } |
|
135 |
+ } |
|
136 |
+ |
|
137 |
+ return $column; |
|
138 |
+ } |
|
139 |
+ |
|
140 |
+ /** |
|
141 |
+ * Get/set an option value by calling getSomeOption method |
|
142 |
+ * @param string $methodName |
|
143 |
+ * @param array $args |
|
144 |
+ * @return string|null |
|
145 |
+ * @throws \BadFunctionCallException |
|
146 |
+ */ |
|
147 |
+ public function __call($methodName, $args){ |
|
148 |
+ $attr = lcfirst( substr($methodName, 3) ); |
|
149 |
+ $key = $this->propertyToKey($attr); |
|
150 |
+ if(strpos($methodName, 'set') === 0){ |
|
151 |
+ $this->setter($key, $args); |
|
152 |
+ } elseif(strpos($methodName, 'get') === 0) { |
|
153 |
+ return $this->getter($key); |
|
154 |
+ } else { |
|
155 |
+ throw new \BadFunctionCallException($methodName . |
|
156 |
+ ' does not exist'); |
|
157 |
+ } |
|
158 |
+ } |
|
159 |
+} |