| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles time related tasks. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component; |
| 9 |
|
| 10 |
use Calotes\Base\Component; |
| 11 |
|
| 12 |
/** |
| 13 |
* Handles time related tasks. |
| 14 |
*/ |
| 15 |
class Timer extends Component { |
| 16 |
|
| 17 |
/** |
| 18 |
* The start time of the timer. |
| 19 |
* |
| 20 |
* @var int |
| 21 |
*/ |
| 22 |
protected $clock; |
| 23 |
|
| 24 |
/** |
| 25 |
* Constructor initializes and starts the timer. |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
$this->start(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Retrieves the maximum execution time allowed for scripts, halved. |
| 33 |
* |
| 34 |
* @return int The half of the maximum execution time in seconds. |
| 35 |
*/ |
| 36 |
public function get_max_time() { |
| 37 |
$max = ini_get( 'max_execution_time' ); |
| 38 |
if ( ! filter_var( $max, FILTER_VALIDATE_INT ) ) { |
| 39 |
$max = 30; |
| 40 |
} |
| 41 |
|
| 42 |
return $max / 2; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Starts or restarts the timer. |
| 47 |
* |
| 48 |
* @return void |
| 49 |
*/ |
| 50 |
public function start(): void { |
| 51 |
$this->clock = defender_get_current_time(); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Checks if the elapsed time has exceeded half of the maximum execution time. |
| 56 |
* |
| 57 |
* @return bool True if the current elapsed time is less than half of the max execution time, false otherwise. |
| 58 |
*/ |
| 59 |
public function check(): bool { |
| 60 |
if ( ( $this->get_difference() / 1000 ) >= $this->get_max_time() ) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Calculates the difference in seconds from when the timer was started to the current time. |
| 69 |
* |
| 70 |
* @return int The time difference in seconds. |
| 71 |
*/ |
| 72 |
public function get_difference(): int { |
| 73 |
return defender_get_current_time() - $this->clock; |
| 74 |
} |
| 75 |
} |
| 76 |
|