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

h4kuna / critical-cache / 12212071516

07 Dec 2024 10:18AM UTC coverage: 94.362% (+0.3%) from 94.035%
12212071516

push

github

h4kuna
feat(UniqueHashQueueService): add new service for generate unique values, witch check mechanism

58 of 60 new or added lines in 3 files covered. (96.67%)

318 of 337 relevant lines covered (94.36%)

0.94 hits per line

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

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

3
namespace h4kuna\CriticalCache\Services;
4

5
use h4kuna\CriticalCache\Contracts\UniqueDataStoreServiceContract;
6
use h4kuna\CriticalCache\Contracts\UniqueValuesGeneratorServiceContract;
7
use h4kuna\CriticalCache\Interfaces\CheckUniqueValueInterface;
8
use h4kuna\CriticalCache\PSR16\CacheLocking;
9
use Psr\SimpleCache\CacheInterface;
10

11
final readonly class UniqueHashQueueService implements UniqueDataStoreServiceContract
12
{
13
        public function __construct(
1✔
14
                private CacheLocking $cache,
15
                private UniqueValuesGeneratorServiceContract $uniqueValuesGeneratorService,
16
        ) {
17
        }
1✔
18

19
        public function execute(
1✔
20
                CheckUniqueValueInterface $checkUniqueValue,
21
                int $queueSize = 100,
22
                ?callable $randomizer = null,
23
                int $tries = 3,
24
        ): string {
25
                $cacheKey = $checkUniqueValue::class;
1✔
26
                return $this->cache->synchronized($cacheKey, function (CacheInterface $cache) use (
1✔
27
                        $randomizer,
1✔
28
                        $checkUniqueValue,
1✔
29
                        $cacheKey,
1✔
30
                        $queueSize,
1✔
31
                        $tries,
1✔
32
                ): string {
33
                        /** @var list<non-empty-string> $values */
34
                        $values = $cache->get($cacheKey) ?? [];
1✔
35
                        if ($values === []) {
1✔
36
                                $values = $this->uniqueValuesGeneratorService->execute($checkUniqueValue, $queueSize, $randomizer, $tries);
1✔
37
                        }
38

39
                        $value = array_pop($values);
1✔
40
                        $cache->set($cacheKey, $values);
1✔
41

42
                        return $value;
1✔
43
                });
1✔
44
        }
45

46
        public function count(CheckUniqueValueInterface $checkUniqueValue): int
1✔
47
        {
48
                $values = $this->cache->get($checkUniqueValue::class);
1✔
49
                if (is_array($values) === false) {
1✔
NEW
50
                        return 0;
×
51
                }
52

53
                return count($values);
1✔
54
        }
55

56
        public function saveNewBatch(
1✔
57
                CheckUniqueValueInterface $checkUniqueValue,
58
                int $queueSize = 100,
59
                ?callable $randomizer = null,
60
                int $tries = 3,
61
        ): void {
62
                $this->cache->set(
1✔
63
                        $checkUniqueValue::class,
1✔
64
                        $this->uniqueValuesGeneratorService->execute($checkUniqueValue, $queueSize, $randomizer, $tries),
1✔
65
                );
66
        }
1✔
67
}
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