geno/wp-content/plugins/cartflows/libraries/action-scheduler/classes/abstracts/ActionScheduler_Lock.php

65 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2024-02-01 17:24:18 +05:30
<?php
/**
* Abstract class for setting a basic lock to throttle some action.
*
* Class ActionScheduler_Lock
*/
abstract class ActionScheduler_Lock {
/** @var ActionScheduler_Lock */
private static $locker = NULL;
/** @var int */
protected static $lock_duration = MINUTE_IN_SECONDS;
/**
* Check if a lock is set for a given lock type.
*
* @param string $lock_type A string to identify different lock types.
* @return bool
*/
public function is_locked( $lock_type ) {
return ( $this->get_expiration( $lock_type ) >= time() );
}
/**
* Set a lock.
*
* To prevent race conditions, implementations should avoid setting the lock if the lock is already held.
*
* @param string $lock_type A string to identify different lock types.
* @return bool
*/
abstract public function set( $lock_type );
/**
* If a lock is set, return the timestamp it was set to expiry.
*
* @param string $lock_type A string to identify different lock types.
* @return bool|int False if no lock is set, otherwise the timestamp for when the lock is set to expire.
*/
abstract public function get_expiration( $lock_type );
/**
* Get the amount of time to set for a given lock. 60 seconds by default.
*
* @param string $lock_type A string to identify different lock types.
* @return int
*/
protected function get_duration( $lock_type ) {
return apply_filters( 'action_scheduler_lock_duration', self::$lock_duration, $lock_type );
}
/**
* @return ActionScheduler_Lock
*/
public static function instance() {
if ( empty( self::$locker ) ) {
$class = apply_filters( 'action_scheduler_lock_class', 'ActionScheduler_OptionLock' );
self::$locker = new $class();
}
return self::$locker;
}
}