Passed
Push — master ( 8e43c9...4981d1 )
by Tarmo
116:46 queued 51:34
created

UuidHelper::getType()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 6
nop 1
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 4
rs 9.9666
c 0
b 0
f 0
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
20
/**
21
 * Class UuidHelper
22
 *
23
 * @package App\Rest
24
 * @author TLe, Tarmo Leppänen <[email protected]>
25
 */
26
class UuidHelper
27
{
28
    private static ?UuidFactory $cache = null;
29
30
    /**
31
     * Getter method for UUID factory.
32
     */
33 838
    public static function getFactory(): UuidFactory
34
    {
35 838
        return self::$cache ??= self::initCache();
36
    }
37
38
    /**
39
     * Method to get proper doctrine parameter type for
40
     * UuidBinaryOrderedTimeType values.
41
     */
42 127
    public static function getType(string $value): ?string
43
    {
44 127
        $factory = self::getFactory();
45 127
        $output = null;
46
47
        try {
48 127
            $fields = $factory->fromString($value)->getFields();
49
50 83
            if ($fields instanceof FieldsInterface) {
51 83
                $output = $fields->getVersion() === 1 ? UuidBinaryOrderedTimeType::NAME : UuidBinaryType::NAME;
52
            }
53 44
        } catch (InvalidUuidStringException) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected ')', expecting '|' or T_VARIABLE on line 53 at column 43
Loading history...
54
            // ok, so now we know that value isn't uuid
55 44
        }
56
57
        return $output;
58 127
    }
59
60
    /**
61
     * Creates a UUID from the string standard representation
62
     */
63
    public static function fromString(string $value): UuidInterface
64 215
    {
65
        return self::getFactory()->fromString($value);
66 215
    }
67
68
    /**
69
     * Method to get bytes value for specified UuidBinaryOrderedTimeType value.
70
     */
71
    public static function getBytes(string $value): string
72 4
    {
73
        return self::fromString($value)->getBytes();
74 4
    }
75
76
    /**
77
     * Method to init UUID factory cache.
78
     *
79
     * @codeCoverageIgnore
80
     */
81
    private static function initCache(): UuidFactory
82
    {
83
        /** @var UuidFactory $factory */
84
        $factory = clone Uuid::getFactory();
85
        $codec = new OrderedTimeCodec($factory->getUuidBuilder());
86
        $factory->setCodec($codec);
87
88
        return $factory;
89
    }
90
}
91