From c204185ae86a98994f80010abf35a190c9406739 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sun, 12 Jul 2026 18:08:19 +0000
Subject: [PATCH] =Refactor of Integrations.php. Separated different functionality into traits that classes can use to add that functionality. Hopefully will make maintaining it a little easier. Still have to finish up, as well as refactoring the individual classes to utilize the new system.

---
 inc/integrations/Auth.php |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/inc/integrations/CredentialsManager.php b/inc/integrations/Auth.php
similarity index 68%
rename from inc/integrations/CredentialsManager.php
rename to inc/integrations/Auth.php
index a3967a2..85dc232 100644
--- a/inc/integrations/CredentialsManager.php
+++ b/inc/integrations/Auth.php
@@ -1,15 +1,14 @@
 <?php
 namespace JVBase\integrations;
-
 use JVBase\managers\CustomTable;
 
 if (!defined('ABSPATH')) {
 	exit;
 }
 
-class CredentialsManager
+class Auth
 {
-	private static ?CredentialsManager $instance = null;
+	private static ?Auth $instance = null;
 	private string $encryption_key;
 	private CustomTable $table;
 
@@ -24,11 +23,16 @@
 		$table = CustomTable::for('integrations');
 		$table->setColumns([
 			'id'			=> 'bigint(20) unsigned NOT NULL AUTO_INCREMENT',
-			'user_id'		=> $table->getUserIDType().' NOT NULL',
+			'user_id'		=> $table->getUserIDType().' DEFAULT NULL',
 			'integration'	=> "ENUM('bluesky','cloudflare','facebook','google-maps','gmb','helcim','instagram','postmark','square','umami') NOT NULL",
-			'credentials'	=> 'varchar(255) DEFAULT NULL',
+			'credentials'	=> 'varchar(1000) DEFAULT NULL',
 			'created_at'	=> 'datetime DEFAULT CURRENT_TIMESTAMP',
-			'updated_at'	=> 'datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
+			'updated_at'	=> 'datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP',
+			'last_tested'	=> 'datetime DEFAULT NULL',
+			'is_healthy'	=> 'tinyint(1) DEFAULT 1',
+			'webhook_healthy'	=> 'tinyint(1) DEFAULT 1',
+			'request_healthy'	=> 'tinyint(1) DEFAULT 1',
+			'oauth_healthy'		=> 'tinyint(1) DEFAULT 1',
 		]);
 
 		$table->setKeys([
@@ -46,7 +50,7 @@
 		$this->table = $table;
 	}
 
-	public static function getInstance(): CredentialsManager
+	public static function getInstance(): Auth
 	{
 		if (self::$instance === null) {
 			self::$instance = new self();
@@ -60,10 +64,19 @@
 	public function storeCredentials(string $service, array $credentials, ?int $userID = null): bool
 	{
 		$find = $this->findBy($service, $userID);
+		error_log('Storing Credentials: '.print_r($credentials, true));
+
 		$update = [
 			'credentials'	=> $this->encrypt(json_encode($credentials))
 		];
-		return (bool)$this->table->findOrCreate($find, $update);
+
+		error_log('Find: '.print_r($find, true));
+		error_log('Update: '.print_r($update, true));
+		$updated = $this->table->findOrCreate($find, $update);
+		error_log('Last error: '.print_r($this->table->getLastError(), true));
+		error_log('Updated: '.print_r($updated, true));
+
+		return (bool)$updated;
 	}
 
 	/**
@@ -92,7 +105,7 @@
 	protected function findBy(string $service, ?int $userID = null):array
 	{
 		return [
-			'user_id'	=> is_null($userID) ? 0 : $userID,
+			'user_id'	=> $userID,
 			'integration'=> $service
 		];
 	}
@@ -142,4 +155,33 @@
 	{
 		return $this->table->pluck('user_id', ['integration' => $service]);
 	}
+
+	public function updateTested(string $service, ?int $userID = null):void
+	{
+		$find = $this->findBy($service, $userID);
+		$this->table->update([
+			'last_tested'	=> time(),
+		], $find);
+	}
+
+	public function markUnhealthy(string $service, string $type = '', ?int $userID = null):void
+	{
+		$update = [];
+		switch ($type) {
+			case 'oauth':
+				$update['oauth_healthy'] = 0;
+				break;
+			case 'request':
+				$update['request_healthy'] = 0;
+				break;
+			case 'webhook':
+				$update['webhook_healthy'] = 0;
+				break;
+			default:
+				$update['is_healthy'] = 0;
+				break;
+		}
+		$find = $this->findBy($service, $userID);
+		$this->table->update($update, $find);
+	}
 }

--
Gitblit v1.10.0