<?php

namespace Pay4AllForMypos\Support;

class MigrationRunner
{
    public static function run( $current_version, $previous_version )
    {
        if ( $current_version == $previous_version ) {
            return;
        }

        $path  = dirname( __DIR__ ) . DIRECTORY_SEPARATOR . 'migrations' . DIRECTORY_SEPARATOR;
        $files = glob( "$path*.php", GLOB_BRACE );


        if ( empty( $files ) ) {
            return;
        }

        foreach ( $files as $file ) {
            
            $version = basename( $file, '.php' );

            if ( version_compare( $version, $previous_version, '<=' ) || version_compare( $version, $current_version, '>' ) ) {
                continue;
            }

            include $file;
            $func = 'twnt_tw_migration_' . str_replace( '.', '_', $version );

            if ( function_exists( $func ) ) {
                Journal::info( 'Running migration for version ' . $version );
                $func();
            }

        }

    }
}