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

h4kuna / critical-cache / 36120366469

25 Sep 2026 09:46AM UTC coverage: 95.195% (+0.2%) from 95.0%
36120366469

push

github

h4kuna
chore(ci): enforce static analysis and test on PHP 8.5

416 of 437 relevant lines covered (95.19%)

0.95 hits per line

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

96.55
/src/Services/UniqueHashQueueService.php
1
<?php declare(strict_types = 1);
2

3
namespace h4kuna\CriticalCache\Services;
4

5
use h4kuna\CriticalCache\Contracts\UniqueHashQueueServiceContract;
6
use h4kuna\CriticalCache\Contracts\UniqueValuesGeneratorServiceContract;
7
use h4kuna\CriticalCache\Interfaces\UniqueValueServiceInterface;
8
use h4kuna\CriticalCache\PSR16\CacheLocking;
9
use Psr\SimpleCache\CacheInterface;
10
use function array_pop;
11
use function count;
12
use function get_object_vars;
13
use function is_array;
14
use function serialize;
15

16
final readonly class UniqueHashQueueService implements UniqueHashQueueServiceContract
17
{
18

19
        public function __construct(
1 ✔
20
                private CacheLocking $cache,
21
                private UniqueValuesGeneratorServiceContract $uniqueValuesGeneratorService,
22
        )
23
        {
24
        }
1 ✔
25

26
        public function execute(
1 ✔
27
                UniqueValueServiceInterface $checkUniqueValue,
28
                ?object $dataSet = null,
29
        ): string
30
        {
31
                $cacheKey = self::makeCacheKey($checkUniqueValue, $dataSet);
1 ✔
32
                return $this->cache->synchronized($cacheKey, function (CacheInterface $cache) use (
1 ✔
33
                        $checkUniqueValue,
1 ✔
34
                        $cacheKey,
1 ✔
35
                        $dataSet,
1 ✔
36
                ): string {
37
                        /** @var list<non-empty-string> $values */
38
                        $values = $cache->get($cacheKey) ?? [];
1 ✔
39
                        if ($values === []) {
1 ✔
40
                                $values = $this->uniqueValuesGeneratorService->execute($checkUniqueValue, $dataSet);
1 ✔
41
                        }
42

43
                        $value = array_pop($values);
1 ✔
44
                        $cache->set($cacheKey, $values, $checkUniqueValue->ttl());
1 ✔
45

46
                        return $value;
1 ✔
47
                });
1 ✔
48
        }
49

50
        public function count(
1 ✔
51
                UniqueValueServiceInterface $checkUniqueValue,
52
                ?object $dataSet = null,
53
        ): int
54
        {
55
                $values = $this->cache->get(self::makeCacheKey($checkUniqueValue, $dataSet));
1 ✔
56
                if (is_array($values) === false) {
1 ✔
57
                        return 0;
×
58
                }
59

60
                return count($values);
1 ✔
61
        }
62

63
        public function saveNewBatch(
1 ✔
64
                UniqueValueServiceInterface $checkUniqueValue,
65
                ?object $dataSet = null,
66
        ): void
67
        {
68
                $this->cache->set(
1 ✔
69
                        self::makeCacheKey($checkUniqueValue, $dataSet),
1 ✔
70
                        $this->uniqueValuesGeneratorService->execute($checkUniqueValue, $dataSet),
1 ✔
71
                );
72
        }
1 ✔
73

74
        private static function makeCacheKey(
1 ✔
75
                UniqueValueServiceInterface $checkUniqueValue,
76
                ?object $dataSet,
77
        ): string
78
        {
79
                if ($dataSet === null) {
1 ✔
80
                        return $checkUniqueValue::class;
1 ✔
81
                }
82

83
                return $checkUniqueValue::class . serialize(get_object_vars($dataSet));
1 ✔
84
        }
85

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

© 2026 Coveralls, Inc