| File was renamed from inc/integrations/CredentialsManager.php |
| | |
| | | <?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; |
| | | |
| | |
| | | $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([ |
| | |
| | | $this->table = $table; |
| | | } |
| | | |
| | | public static function getInstance(): CredentialsManager |
| | | public static function getInstance(): Auth |
| | | { |
| | | if (self::$instance === null) { |
| | | self::$instance = new self(); |
| | |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | protected function findBy(string $service, ?int $userID = null):array |
| | | { |
| | | return [ |
| | | 'user_id' => is_null($userID) ? 0 : $userID, |
| | | 'user_id' => $userID, |
| | | 'integration'=> $service |
| | | ]; |
| | | } |
| | |
| | | { |
| | | 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); |
| | | } |
| | | } |