Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
UuidIdGenerator
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 next
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace LambdaTwelve\OneRecord\Server;
6
7use LambdaTwelve\OneRecord\Model\Uuid;
8use LambdaTwelve\OneRecord\Server\Spi\IdGenerator;
9
10/**
11 * Random UUIDs (version 4) from an injectable byte source, so tests can be
12 * deterministic.
13 */
14final class UuidIdGenerator implements IdGenerator
15{
16    /** @var callable(int): string */
17    private $randomBytes;
18
19    /**
20     * @param ?callable(int): string $randomBytes defaults to random_bytes
21     */
22    public function __construct(?callable $randomBytes = null)
23    {
24        $this->randomBytes = $randomBytes ?? static fn(int $length): string => random_bytes(max(1, $length));
25    }
26
27    public function next(): string
28    {
29        return Uuid::v4($this->randomBytes);
30    }
31}