<?php

// Encapsulates a single connection to a database
class DBHandle 
{
    private string $host    = CONFIG['database']['args']['host'];
    private string $db      = CONFIG['database']['args']['database'];
    private string $user    = CONFIG['database']['args']['user'];
    private string $pass    = CONFIG['database']['args']['password'];
    private string $charset = CONFIG['database']['args']['charset'];

    public object $dbh;

    public function __construct()
    {
        try {
            $this->dbh = $this->connectWithMySQL();
        } catch (PDOException $e) {
            throw new PDOException($e->getMessage(), (int)$e->getCode());
        }
    }

    private function connectWithMySQL(): object 
    {
        $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
        $options = [
            PDO::ATTR_PERSISTENT => true,
        ];
        return new PDO($dsn, $this->user, $this->pass, $options);
    }
}