<?php

namespace Extension\VisitorBlocker;

use Application\Config;
use Database\Connectors\ConnectionFactory;
use Exception;
use PDO;

class Helper
{
    private static $dbConnection = null;

    private function __construct()
    {
        return;
    }

    public static function getDatabaseConnection()
    {
        if (self::$dbConnection === null) {
            try {
                $factory            = new ConnectionFactory();
                self::$dbConnection = $factory->make(array(
                    'driver'    => 'mysql',
                    'host'      => Config::settings('db_host'),
                    'username'  => Config::settings('db_username'),
                    'password'  => Config::settings('db_password'),
                    'database'  => Config::settings('db_name'),
                    'charset'   => 'utf8',
                    'collation' => 'utf8_unicode_ci',
                    'options' => [PDO::ATTR_EMULATE_PREPARES => true],
                ));
            } catch (Exception $ex) {
                return false;
            }
        }
        return self::$dbConnection;
    }
}
