Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| MutableClock | |
100.00% |
6 / 6 |
|
100.00% |
4 / 4 |
4 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| now | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setTime | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| advance | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Cache\Clock; |
| 16 | |
| 17 | /** |
| 18 | * Mutable clock class |
| 19 | * |
| 20 | * A clock whose time can be explicitly set and advanced, for deterministic testing of TTL/expiration behavior |
| 21 | * without real sleep() calls. |
| 22 | * |
| 23 | * @category Pop |
| 24 | * @package Pop\Cache |
| 25 | * @author Nick Sagona, III <nick@popphp.org> |
| 26 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 27 | * @license https://www.popphp.org/license New BSD License |
| 28 | * @version 5.0.0 |
| 29 | */ |
| 30 | class MutableClock implements ClockInterface |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Current time as a Unix timestamp |
| 35 | * @var int |
| 36 | */ |
| 37 | protected int $time; |
| 38 | |
| 39 | /** |
| 40 | * Constructor |
| 41 | * |
| 42 | * Instantiate the mutable clock object |
| 43 | * |
| 44 | * @param ?int $time |
| 45 | */ |
| 46 | public function __construct(?int $time = null) |
| 47 | { |
| 48 | $this->time = $time ?? time(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the current time as a Unix timestamp |
| 53 | * |
| 54 | * @return int |
| 55 | */ |
| 56 | public function now(): int |
| 57 | { |
| 58 | return $this->time; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Set the current time |
| 63 | * |
| 64 | * @param int $time |
| 65 | * @return MutableClock |
| 66 | */ |
| 67 | public function setTime(int $time): MutableClock |
| 68 | { |
| 69 | $this->time = $time; |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Advance the current time by a number of seconds |
| 75 | * |
| 76 | * @param int $seconds |
| 77 | * @return MutableClock |
| 78 | */ |
| 79 | public function advance(int $seconds): MutableClock |
| 80 | { |
| 81 | $this->time += $seconds; |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | } |