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

h4kuna / critical-cache / 12136248911

03 Dec 2024 09:00AM UTC coverage: 93.796% (+0.3%) from 93.511%
12136248911

push

github

h4kuna
fix(github actions)

257 of 274 relevant lines covered (93.8%)

0.94 hits per line

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

96.15
/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
 */
17
final class ValidService implements ValidServiceContract
18
{
19
        public function __construct(
1✔
20
                private readonly CacheInterface $cache,
21
                private readonly ClockInterface $clock,
22
        ) {
23
        }
1✔
24

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

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

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

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

39
        /**
40
         * @return TypeRange
41
         */
42
        private function decode(string $key): array
1✔
43
        {
44
                $value = $this->cache->get($key);
1✔
45
                if (is_string($value) === false) {
1✔
46
                        $value = '';
1✔
47
                }
48

49
                return DateRangeStore::decode($value);
1✔
50
        }
51

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

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

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

67
        public function set(
1✔
68
                string $key,
69
                int|DateInterval|DateTimeInterface $validTo,
70
                int|DateInterval|DateTimeInterface|null $validFrom = null,
71
                string $value = '',
72
        ): void {
73
                $dateTo = Expire::toDate($validTo, $this->clock);
1✔
74
                assert($dateTo instanceof DateTimeInterface);
75

76
                $this->cache->set($key, $this->encode($validFrom, $dateTo, $value), $this->clock->now()->diff($dateTo));
1✔
77
        }
1✔
78

79
        private function encode(int|DateInterval|DateTimeInterface|null $from, DateTimeInterface $to, string $value): string
1✔
80
        {
81
                return DateRangeStore::encode(Expire::toDate($from, $this->clock), $to, $value);
1✔
82
        }
83

84
        private function isValidCondition(?DateTimeImmutable $from, ?DateTimeImmutable $to): bool
1✔
85
        {
86
                return $to !== null && ($from === null || $from <= $this->clock->now());
1✔
87
        }
88
}
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

© 2025 Coveralls, Inc