AbstractUuidIdentityGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getUuidString() 0 3 1
A getUuidGenerator() 0 11 2
1
<?php
2
3
/*
4
 * identity (https://github.com/phpgears/identity).
5
 * Identity objects for PHP.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/identity
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Identity;
15
16
use Ramsey\Uuid\Codec\TimestampFirstCombCodec;
17
use Ramsey\Uuid\Generator\CombGenerator;
18
use Ramsey\Uuid\UuidFactory;
19
use Ramsey\Uuid\UuidFactoryInterface;
20
21
/**
22
 * Abstract UUID identity generator.
23
 */
24
abstract class AbstractUuidIdentityGenerator implements IdentityGenerator
25
{
26
    /**
27
     * @var UuidFactoryInterface
28
     */
29
    private $uuidFactory;
30
31
    /**
32
     * Get UUID string.
33
     *
34
     * @return string
35
     */
36
    protected function getUuidString(): string
37
    {
38
        return $this->getUuidGenerator()->uuid4()->toString();
39
    }
40
41
    /**
42
     * Get UUID generator.
43
     *
44
     * @return UuidFactoryInterface
45
     */
46
    private function getUuidGenerator(): UuidFactoryInterface
47
    {
48
        if ($this->uuidFactory === null) {
49
            $this->uuidFactory = new UuidFactory();
50
            $this->uuidFactory->setCodec(new TimestampFirstCombCodec($this->uuidFactory->getUuidBuilder()));
51
            $this->uuidFactory->setRandomGenerator(
52
                new CombGenerator($this->uuidFactory->getRandomGenerator(), $this->uuidFactory->getNumberConverter())
53
            );
54
        }
55
56
        return $this->uuidFactory;
57
    }
58
}
59