PluginProbe
User Access Manager / 2.2.3
User Access Manager v2.2.3
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/Config/Config.php +92 -27 2.3.202.2.3 View file →
@@ -1,5 +1,18 @@
1 1 <?php
2 +/**
3 + * Config.php
4 + *
5 + * The Config class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
2 15
3 16 declare(strict_types=1);
4 17
5 18 namespace UserAccessManager\Config;
@@ -4,32 +17,63 @@
4 17
5 18 namespace UserAccessManager\Config;
6 19
7 20 use Exception;
8 -use UserAccessManager\Config\Parameter\ConfigParameter;
9 21 use UserAccessManager\Wrapper\Wordpress;
10 22
23 +/**
24 + * Class Config
25 + *
26 + * @package UserAccessManager\Config
27 + */
11 28 class Config
12 29 {
13 - protected array $wpOptions = [];
14 30 /**
31 + * @var Wordpress
32 + */
33 + protected $wordpress;
34 +
35 + /**
36 + * @var string
37 + */
38 + protected $key;
39 +
40 + /**
41 + * @var array
42 + */
43 + protected $wpOptions = [];
44 +
45 + /**
15 46 * @var ConfigParameter[]
16 47 */
17 - protected array $defaultConfigParameters = [];
48 + protected $defaultConfigParameters = [];
49 +
18 50 /**
19 51 * @var null|ConfigParameter[]
20 52 */
21 - protected ?array $configParameters = null;
53 + protected $configParameters = null;
22 54
55 + /**
56 + * Config constructor.
57 + * @param Wordpress $wordpress
58 + * @param string $key
59 + */
23 60 public function __construct(
24 - private Wordpress $wordpress,
25 - protected string $key
61 + Wordpress $wordpress,
62 + string $key
26 63 ) {
64 + $this->wordpress = $wordpress;
65 + $this->key = $key;
27 66 }
28 67
29 - public function getWpOption(string $option): mixed
68 + /**
69 + * Returns the WordPress options.
70 + * @param string $option
71 + * @return mixed
72 + */
73 + public function getWpOption(string $option)
30 74 {
31 - if (isset($this->wpOptions[$option]) === false) {
75 + if (!isset($this->wpOptions[$option]) === true) {
32 76 $this->wpOptions[$option] = $this->wordpress->getOption($option);
33 77 }
34 78
35 79 return $this->wpOptions[$option];
@@ -35,8 +79,9 @@
35 79 return $this->wpOptions[$option];
36 80 }
37 81
38 82 /**
83 + * Returns the default parameters for the current config.
39 84 * @return ConfigParameter[]
40 85 */
41 86 protected function getDefaultConfigParameters(): array
42 87 {
@@ -43,36 +88,43 @@
43 88 return $this->defaultConfigParameters;
44 89 }
45 90
46 91 /**
92 + * Sets the default config parameters
47 93 * @param ConfigParameter[] $defaultConfigParameters
48 94 */
49 - public function setDefaultConfigParameters(array $defaultConfigParameters): void
95 + public function setDefaultConfigParameters(array $defaultConfigParameters)
50 96 {
51 97 $this->defaultConfigParameters = $defaultConfigParameters;
52 98 }
53 99
54 100 /**
101 + * Returns the current settings
55 102 * @return ConfigParameter[]
56 103 */
57 104 public function getConfigParameters(): array
58 105 {
59 - if ($this->configParameters !== null) {
60 - return $this->configParameters;
61 - }
106 + if ($this->configParameters === null) {
107 + $configParameters = $this->getDefaultConfigParameters();
108 + $currentOptions = (array) $this->getWpOption($this->key);
62 109
63 - $configParameters = $this->getDefaultConfigParameters();
110 + foreach ($currentOptions as $key => $option) {
111 + if (isset($configParameters[$key])) {
112 + $configParameters[$key]->setValue($option);
113 + }
114 + }
64 115
65 - foreach ((array) $this->getWpOption($this->key) as $key => $option) {
66 - if (isset($configParameters[$key]) === true) {
67 - $configParameters[$key]->setValue($option);
68 - }
116 + $this->configParameters = $configParameters;
69 117 }
70 118
71 - return $this->configParameters = $configParameters;
119 + return $this->configParameters;
72 120 }
73 121
74 - public function setConfigParameters(array $rawParameters): void
122 + /**
123 + * Sets the new config parameters and saves them to the database.
124 + * @param array $rawParameters
125 + */
126 + public function setConfigParameters(array $rawParameters)
75 127 {
76 128 $configParameters = $this->getConfigParameters();
77 129
78 130 foreach ($rawParameters as $key => $value) {
@@ -80,18 +132,23 @@
80 132 $configParameters[$key]->setValue($value);
81 133 }
82 134 }
83 135
84 - $persistableValues = [];
136 + $this->configParameters = $configParameters;
85 137
138 + $simpleConfigParameters = [];
139 +
86 140 foreach ($configParameters as $parameter) {
87 - $persistableValues[$parameter->getId()] = $parameter->getValue();
141 + $simpleConfigParameters[$parameter->getId()] = $parameter->getValue();
88 142 }
89 143
90 - $this->wordpress->updateOption($this->key, $persistableValues);
144 + $this->wordpress->updateOption($this->key, $simpleConfigParameters);
91 145 }
92 146
93 - public function flushConfigParameters(): void
147 + /**
148 + * Flushes the config parameters.
149 + */
150 + public function flushConfigParameters()
94 151 {
95 152 $this->defaultConfigParameters = [];
96 153 $this->configParameters = null;
97 154 }
@@ -96,26 +153,34 @@
96 153 $this->configParameters = null;
97 154 }
98 155
99 156 /**
157 + * Returns the requested parameter value
158 + * @param string $parameterName
159 + * @return mixed
100 160 * @throws Exception
101 161 */
102 - public function getParameterValueRaw(string $parameterName): mixed
162 + public function getParameterValueRaw(string $parameterName)
103 163 {
104 164 $options = $this->getConfigParameters();
105 165
106 166 if (isset($options[$parameterName]) === false) {
107 - throw new Exception("Unknown config parameter '$parameterName'.");
167 + throw new Exception("Unknown config parameter '{$parameterName}'.");
108 168 }
109 169
110 170 return $options[$parameterName]->getValue();
111 171 }
112 172
113 - public function getParameterValue(string $parameterName): mixed
173 + /**
174 + * Returns the requested parameter value but suppresses exceptions.
175 + * @param string $parameterName
176 + * @return mixed
177 + */
178 + public function getParameterValue(string $parameterName)
114 179 {
115 180 try {
116 181 return $this->getParameterValueRaw($parameterName);
117 - } catch (Exception) {
182 + } catch (Exception $exception) {
118 183 return null;
119 184 }
120 185 }
121 186 }