<?php
namespace SabaiApps\Framework\DB;

/**
 * AbstractConnection class for managing connections to a database.
 * Provides methods for establishing connections and retrieving connection details.
 */
abstract class AbstractConnection {

	/**
	 * @var string
	 */
	protected $_scheme;
	/**
	 * @var bool
	 */
	protected $_connected;

	/**
	 * Constructor
	 *
	 * @param string $scheme
	 */
	protected function __construct( $scheme ) {
		$this->_scheme = $scheme;
	}

	/**
	 * Establishes a connection with the data source.
	 * Ensures the connection is only established once.
	 *
	 * @return $this
	 */
	public function connect() {
		if ( ! $this->_connected ) {
			$this->_doConnect();
			$this->_connected = true;
		}

		return $this;
	}

	/**
	 * Gets the name of the database scheme.
	 *
	 * @return string Scheme name.
	 */
	public function getScheme() {
		return $this->_scheme;
	}

	/**
	 * Abstract method for implementing the connection logic.
	 * Must be implemented by subclasses.
	 */
	abstract protected function _doConnect();
}
