Remove static behavior from SettingsController

[MAILPOET-2436]
This commit is contained in:
Jan Jakeš
2019-10-28 10:39:48 +01:00
committed by Jack Kitterhing
parent d970dda637
commit 388ced8b53
5 changed files with 17 additions and 23 deletions

View File

@@ -16,16 +16,16 @@ class SettingsController {
const DEFAULT_SENDING_FREQUENCY_INTERVAL = 5; // in minutes const DEFAULT_SENDING_FREQUENCY_INTERVAL = 5; // in minutes
const DEFAULT_DEACTIVATE_SUBSCRIBER_AFTER_INACTIVE_DAYS = 180; const DEFAULT_DEACTIVATE_SUBSCRIBER_AFTER_INACTIVE_DAYS = 180;
private static $loaded = false; private $loaded = false;
private static $settings = []; private $settings = [];
private $defaults = null; private $defaults = null;
function get($key, $default = null) { function get($key, $default = null) {
$this->ensureLoaded(); $this->ensureLoaded();
$key_parts = explode('.', $key); $key_parts = explode('.', $key);
$setting = self::$settings; $setting = $this->settings;
if ($default === null) { if ($default === null) {
$default = $this->getDefaultValue($key_parts); $default = $this->getDefaultValue($key_parts);
} }
@@ -81,13 +81,13 @@ class SettingsController {
function fetch($key, $default = null) { function fetch($key, $default = null) {
$keys = explode('.', $key); $keys = explode('.', $key);
$main_key = $keys[0]; $main_key = $keys[0];
self::$settings[$main_key] = $this->fetchValue($main_key); $this->settings[$main_key] = $this->fetchValue($main_key);
return $this->get($key, $default); return $this->get($key, $default);
} }
function getAll() { function getAll() {
$this->ensureLoaded(); $this->ensureLoaded();
return array_replace_recursive($this->getAllDefaults(), self::$settings); return array_replace_recursive($this->getAllDefaults(), $this->settings);
} }
function set($key, $value) { function set($key, $value) {
@@ -95,7 +95,7 @@ class SettingsController {
$key_parts = explode('.', $key); $key_parts = explode('.', $key);
$main_key = $key_parts[0]; $main_key = $key_parts[0];
$last_key = array_pop($key_parts); $last_key = array_pop($key_parts);
$setting =& self::$settings; $setting =& $this->settings;
foreach ($key_parts as $key_part) { foreach ($key_parts as $key_part) {
$setting =& $setting[$key_part]; $setting =& $setting[$key_part];
if (!is_array($setting)) { if (!is_array($setting)) {
@@ -103,20 +103,20 @@ class SettingsController {
} }
} }
$setting[$last_key] = $value; $setting[$last_key] = $value;
$this->saveValue($main_key, self::$settings[$main_key]); $this->saveValue($main_key, $this->settings[$main_key]);
} }
function delete($key) { function delete($key) {
Setting::deleteValue($key); Setting::deleteValue($key);
unset(self::$settings[$key]); unset($this->settings[$key]);
} }
private function ensureLoaded() { private function ensureLoaded() {
if (self::$loaded) { if ($this->loaded) {
return; return;
} }
self::$settings = Setting::getAll() ?: []; $this->settings = Setting::getAll() ?: [];
self::$loaded = true; $this->loaded = true;
} }
private function getDefaultValue($keys) { private function getDefaultValue($keys) {
@@ -156,14 +156,9 @@ class SettingsController {
return ($setting->id() > 0 && $setting->getErrors() === false); return ($setting->id() > 0 && $setting->getErrors() === false);
} }
/** function resetCache() {
* Temporary function for tests use only. $this->settings = [];
* It is needed until this is only instantiated in one place (DI Container) $this->loaded = false;
* Once this is achieved we can make properties not static and remove this method
*/
static function resetCache() {
self::$settings = [];
self::$loaded = false;
} }
/** @return SettingsController */ /** @return SettingsController */

View File

@@ -39,7 +39,6 @@ class Newsletter {
$this->queue_options = []; $this->queue_options = [];
$this->task_subscribers = []; $this->task_subscribers = [];
$this->loadBodyFrom('newsletterWithALC.json'); $this->loadBodyFrom('newsletterWithALC.json');
SettingsController::resetCache(); // Newsletter model reads settings so we need to ensure it use fresh data
} }
/** /**

View File

@@ -10,8 +10,8 @@ class Settings {
private $settings; private $settings;
public function __construct() { public function __construct() {
SettingsController::resetCache();
$this->settings = SettingsController::getInstance(); $this->settings = SettingsController::getInstance();
$this->settings->resetCache();
} }
function withDefaultSettings() { function withDefaultSettings() {

View File

@@ -42,7 +42,7 @@ class SettingsTest extends \MailPoetTest {
expect($response->data['some']['setting']['key'])->true(); expect($response->data['some']['setting']['key'])->true();
Setting::deleteMany(); Setting::deleteMany();
SettingsController::resetCache(); $this->settings->resetCache();
$response = $this->endpoint->get(); $response = $this->endpoint->get();
expect($response->status)->equals(APIResponse::STATUS_OK); expect($response->status)->equals(APIResponse::STATUS_OK);
expect($response->data)->equals($this->settings->getAllDefaults()); expect($response->data)->equals($this->settings->getAllDefaults());

View File

@@ -149,7 +149,7 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test {
$this->di_container = ContainerWrapper::getInstance(WP_DEBUG); $this->di_container = ContainerWrapper::getInstance(WP_DEBUG);
$this->connection = $this->di_container->get(Connection::class); $this->connection = $this->di_container->get(Connection::class);
$this->entity_manager = $this->di_container->get(EntityManager::class); $this->entity_manager = $this->di_container->get(EntityManager::class);
\MailPoet\Settings\SettingsController::resetCache(); $this->di_container->get(SettingsController::class)->resetCache();
parent::setUp(); parent::setUp();
} }