<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\Migration;

use ModulesGarden\HostingQuotaNotifications\App\Helper\Migration\Table;
use ModulesGarden\HostingQuotaNotifications\App\Helper\Migration\DBManager;
use ModulesGarden\HostingQuotaNotifications\App\Libs\ModuleSettings\Manage;

/**
 * Class Migrate
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Migration
{
    CONST TABLES_CLASS_NAMESPACE = "ModulesGarden\HostingQuotaNotifications\App\Libs\Migration\Tables\\";
    CONST MIGRATED = 'migrated';

    public function run()
    {
        if(Manage::getValueIfSetting(SELF::MIGRATED))
        {
            return;
        }

        $this->migrateTables();
        Manage::set(SELF::MIGRATED, 'true');
    }

    public function migrateTables()
    {
        foreach (Table::getTablesToMigrate() as $className => $table)
        {
            $class = SELF::TABLES_CLASS_NAMESPACE . $className;
            $class =  new $class($table);
            $class->migrate();
        }
    }

    public function deleteMigratedTables()
    {
        foreach (Table::getTables() as $table)
        {
            DBManager::deleteTable($table);
        }
    }




}