Skip to content

Commit ec2f32e

Browse files
authored
Chore: Replace clock (#37)
1 parent e612f97 commit ec2f32e

12 files changed

Lines changed: 177 additions & 72 deletions

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": "^8.0",
99
"rikudou/clock": "^1.0",
1010
"psr/simple-cache": "^1.0",
11-
"async-aws/dynamo-db": "^1.0"
11+
"async-aws/dynamo-db": "^1.0",
12+
"psr/clock": "^1.0"
1213
},
1314
"autoload": {
1415
"psr-4": {

src/Converter/DefaultCacheItemConverter.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,26 @@
33
namespace Rikudou\DynamoDbCache\Converter;
44

55
use Psr\Cache\CacheItemInterface;
6-
use Rikudou\Clock\Clock;
7-
use Rikudou\Clock\ClockInterface;
6+
use Psr\Clock\ClockInterface as PsrClock;
7+
use Rikudou\Clock\ClockInterface as RikudouClock;
88
use Rikudou\DynamoDbCache\DynamoCacheItem;
99
use Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface;
1010
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
11+
use Rikudou\DynamoDbCache\Helper\ClockHelper;
1112

1213
final class DefaultCacheItemConverter implements CacheItemConverterInterface
1314
{
1415
private CacheItemEncoderInterface $encoder;
1516

16-
private ClockInterface $clock;
17+
private PsrClock $clock;
1718

18-
public function __construct(?CacheItemEncoderInterface $encoder = null, ?ClockInterface $clock = null)
19+
public function __construct(?CacheItemEncoderInterface $encoder = null, RikudouClock|PsrClock|null $clock = null)
1920
{
2021
if ($encoder === null) {
2122
$encoder = new SerializeItemEncoder();
2223
}
23-
if ($clock === null) {
24-
$clock = new Clock();
25-
}
24+
$clock = ClockHelper::psrClock($clock);
25+
2626
$this->encoder = $encoder;
2727
$this->clock = $clock;
2828
}

src/DynamoCacheItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DateInterval;
66
use DateTimeInterface;
77
use Psr\Cache\CacheItemInterface;
8-
use Rikudou\Clock\ClockInterface;
8+
use Psr\Clock\ClockInterface;
99
use Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface;
1010

1111
final class DynamoCacheItem implements CacheItemInterface

src/DynamoDbCache.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
use LogicException;
1515
use Psr\Cache\CacheItemInterface;
1616
use Psr\Cache\CacheItemPoolInterface;
17+
use Psr\Clock\ClockInterface as PsrClock;
1718
use Psr\SimpleCache\CacheInterface;
18-
use Rikudou\Clock\Clock;
19-
use Rikudou\Clock\ClockInterface;
19+
use Rikudou\Clock\ClockInterface as RikudouClock;
2020
use Rikudou\DynamoDbCache\Converter\CacheItemConverterRegistry;
2121
use Rikudou\DynamoDbCache\Converter\DefaultCacheItemConverter;
2222
use Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface;
2323
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
2424
use Rikudou\DynamoDbCache\Enum\NetworkErrorMode;
2525
use Rikudou\DynamoDbCache\Exception\CacheItemNotFoundException;
2626
use Rikudou\DynamoDbCache\Exception\InvalidArgumentException;
27+
use Rikudou\DynamoDbCache\Helper\ClockHelper;
2728

2829
final class DynamoDbCache implements CacheItemPoolInterface, CacheInterface
2930
{
@@ -35,7 +36,7 @@ final class DynamoDbCache implements CacheItemPoolInterface, CacheInterface
3536
*/
3637
private array $deferred = [];
3738

38-
private ClockInterface $clock;
39+
private PsrClock $clock;
3940

4041
private CacheItemConverterRegistry $converter;
4142

@@ -47,16 +48,14 @@ public function __construct(
4748
private string $primaryField = 'id',
4849
private string $ttlField = 'ttl',
4950
private string $valueField = 'value',
50-
?ClockInterface $clock = null,
51+
RikudouClock|PsrClock|null $clock = null,
5152
?CacheItemConverterRegistry $converter = null,
5253
?CacheItemEncoderInterface $encoder = null,
5354
private ?string $prefix = null,
5455
#[ExpectedValues(valuesFromClass: NetworkErrorMode::class)]
5556
private int $networkErrorMode = NetworkErrorMode::DEFAULT,
5657
) {
57-
if ($clock === null) {
58-
$clock = new Clock();
59-
}
58+
$clock = ClockHelper::psrClock($clock);
6059
$this->clock = $clock;
6160

6261
if ($encoder === null) {

src/DynamoDbCacheBuilder.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use AsyncAws\DynamoDb\DynamoDbClient;
66
use JetBrains\PhpStorm\ExpectedValues;
7-
use Rikudou\Clock\ClockInterface;
7+
use Psr\Clock\ClockInterface as PsrClock;
8+
use Rikudou\Clock\ClockInterface as RikudouClock;
89
use Rikudou\DynamoDbCache\Converter\CacheItemConverterRegistry;
910
use Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface;
1011
use Rikudou\DynamoDbCache\Enum\NetworkErrorMode;
12+
use Rikudou\DynamoDbCache\Helper\ClockHelper;
1113

1214
final class DynamoDbCacheBuilder
1315
{
@@ -19,7 +21,7 @@ final class DynamoDbCacheBuilder
1921

2022
private ?string $prefix = null;
2123

22-
private ?ClockInterface $clock = null;
24+
private ?PsrClock $clock = null;
2325

2426
private ?CacheItemConverterRegistry $converterRegistry = null;
2527

@@ -70,8 +72,10 @@ public function withPrefix(?string $prefix): self
7072
return $copy;
7173
}
7274

73-
public function withClock(?ClockInterface $clock): self
75+
public function withClock(RikudouClock|PsrClock|null $clock): self
7476
{
77+
$clock = ClockHelper::psrClock($clock);
78+
7579
$copy = clone $this;
7680
$copy->clock = $clock;
7781

src/Helper/ClockHelper.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Rikudou\DynamoDbCache\Helper;
4+
5+
use DateTimeImmutable;
6+
use Psr\Clock\ClockInterface as PsrClock;
7+
use Rikudou\Clock\ClockInterface as RikudouClock;
8+
9+
/**
10+
* @internal
11+
*/
12+
final class ClockHelper
13+
{
14+
public static function psrClock(RikudouClock|PsrClock|null $clock = null): PsrClock
15+
{
16+
if ($clock instanceof RikudouClock) {
17+
trigger_error(sprintf('%s is deprecated, use %s instead', RikudouClock::class, PsrClock::class), E_USER_DEPRECATED);
18+
}
19+
20+
return new class($clock) implements PsrClock {
21+
public function __construct(
22+
private RikudouClock|PsrClock|null $clock = null,
23+
) {
24+
}
25+
26+
public function now(): DateTimeImmutable
27+
{
28+
if ($this->clock !== null) {
29+
return DateTimeImmutable::createFromInterface($this->clock->now());
30+
}
31+
32+
return new DateTimeImmutable();
33+
}
34+
};
35+
}
36+
37+
public static function fixedTimeClock(DateTimeImmutable $now): PsrClock
38+
{
39+
return new class($now) implements PsrClock {
40+
public function __construct(
41+
private DateTimeImmutable $now,
42+
) {
43+
}
44+
45+
public function now(): DateTimeImmutable
46+
{
47+
return $this->now;
48+
}
49+
};
50+
}
51+
}

tests/Converter/DefaultCacheItemConverterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use PHPUnit\Framework\TestCase;
66
use Psr\Cache\CacheItemInterface;
7-
use Rikudou\Clock\Clock;
87
use Rikudou\DynamoDbCache\Converter\DefaultCacheItemConverter;
98
use Rikudou\DynamoDbCache\DynamoCacheItem;
109
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
10+
use Rikudou\DynamoDbCache\Helper\ClockHelper;
1111

1212
final class DefaultCacheItemConverterTest extends TestCase
1313
{
@@ -33,7 +33,7 @@ protected function setUp(): void
3333
true,
3434
'something',
3535
null,
36-
new Clock(),
36+
ClockHelper::psrClock(),
3737
new SerializeItemEncoder()
3838
);
3939
$this->basicCacheItem = $this->createBasicCacheItem();

tests/DynamoCacheItemTest.php

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
use DateInterval;
66
use DateTime;
7+
use DateTimeImmutable;
78
use PHPUnit\Framework\TestCase;
8-
use ReflectionObject;
9-
use Rikudou\Clock\Clock;
10-
use Rikudou\Clock\ClockInterface;
11-
use Rikudou\Clock\TestClock;
9+
use Psr\Clock\ClockInterface;
1210
use Rikudou\DynamoDbCache\DynamoCacheItem;
1311
use Rikudou\DynamoDbCache\Encoder\SerializeItemEncoder;
14-
use Rikudou\DynamoDbCache\Exception\InvalidArgumentException;
12+
use Rikudou\DynamoDbCache\Helper\ClockHelper;
1513

1614
final class DynamoCacheItemTest extends TestCase
1715
{
@@ -21,19 +19,13 @@ final class DynamoCacheItemTest extends TestCase
2119
private const DEFAULT_EXPIRES_AT = null;
2220
private const DEFAULT_DATE = '2030-01-01T15:00:00+00:00';
2321

24-
/**
25-
* @var DynamoCacheItem
26-
*/
27-
private $instance;
22+
private DynamoCacheItem $instance;
2823

29-
/**
30-
* @var TestClock
31-
*/
32-
private $clock;
24+
private ClockInterface $clock;
3325

3426
protected function setUp(): void
3527
{
36-
$this->clock = new TestClock(new DateTime(self::DEFAULT_DATE));
28+
$this->clock = ClockHelper::fixedTimeClock(new DateTimeImmutable(self::DEFAULT_DATE));
3729
$this->instance = new DynamoCacheItem(
3830
self::DEFAULT_KEY,
3931
self::DEFAULT_IS_HIT,
@@ -87,29 +79,11 @@ public function testExpiresAfter()
8779
self::assertNull($this->instance->getExpiresAt());
8880

8981
$this->instance->expiresAfter(new DateInterval('P1DT2H3M'));
90-
self::assertEquals('2030-01-02T17:04:00+00:00', $this->instance->getExpiresAt()->format('c'));
82+
self::assertEquals('2030-01-02T17:03:00+00:00', $this->instance->getExpiresAt()->format('c'));
9183
}
9284

9385
public function testGetRaw()
9486
{
9587
self::assertEquals(serialize(self::DEFAULT_VALUE), $this->instance->getRaw());
9688
}
97-
98-
public function testDefaultClock()
99-
{
100-
$instance = new DynamoCacheItem(
101-
self::DEFAULT_KEY,
102-
self::DEFAULT_IS_HIT,
103-
self::DEFAULT_VALUE,
104-
self::DEFAULT_EXPIRES_AT,
105-
new Clock(),
106-
new SerializeItemEncoder()
107-
);
108-
109-
$reflection = new ReflectionObject($instance);
110-
$clock = $reflection->getProperty('clock');
111-
$clock->setAccessible(true);
112-
113-
self::assertInstanceOf(ClockInterface::class, $clock->getValue($instance));
114-
}
11589
}

tests/DynamoDbCacheBuilderTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
namespace Rikudou\Tests\DynamoDbCache;
44

55
use AsyncAws\DynamoDb\DynamoDbClient;
6+
use DateTimeInterface;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Clock\ClockInterface;
69
use ReflectionObject;
7-
use Rikudou\Clock\Clock;
8-
use Rikudou\Clock\ClockInterface;
910
use Rikudou\DynamoDbCache\Converter\CacheItemConverterRegistry;
1011
use Rikudou\DynamoDbCache\DynamoDbCache;
1112
use Rikudou\DynamoDbCache\DynamoDbCacheBuilder;
12-
use PHPUnit\Framework\TestCase;
1313
use Rikudou\DynamoDbCache\Encoder\CacheItemEncoderInterface;
1414
use Rikudou\DynamoDbCache\Encoder\JsonItemEncoder;
1515
use Rikudou\DynamoDbCache\Enum\NetworkErrorMode;
16+
use Rikudou\DynamoDbCache\Helper\ClockHelper;
1617

1718
class DynamoDbCacheBuilderTest extends TestCase
1819
{
@@ -51,7 +52,7 @@ public function testImmutability()
5152
{
5253
$registry = new CacheItemConverterRegistry();
5354
$encoder = new JsonItemEncoder();
54-
$clock = new Clock();
55+
$clock = ClockHelper::psrClock();
5556

5657
self::assertNotSame($this->instance, $this->instance->withClock($clock));
5758
self::assertNotSame($this->instance, $this->instance->withConverterRegistry($registry));
@@ -171,15 +172,15 @@ public function testWithConverterRegistry()
171172

172173
public function testWithClock()
173174
{
174-
$clock = new Clock();
175+
$clock = ClockHelper::psrClock();
175176
$instance = $this->instance->withClock($clock);
176177

177178
$result = $this->getBuiltData($instance->build());
178179
self::assertEquals('test', $result['tableName']);
179180
self::assertEquals('id', $result['primaryField']);
180181
self::assertEquals('ttl', $result['ttlField']);
181182
self::assertEquals('value', $result['valueField']);
182-
self::assertSame($clock, $result['clock']);
183+
self::assertSame($clock->now()->format(DateTimeInterface::RFC7231), $result['clock']->now()->format(DateTimeInterface::RFC7231));
183184
self::assertInstanceOf(CacheItemConverterRegistry::class, $result['converter']);
184185
self::assertInstanceOf(CacheItemEncoderInterface::class, $result['encoder']);
185186
self::assertNull($result['prefix']);
@@ -188,7 +189,7 @@ public function testWithClock()
188189

189190
public function testAllAtOnce()
190191
{
191-
$clock = new Clock();
192+
$clock = ClockHelper::psrClock();
192193
$registry = new CacheItemConverterRegistry();
193194
$encoder = new JsonItemEncoder();
194195

@@ -208,7 +209,7 @@ public function testAllAtOnce()
208209
self::assertEquals('id1', $result['primaryField']);
209210
self::assertEquals('ttl1', $result['ttlField']);
210211
self::assertEquals('value1', $result['valueField']);
211-
self::assertSame($clock, $result['clock']);
212+
self::assertSame($clock->now()->format(DateTimeInterface::RFC7231), $result['clock']->now()->format(DateTimeInterface::RFC7231));
212213
self::assertSame($registry, $result['converter']);
213214
self::assertSame($encoder, $result['encoder']);
214215
self::assertEquals('testPrefix', $result['prefix']);

0 commit comments

Comments
 (0)