encryption = $encryption; $this->tableName = $wpdb->prefix . 'oneclickformsync_connections'; } /** * @inheritDoc */ public function find(string $id): ?Connection { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$this->tableName} WHERE id = %s", $id), ARRAY_A ); if (!$row) { return null; } return $this->mapRowToConnection($row); } /** * @inheritDoc */ public function findAll(): array { global $wpdb; $rows = $wpdb->get_results( "SELECT * FROM {$this->tableName} ORDER BY created_at DESC", ARRAY_A ); $connections = []; foreach ($rows as $row) { $connections[] = $this->mapRowToConnection($row); } return $connections; } /** * @inheritDoc */ public function save(Connection $connection): bool { global $wpdb; $data = $connection->toArray(); $data['updated_at'] = current_time('mysql'); // Encrypt credentials before database insertion $data['client_secret'] = $this->encryption->encrypt($data['client_secret']); $data['refresh_token'] = $this->encryption->encrypt($data['refresh_token']); $data['access_token'] = $this->encryption->encrypt($data['access_token']); $exists = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM {$this->tableName} WHERE id = %s", $connection->getId()) ); if ($exists) { $result = $wpdb->update( $this->tableName, $data, ['id' => $connection->getId()] ); return $result !== false; } else { $data['created_at'] = current_time('mysql'); $result = $wpdb->insert( $this->tableName, $data ); return $result !== false; } } /** * @inheritDoc */ public function delete(string $id): bool { global $wpdb; $result = $wpdb->delete( $this->tableName, ['id' => $id] ); return $result !== false; } /** * Map a raw database row to a decrypted Connection entity. * * @param array $row * @return Connection */ private function mapRowToConnection(array $row): Connection { $row['client_secret'] = $this->encryption->decrypt($row['client_secret']); $row['refresh_token'] = $this->encryption->decrypt($row['refresh_token']); $row['access_token'] = $this->encryption->decrypt($row['access_token']); return new Connection($row); } }