PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / trunk
Defender Security – Malware Scanner, Login Security & Firewall vtrunk
6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 3.1.1 All 140 releases
defender-security / src / component / class-timer.php

class-timer.php in Defender Security – Malware Scanner, Login Security & Firewall trunk, at src/component/class-timer.php

76 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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