Bläddra i källkod

Update AutospamUpdateCachedDataPipeline

Daniel Supernault 2 år sedan
förälder
incheckning
5abc2445a7
1 ändrade filer med 71 tillägg och 47 borttagningar
  1. 71 47
      app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php

+ 71 - 47
app/Jobs/AutospamPipeline/AutospamUpdateCachedDataPipeline.php

@@ -24,56 +24,80 @@ class AutospamUpdateCachedDataPipeline implements ShouldQueue
 	{
 	}
 
-    /**
-     * Execute the job.
-     */
-    public function handle(): void
-    {
-        $spam = json_decode(Storage::get(AutospamService::MODEL_SPAM_PATH), true);
-    	$newSpam = AutospamCustomTokens::whereCategory('spam')->get();
-    	foreach($newSpam as $ns) {
-    		$key = strtolower($ns->token);
-    		if(isset($spam['words']['spam'][$key])) {
-    			$spam['words']['spam'][$key] = $spam['words']['spam'][$key] + $ns->weight;
-    		} else {
-    			$spam['words']['spam'][$key] = $ns->weight;
-    		}
-    	}
-    	$newSpamCount = count($spam['words']['spam']);
-    	$spam['documents']['spam'] = $newSpamCount;
-    	arsort($spam['words']['spam']);
-    	Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
+	/**
+	 * Execute the job.
+	 */
+	public function handle(): void
+	{
+		$spamExists = Storage::exists(AutospamService::MODEL_SPAM_PATH);
+		if($spamExists) {
+			$spam = json_decode(Storage::get(AutospamService::MODEL_SPAM_PATH), true);
+		} else {
+			$spam = [
+				'documents' => [
+					'spam' => 0
+				],
+				'words' => [
+					'spam' => []
+				]
+			];
+		}
+		$newSpam = AutospamCustomTokens::whereCategory('spam')->get();
+		foreach($newSpam as $ns) {
+			$key = strtolower($ns->token);
+			if(isset($spam['words']['spam'][$key])) {
+				$spam['words']['spam'][$key] = $spam['words']['spam'][$key] + $ns->weight;
+			} else {
+				$spam['words']['spam'][$key] = $ns->weight;
+			}
+		}
+		$newSpamCount = count($spam['words']['spam']);
+		$spam['documents']['spam'] = $newSpamCount;
+		arsort($spam['words']['spam']);
+		Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
 
-    	$ham = json_decode(Storage::get(AutospamService::MODEL_HAM_PATH), true);
-    	$newHam = AutospamCustomTokens::whereCategory('ham')->get();
-    	foreach($newHam as $ns) {
-    		$key = strtolower($ns->token);
-    		if(isset($spam['words']['ham'][$key])) {
-    			$ham['words']['ham'][$key] = $ham['words']['ham'][$key] + $ns->weight;
-    		} else {
-    			$ham['words']['ham'][$key] = $ns->weight;
-    		}
-    	}
+		$hamExists = Storage::exists(AutospamService::MODEL_HAM_PATH);
+		if($hamExists) {
+			$ham = json_decode(Storage::get(AutospamService::MODEL_HAM_PATH), true);
+		} else {
+			$ham = [
+				'documents' => [
+					'ham' => 0
+				],
+				'words' => [
+					'ham' => []
+				]
+			];
+		}
+		$newHam = AutospamCustomTokens::whereCategory('ham')->get();
+		foreach($newHam as $ns) {
+			$key = strtolower($ns->token);
+			if(isset($spam['words']['ham'][$key])) {
+				$ham['words']['ham'][$key] = $ham['words']['ham'][$key] + $ns->weight;
+			} else {
+				$ham['words']['ham'][$key] = $ns->weight;
+			}
+		}
 
-    	$newHamCount = count($ham['words']['ham']);
-    	$ham['documents']['ham'] = $newHamCount;
-    	arsort($ham['words']['ham']);
+		$newHamCount = count($ham['words']['ham']);
+		$ham['documents']['ham'] = $newHamCount;
+		arsort($ham['words']['ham']);
 
-    	Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
+		Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
 
-    	$combined = [
-    		'documents' => [
-    			'spam' => $newSpamCount,
-    			'ham' => $newHamCount,
-    		],
-    		'words' => [
-    			'spam' => $spam['words']['spam'],
-    			'ham' => $ham['words']['ham']
-    		]
-    	];
+		$combined = [
+			'documents' => [
+				'spam' => $newSpamCount,
+				'ham' => $newHamCount,
+			],
+			'words' => [
+				'spam' => $spam['words']['spam'],
+				'ham' => $ham['words']['ham']
+			]
+		];
 
-    	Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES));
-    	Cache::forget(AutospamService::MODEL_CACHE_KEY);
-    	Cache::forget(AutospamService::CHCKD_CACHE_KEY);
-    }
+		Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES));
+		Cache::forget(AutospamService::MODEL_CACHE_KEY);
+		Cache::forget(AutospamService::CHCKD_CACHE_KEY);
+	}
 }