| @@ -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; |
| @@ -6,29 +19,59 @@ | ||
| 6 | 19 | |
| 7 | 20 | use Exception; |
| 8 | 21 | use UserAccessManager\Wrapper\Wordpress; |
| 9 | 22 | |
| 23 | +/** | |
| 24 | + * Class Config | |
| 25 | + * | |
| 26 | + * @package UserAccessManager\Config | |
| 27 | + */ | |
| 10 | 28 | class Config |
| 11 | 29 | { |
| 12 | - protected string $key; | |
| 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, | |
| 61 | + Wordpress $wordpress, | |
| 25 | 62 | string $key |
| 26 | 63 | ) { |
| 64 | + $this->wordpress = $wordpress; | |
| 27 | 65 | $this->key = $key; |
| 28 | 66 | } |
| 29 | 67 | |
| 30 | - 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) | |
| 31 | 74 | { |
| 32 | 75 | if (!isset($this->wpOptions[$option]) === true) { |
| 33 | 76 | $this->wpOptions[$option] = $this->wordpress->getOption($option); |
| 34 | 77 | } |
| @@ -36,8 +79,9 @@ | ||
| 36 | 79 | return $this->wpOptions[$option]; |
| 37 | 80 | } |
| 38 | 81 | |
| 39 | 82 | /** |
| 83 | + * Returns the default parameters for the current config. | |
| 40 | 84 | * @return ConfigParameter[] |
| 41 | 85 | */ |
| 42 | 86 | protected function getDefaultConfigParameters(): array |
| 43 | 87 | { |
| @@ -44,16 +88,18 @@ | ||
| 44 | 88 | return $this->defaultConfigParameters; |
| 45 | 89 | } |
| 46 | 90 | |
| 47 | 91 | /** |
| 92 | + * Sets the default config parameters | |
| 48 | 93 | * @param ConfigParameter[] $defaultConfigParameters |
| 49 | 94 | */ |
| 50 | - public function setDefaultConfigParameters(array $defaultConfigParameters): void | |
| 95 | + public function setDefaultConfigParameters(array $defaultConfigParameters) | |
| 51 | 96 | { |
| 52 | 97 | $this->defaultConfigParameters = $defaultConfigParameters; |
| 53 | 98 | } |
| 54 | 99 | |
| 55 | 100 | /** |
| 101 | + * Returns the current settings | |
| 56 | 102 | * @return ConfigParameter[] |
| 57 | 103 | */ |
| 58 | 104 | public function getConfigParameters(): array |
| 59 | 105 | { |
| @@ -73,11 +119,12 @@ | ||
| 73 | 119 | return $this->configParameters; |
| 74 | 120 | } |
| 75 | 121 | |
| 76 | 122 | /** |
| 123 | + * Sets the new config parameters and saves them to the database. | |
| 77 | 124 | * @param array $rawParameters |
| 78 | 125 | */ |
| 79 | - public function setConfigParameters(array $rawParameters): void | |
| 126 | + public function setConfigParameters(array $rawParameters) | |
| 80 | 127 | { |
| 81 | 128 | $configParameters = $this->getConfigParameters(); |
| 82 | 129 | |
| 83 | 130 | foreach ($rawParameters as $key => $value) { |
| @@ -96,9 +143,12 @@ | ||
| 96 | 143 | |
| 97 | 144 | $this->wordpress->updateOption($this->key, $simpleConfigParameters); |
| 98 | 145 | } |
| 99 | 146 | |
| 100 | - public function flushConfigParameters(): void | |
| 147 | + /** | |
| 148 | + * Flushes the config parameters. | |
| 149 | + */ | |
| 150 | + public function flushConfigParameters() | |
| 101 | 151 | { |
| 102 | 152 | $this->defaultConfigParameters = []; |
| 103 | 153 | $this->configParameters = null; |
| 104 | 154 | } |
| @@ -103,27 +153,34 @@ | ||
| 103 | 153 | $this->configParameters = null; |
| 104 | 154 | } |
| 105 | 155 | |
| 106 | 156 | /** |
| 157 | + * Returns the requested parameter value | |
| 158 | + * @param string $parameterName | |
| 159 | + * @return mixed | |
| 107 | 160 | * @throws Exception |
| 108 | 161 | */ |
| 109 | - public function getParameterValueRaw(string $parameterName): mixed | |
| 162 | + public function getParameterValueRaw(string $parameterName) | |
| 110 | 163 | { |
| 111 | 164 | $options = $this->getConfigParameters(); |
| 112 | 165 | |
| 113 | 166 | if (isset($options[$parameterName]) === false) { |
| 114 | - throw new Exception("Unknown config parameter '$parameterName'."); | |
| 167 | + throw new Exception("Unknown config parameter '{$parameterName}'."); | |
| 115 | 168 | } |
| 116 | 169 | |
| 117 | 170 | return $options[$parameterName]->getValue(); |
| 118 | 171 | } |
| 119 | 172 | |
| 120 | - 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) | |
| 121 | 179 | { |
| 122 | 180 | try { |
| 123 | 181 | return $this->getParameterValueRaw($parameterName); |
| 124 | - } catch (Exception) { | |
| 182 | + } catch (Exception $exception) { | |
| 183 | + return null; | |
| 125 | 184 | } |
| 126 | - | |
| 127 | - return null; | |
| 128 | 185 | } |
| 129 | 186 | } |