Skip to content

Commit

Permalink
Simplify TimeKeeper (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai authored Jun 17, 2023
1 parent 1cbb522 commit 1247df0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/Helper/TimeKeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

namespace Automattic\SlidingWindowCounter\Helper;

interface TimeKeeper
use function time;

class TimeKeeper
{
/**
* A mockable time-getter for the current time.
*/
public function getCurrentUnixTime(): int;
public function getCurrentUnixTime(): int
{
return time();
}
}
10 changes: 2 additions & 8 deletions src/SlidingWindowCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use function is_int;
use function Pipeline\take;
use function sprintf;
use function time;

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

/** @var \Automattic\SlidingWindowCounter\Cache\CounterCache The counter cache instance */
/** @var Cache\CounterCache The counter cache instance */
private Cache\CounterCache $counter_cache;

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

// Optional dependencies
$this->time_keeper = $time_keeper ?? new class() implements Helper\TimeKeeper {
public function getCurrentUnixTime(): int
{
return time();
}
};
$this->time_keeper = $time_keeper ?? new Helper\TimeKeeper();

$this->frame_builder = $frame_builder ?? new Helper\FrameBuilder($window_size, $observation_period, $this->time_keeper);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Helper/FakeTimeKeeper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use Automattic\SlidingWindowCounter\Helper\TimeKeeper;

class FakeTimeKeeper implements TimeKeeper
class FakeTimeKeeper extends TimeKeeper
{
private int $time;

Expand Down

0 comments on commit 1247df0

Please sign in to comment.