<?php

namespace App\Config;

use mysqli;

class Database
{
    private static $conn = null;

    /**
     * Database Connection
     * 
     * @throws Exception
     */
    public static function connection()
    {
        if (self::$conn === null) {

            self::$conn = new mysqli(
                $_ENV['DB_HOST'],
                $_ENV['DB_USER'],
                $_ENV['DB_PASS'],
                $_ENV['DB_NAME'],
                $_ENV['DB_PORT']
            );

            if (self::$conn->connect_error) {
                throw new \Exception('DB Connection Error');
            }

            self::$conn->set_charset('utf8mb4');
        }

        return self::$conn;
    }
}
