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

h4kuna / critical-cache / 12212092232

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

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.0
/src/Services/UniqueValuesGeneratorService.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\CriticalCache\Services;
4

5
use h4kuna\CriticalCache\Contracts\RandomGeneratorContract;
6
use h4kuna\CriticalCache\Contracts\UniqueValuesGeneratorServiceContract;
7
use h4kuna\CriticalCache\Exceptions\GenerateUniqueDataFailedException;
8
use h4kuna\CriticalCache\Interfaces\CheckUniqueValueInterface;
9

10
final readonly class UniqueValuesGeneratorService implements UniqueValuesGeneratorServiceContract
11
{
12
        public function __construct(
1✔
13
                private RandomGeneratorContract $randomGenerator,
14
        ) {
15
        }
1✔
16

17
        public function execute(
1✔
18
                CheckUniqueValueInterface $checkUniqueColumnQuery,
19
                int $queueSize = 100,
20
                ?callable $randomizer = null,
21
                int $tries = 3,
22
        ): array {
23
                $randomizer ??= $this->randomGenerator->generate(...);
1✔
24
                $values = [];
1✔
25
                for ($i = 0; $i < $tries; ++$i) {
1✔
26
                        $values = $this->newRandomBatch($randomizer, $checkUniqueColumnQuery, $queueSize);
1✔
27
                        if ($values !== []) {
1✔
28
                                break;
1✔
29
                        }
30
                }
31
                if ($values === []) {
1✔
32
                        throw new GenerateUniqueDataFailedException(sprintf('Empty unique data, after "%s" tries.', $tries));
1✔
33
                }
34

35
                return $values;
1✔
36
        }
37

38
        /**
39
         * @param callable(): string $randomizer
40
         * @return list<non-empty-string>
41
         */
42
        private function newRandomBatch(
1✔
43
                callable $randomizer,
44
                CheckUniqueValueInterface $checkUniqueColumnQuery,
45
                int $size,
46
        ): array {
47
                $new = [];
1✔
48
                $i = 0;
1✔
49
                do {
50
                        $v = $randomizer();
1✔
51
                        if (isset($new[$v])) {
1✔
NEW
52
                                continue;
×
53
                        }
54
                        ++$i;
1✔
55
                        $new[$v] = $v;
1✔
56
                } while ($i < $size);
1✔
57

58
                foreach ($checkUniqueColumnQuery->execute($new) as $uuid) {
1✔
59
                        unset($new[$uuid]);
1✔
60
                }
61

62
                /** @var list<non-empty-string> $out */
63
                $out = array_values($new);
1✔
64

65
                return $out;
1✔
66
        }
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