UuidHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 64
ccs 15
cts 15
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFactory() 0 3 1
A initCache() 0 8 1
A fromString() 0 3 1
A getBytes() 0 3 1
A getType() 0 17 4
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * /src/Rest/UuidHelper.php
5
 *
6
 * @author TLe, Tarmo Leppänen <[email protected]>
7
 */
8
9
namespace App\Rest;
10
11
use Ramsey\Uuid\Codec\OrderedTimeCodec;
12
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
13
use Ramsey\Uuid\Doctrine\UuidBinaryType;
14
use Ramsey\Uuid\Exception\InvalidUuidStringException;
15
use Ramsey\Uuid\Rfc4122\FieldsInterface;
16
use Ramsey\Uuid\Uuid;
17
use Ramsey\Uuid\UuidFactory;
18
use Ramsey\Uuid\UuidInterface;
19
use function syslog;
20
21
/**
22
 * Class UuidHelper
23
 *
24
 * @package App\Rest
25
 * @author TLe, Tarmo Leppänen <[email protected]>
26
 */
27
class UuidHelper
28
{
29
    private static ?UuidFactory $cache = null;
30
31
    /**
32
     * Getter method for UUID factory.
33
     */
34 1005
    public static function getFactory(): UuidFactory
35
    {
36 1005
        return self::$cache ??= self::initCache();
37
    }
38
39
    /**
40
     * Method to get proper doctrine parameter type for
41
     * UuidBinaryOrderedTimeType values.
42
     */
43 112
    public static function getType(string $value): ?string
44
    {
45 112
        $factory = self::getFactory();
46 112
        $output = null;
47
48
        try {
49 112
            $fields = $factory->fromString($value)->getFields();
50
51 72
            if ($fields instanceof FieldsInterface) {
52 72
                $output = $fields->getVersion() === 1 ? UuidBinaryOrderedTimeType::NAME : UuidBinaryType::NAME;
53
            }
54 40
        } catch (InvalidUuidStringException $exception) {
55
            // ok, so now we know that value isn't uuid
56 40
            syslog(LOG_INFO, $exception->getMessage());
57
        }
58
59 112
        return $output;
60
    }
61
62
    /**
63
     * Creates a UUID from the string standard representation
64
     */
65 269
    public static function fromString(string $value): UuidInterface
66
    {
67 269
        return self::getFactory()->fromString($value);
68
    }
69
70
    /**
71
     * Method to get bytes value for specified UuidBinaryOrderedTimeType value.
72
     */
73 4
    public static function getBytes(string $value): string
74
    {
75 4
        return self::fromString($value)->getBytes();
76
    }
77
78
    /**
79
     * Method to init UUID factory cache.
80
     *
81
     * @codeCoverageIgnore
82
     */
83
    private static function initCache(): UuidFactory
84
    {
85
        /** @var UuidFactory $factory */
86
        $factory = clone Uuid::getFactory();
87
        $codec = new OrderedTimeCodec($factory->getUuidBuilder());
88
        $factory->setCodec($codec);
89
90
        return $factory;
91
    }
92
}
93