Forráskód Böngészése

Update Censorship

Move the work with the cache to the model.
Visman 4 éve
szülő
commit
de3df36af6

+ 22 - 5
app/Models/Censorship/Model.php

@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace ForkBB\Models\Censorship;
 
 use ForkBB\Models\Model as ParentModel;
+use RuntimeException;
 
 class Model extends ParentModel
 {
@@ -16,12 +17,16 @@ class Model extends ParentModel
         if ('1' == $this->c->config->o_censoring) {
             $list = $this->c->Cache->get('censorship');
 
-            if (isset($list['searchList'], $list['replaceList'])) {
-                $this->searchList  = $list['searchList'];
-                $this->replaceList = $list['replaceList'];
-            } else {
-                $this->refresh();
+            if (! isset($list['searchList'], $list['replaceList'])) {
+                $list = $this->refresh();
+
+                if (true !== $this->c->Cache->set('censorship', $list)) {
+                    throw new RuntimeException('Unable to write value to cache - censorship');
+                }
             }
+
+            $this->searchList  = $list['searchList'];
+            $this->replaceList = $list['replaceList'];
         }
 
         return $this;
@@ -38,4 +43,16 @@ class Model extends ParentModel
             return $str;
         }
     }
+
+    /**
+     * Сбрасывает кеш цензуры
+     */
+    public function reset(): Model
+    {
+        if (true !== $this->c->Cache->delete('censorship')) {
+            throw new RuntimeException('Unable to remove key from cache - censorship');
+        }
+
+        return $this;
+    }
 }

+ 4 - 13
app/Models/Censorship/Refresh.php

@@ -5,8 +5,6 @@ declare(strict_types=1);
 namespace ForkBB\Models\Censorship;
 
 use ForkBB\Models\Method;
-use ForkBB\Models\Censorship\Model as Censorship;
-use RuntimeException;
 
 class Refresh extends Method
 {
@@ -14,7 +12,7 @@ class Refresh extends Method
      * Заполняет модель данными из БД
      * Создает кеш
      */
-    public function refresh(): Censorship
+    public function refresh(): array
     {
         $query = 'SELECT ce.id, ce.search_for, ce.replace_with
             FROM ::censoring AS ce';
@@ -28,17 +26,10 @@ class Refresh extends Method
                 . ')(?![\p{L}\p{N}])%iu';
             $replace[$row['id']] = $row['replace_with'];
         }
-        $this->model->searchList  = $search;
-        $this->model->replaceList = $replace;
-        $result = $this->c->Cache->set('censorship', [
+
+        return [
             'searchList'  => $search,
             'replaceList' => $replace,
-        ]);
-
-        if (true !== $result) {
-            throw new RuntimeException('Unable to write value to cache - censorship');
-        }
-
-        return $this->model;
+        ];
     }
 }

+ 1 - 5
app/Models/Censorship/Save.php

@@ -64,10 +64,6 @@ class Save extends Method
             $this->c->DB->exec($query, $vars);
         }
 
-        if (true !== $this->c->Cache->delete('censorship')) {
-            throw new RuntimeException('Unable to remove key from cache - censorship');
-        }
-
-        return $this->model;
+        return $this->model->reset();
     }
 }