• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / critical-cache / 21202020816

21 Jan 2026 08:10AM UTC coverage: 95.0% (-0.2%) from 95.214%
21202020816

push

github

h4kuna
feat(ValidityAwareCache): remove ValidService

73 of 78 new or added lines in 7 files covered. (93.59%)

380 of 400 relevant lines covered (95.0%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/src/Caching/ValidityAwareCache.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\CriticalCache\Caching;
4

5
use DateInterval;
6
use DateTimeInterface;
7
use h4kuna\CriticalCache\Caching\ValidityAware\TimeRangeEncoder;
8
use h4kuna\CriticalCache\Caching\ValidityAware\TimeRangeItem;
9
use h4kuna\CriticalCache\Contracts\ValidityAwareCacheContract;
10
use h4kuna\CriticalCache\Exceptions\LogicException;
11
use h4kuna\CriticalCache\PSR16\Expire;
12
use Psr\Clock\ClockInterface;
13
use Psr\SimpleCache\CacheInterface;
14

15
/**
16
 * @phpstan-import-type TypeSave from TimeRangeEncoder
17
 */
18
final readonly class ValidityAwareCache implements ValidityAwareCacheContract
19
{
20
        public function __construct(
1✔
21
                private CacheInterface $cache,
22
                private ClockInterface $clock,
23
                private TimeRangeEncoder $timeRangeEncoder,
24
        ) {
25
        }
1✔
26

27
        public function get(string $key): TimeRangeItem
1✔
28
        {
29
                /** @var TypeSave|null $value */
30
                $value = $this->cache->get($key);
1✔
31

32
                return $this->timeRangeEncoder->decode($value);
1✔
33
        }
34

35
        public function delete(string $key): void
1✔
36
        {
37
                $this->cache->delete($key);
1✔
38
        }
1✔
39

40
        public function set(
1✔
41
                string $key,
42
                int|DateInterval|DateTimeInterface $validTo,
43
                int|DateInterval|DateTimeInterface|null $validFrom = null,
44
                string $value = '',
45
        ): void {
46

47
                $validFromDate = Expire::toDate($validFrom, $this->clock);
1✔
48
                $validToDate = Expire::toDate($validTo, $this->clock);
1✔
49

50
                if ($validFromDate !== null && $validFromDate >= $validToDate) {
1✔
NEW
51
                        throw new LogicException('Parameters $from and $to are invalid, because $from is higher or equal to $to.');
×
52
                }
53

54
                $now = $this->clock->now();
1✔
55
                if ($validFromDate !== null) {
1✔
56
                        $diffFrom = $validFromDate->getTimestamp() - $now->getTimestamp();
1✔
57
                        if ($diffFrom > 0) {
1✔
58
                                $validToDate = $now->setTimestamp($validToDate->getTimestamp() + $diffFrom);
1✔
59
                        }
60
                }
61

62
                $this->cache->set($key, $this->timeRangeEncoder->encode($validFromDate, $validToDate, $value), $now->diff($validToDate));
1✔
63
        }
1✔
64

65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc