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

h4kuna / critical-cache / 21130763308

19 Jan 2026 08:40AM UTC coverage: 95.214% (-0.09%) from 95.3%
21130763308

push

github

h4kuna
feat(update): new version
- UseOneTimeService support validFrom
- use LogicException
- remove old dependency Nette/Caching

29 of 33 new or added lines in 6 files covered. (87.88%)

1 existing line in 1 file now uncovered.

378 of 397 relevant lines covered (95.21%)

0.95 hits per line

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

97.56
/src/Services/ValidService.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\CriticalCache\Services;
4

5
use DateInterval;
6
use DateTimeImmutable;
7
use DateTimeInterface;
8
use h4kuna\CriticalCache\Contracts\ValidServiceContract;
9
use h4kuna\CriticalCache\PSR16\Expire;
10
use h4kuna\CriticalCache\Utils\DateRangeStore;
11
use Psr\Clock\ClockInterface;
12
use Psr\SimpleCache\CacheInterface;
13

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

26
        public function isValid(string $key): bool
1✔
27
        {
28
                ['from' => $from, 'to' => $to] = $this->decode($key);
1✔
29

30
                return $this->isValidCondition($from, $to);
1✔
31
        }
32

33
        public function value(string $key): ?string
1✔
34
        {
35
                ['from' => $from, 'to' => $to, 'v' => $value] = $this->decode($key);
1✔
36

37
                return $this->isValidCondition($from, $to) ? $value : null;
1✔
38
        }
39

40
        /**
41
         * @return TypeRange
42
         */
43
        private function decode(string $key): array
1✔
44
        {
45
                $value = $this->cache->get($key);
1✔
46
                if (is_array($value) === false) {
1✔
47
                        $value = [];
1✔
48
                }
49
                /** @var TypeSave $value */
50
                return DateRangeStore::decode($value);
1✔
51
        }
52

53
        public function from(string $key): ?DateTimeImmutable
1✔
54
        {
55
                return $this->decode($key)['from'];
1✔
56
        }
57

58
        public function remove(string $key): void
1✔
59
        {
60
                $this->cache->delete($key);
1✔
61
        }
1✔
62

63
        public function to(string $key): ?DateTimeImmutable
1✔
64
        {
65
                return $this->decode($key)['to'];
1✔
66
        }
67

68
        public function set(
1✔
69
                string $key,
70
                int|DateInterval|DateTimeInterface $validTo,
71
                int|DateInterval|DateTimeInterface|null $validFrom = null,
72
                string $value = '',
73
        ): void {
74

75
                $validFromDate = Expire::toDate($validFrom, $this->clock);
1✔
76
                $validToDate = Expire::toDate($validTo, $this->clock);
1✔
77

78
                $now = $this->clock->now();
1✔
79
                if ($validFromDate !== null) {
1✔
80
                        $diffFrom = $validFromDate->getTimestamp() - $now->getTimestamp();
1✔
81
                        if ($diffFrom > 0) {
1✔
82
                                $validToDate = $now->setTimestamp($validToDate->getTimestamp() + $diffFrom);
1✔
83
                        }
84
                }
85

86
                $this->cache->set($key, $this->encode($validFromDate, $validToDate, $value), $now->diff($validToDate));
1✔
87
        }
1✔
88

89
        /**
90
         * @return TypeSave
91
         */
92
        private function encode(?DateTimeInterface $from, DateTimeInterface $to, string $value): array
1✔
93
        {
94
                return DateRangeStore::encode($from, $to, $value);
1✔
95
        }
96

97
        private function isValidCondition(?DateTimeImmutable $from, ?DateTimeImmutable $to): bool
1✔
98
        {
99
                if ($from === null && $to === null) {
1✔
100
                        return false;
1✔
101
                }
102

103
                $now = $this->clock->now();
1✔
104
                if ($from !== null && $to !== null) {
1✔
105
                        return $from < $now && $now <= $to;
1✔
106
                }
107

108
                if ($from === null) {
1✔
109
                        return $now <= $to;
1✔
110
                }
111

NEW
112
                return $from < $now;
×
113
        }
114
}
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