Skip to content

Commit 1247df0

Browse files
authored
Simplify TimeKeeper (#3)
1 parent 1cbb522 commit 1247df0

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

src/Helper/TimeKeeper.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@
2020

2121
namespace Automattic\SlidingWindowCounter\Helper;
2222

23-
interface TimeKeeper
23+
use function time;
24+
25+
class TimeKeeper
2426
{
2527
/**
2628
* A mockable time-getter for the current time.
2729
*/
28-
public function getCurrentUnixTime(): int;
30+
public function getCurrentUnixTime(): int
31+
{
32+
return time();
33+
}
2934
}

src/SlidingWindowCounter.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
use function is_int;
2727
use function Pipeline\take;
2828
use function sprintf;
29-
use function time;
3029

3130
/**
3231
* Sliding window counter and time series.
@@ -42,7 +41,7 @@ class SlidingWindowCounter
4241
/** @var int Maximum number of seconds for the buckets to last in cache. */
4342
private int $observation_period;
4443

45-
/** @var \Automattic\SlidingWindowCounter\Cache\CounterCache The counter cache instance */
44+
/** @var Cache\CounterCache The counter cache instance */
4645
private Cache\CounterCache $counter_cache;
4746

4847
/** @var Helper\TimeKeeper The timekeeper instance. */
@@ -91,12 +90,7 @@ public function __construct(
9190
$this->counter_cache = $counter_cache;
9291

9392
// Optional dependencies
94-
$this->time_keeper = $time_keeper ?? new class() implements Helper\TimeKeeper {
95-
public function getCurrentUnixTime(): int
96-
{
97-
return time();
98-
}
99-
};
93+
$this->time_keeper = $time_keeper ?? new Helper\TimeKeeper();
10094

10195
$this->frame_builder = $frame_builder ?? new Helper\FrameBuilder($window_size, $observation_period, $this->time_keeper);
10296
}

tests/Helper/FakeTimeKeeper.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
use Automattic\SlidingWindowCounter\Helper\TimeKeeper;
2424

25-
class FakeTimeKeeper implements TimeKeeper
25+
class FakeTimeKeeper extends TimeKeeper
2626
{
2727
private int $time;
2828

0 commit comments

Comments
 (0)