<?php

namespace FOP;

class ClassStaticMethodDelayed {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function worker($transaction = true ) { 
    
    
    // --- BLOCK: main ---
    
	$cronStart = time();
	$olduser = \FOP\User::getID();
	try {
		try {
			while (time() - $cronStart < 120) {
				\FOP\Database::setTime(time());
				if ($transaction) \FOP\Database::beginTransaction();
				$row = \FOP\Database::query('SELECT * FROM delayedmethods WHERE '.qi('lock').' IS NULL AND COALESCE(start, 0) < '.\FOP\Database::sessionVarSQL('time').' LIMIT 1;')->fetch(\PDO::FETCH_ASSOC);

				if (empty($row)) {
					if ($transaction) \FOP\Database::rollbackTransaction();
					return;
				} else {
						$stmt = \FOP\Database::prepare('UPDATE delayedmethods SET '.qi('lock').' = '.\FOP\Database::sessionVarSQL('time').' WHERE '.qi('lock').' IS NULL AND '.qi('ID').' = ' . \FOP\Database::quote($row['ID']));
						$stmt->execute();
						if ($transaction) \FOP\Database::commitTransaction();
						if ($stmt->rowCount()) {
							\FOP\User::login($row['user'], false);
							\FOP\User::updateColumns();
							call_user_func_array(str_replace('\\\\', '\\', $row['method']), json_decode($row['parameter'], true));
							\FOP\Database::query('DELETE FROM delayedmethods WHERE '.qi('ID').' = ' . \FOP\Database::quote($row['ID']));
						}
					}
			}
		} catch (\Exception $e) {
			error_log('Failed during delayed method: ' . $e->getMessage() . "\n" . $e->getTraceAsString());
		}
	} finally {
		\FOP\User::login($olduser, false);
		\FOP\User::updateColumns();
	}
	
    
  }


  
  

  
  
}



