]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Config/PreloadPConfigAdapter.php
Refactor ConfigMockTrait to mocked ConfigCache
[friendica.git] / src / Core / Config / PreloadPConfigAdapter.php
index 88774f2979b81014502d6d2c55fa9d7daa96694a..af97815adef38d159ab6ba44f27766d10ed7ca9d 100644 (file)
@@ -3,7 +3,6 @@
 namespace Friendica\Core\Config;
 
 use Exception;
-use Friendica\Core\PConfig;
 use Friendica\Database\DBA;
 
 /**
@@ -17,11 +16,27 @@ class PreloadPConfigAdapter implements IPConfigAdapter
 {
        private $config_loaded = false;
 
-       public function __construct($uid)
+       /**
+        * The config cache of this adapter
+        * @var IPConfigCache
+        */
+       private $configCache;
+
+       /**
+        * @param IPConfigCache $configCache The config cache of this adapter
+        * @param int           $uid    The UID of the current user
+        */
+       public function __construct(IPConfigCache $configCache, $uid = null)
        {
-               $this->load($uid, 'config');
+               $this->configCache = $configCache;
+               if (isset($uid)) {
+                       $this->load($uid, 'config');
+               }
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function load($uid, $family)
        {
                if ($this->config_loaded) {
@@ -34,13 +49,16 @@ class PreloadPConfigAdapter implements IPConfigAdapter
 
                $pconfigs = DBA::select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
                while ($pconfig = DBA::fetch($pconfigs)) {
-                       PConfig::setPConfigValue($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
+                       $this->configCache->setP($uid, $pconfig['cat'], $pconfig['k'], $pconfig['v']);
                }
                DBA::close($pconfigs);
 
                $this->config_loaded = true;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function get($uid, $cat, $k, $default_value = null, $refresh = false)
        {
                if (!$this->config_loaded) {
@@ -50,17 +68,18 @@ class PreloadPConfigAdapter implements IPConfigAdapter
                if ($refresh) {
                        $config = DBA::selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $k]);
                        if (DBA::isResult($config)) {
-                               PConfig::setPConfigValue($uid, $cat, $k, $config['v']);
+                               $this->configCache->setP($uid, $cat, $k, $config['v']);
                        } else {
-                               PConfig::deletePConfigValue($uid, $cat, $k);
+                               $this->configCache->deleteP($uid, $cat, $k);
                        }
                }
 
-               $return = PConfig::getPConfigValue($uid, $cat, $k, $default_value);
-
-               return $return;
+               return $this->configCache->getP($uid, $cat, $k, $default_value);;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function set($uid, $cat, $k, $value)
        {
                if (!$this->config_loaded) {
@@ -71,11 +90,11 @@ class PreloadPConfigAdapter implements IPConfigAdapter
                // The exception are array values.
                $compare_value = !is_array($value) ? (string)$value : $value;
 
-               if (PConfig::getPConfigValue($uid, $cat, $k) === $compare_value) {
+               if ($this->configCache->getP($uid, $cat, $k) === $compare_value) {
                        return true;
                }
 
-               PConfig::setPConfigValue($uid, $cat, $k, $value);
+               $this->configCache->setP($uid, $cat, $k, $value);
 
                // manage array value
                $dbvalue = is_array($value) ? serialize($value) : $value;
@@ -88,13 +107,16 @@ class PreloadPConfigAdapter implements IPConfigAdapter
                return true;
        }
 
+       /**
+        * {@inheritdoc}
+        */
        public function delete($uid, $cat, $k)
        {
                if (!$this->config_loaded) {
                        $this->load($uid, $cat);
                }
 
-               PConfig::deletePConfigValue($uid, $cat, $k);
+               $this->configCache->deleteP($uid, $cat, $k);
 
                $result = DBA::delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $k]);