<?php
class APIDB
{

    private $DB_HOST;

    private $DB_USER;

    private $DB_PASSWORD;

    private $DB_DATABASE;

    private $connection;

    private $isConnected = false;

    public $message;


    public function __construct($db_name = null)
    {

        $configuration = new CONFIG();

        $this->DB_HOST = $configuration->DB_HOST;


        $this->DB_USER = $configuration->DB_USER;

        $this->DB_PASSWORD = $configuration->DB_PASSWORD;

        $this->DB_DATABASE = $db_name == null ? $configuration->DB_DATABASE : $db_name;
    }

    public function dbConnection()
    {
        $this->connection = null;
        try {
            $this->connection = new PDO('mysql:dbhost=' . $this->DB_HOST . ';dbname=' . $this->DB_DATABASE, $this->DB_USER, $this->DB_PASSWORD);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->isConnected = true;
            $this->message = "";
        } catch (PDOException $e) {
            $this->message = $e->getMessage();
            $this->isConnected = false;
        }
        return $this->connection;
    }

    public function inService()
    {
        return $this->isConnected;
    }
}
