<?php

namespace Twnt4All\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 = 'pay4all_for_twint_migration_' . str_replace( '.', '_', $version );

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

        }

    }
}