Base62UuidIdentity   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromUuid() 0 8 1
A fromString() 0 15 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 Gears\Identity\Exception\InvalidIdentityException;
17
use Tuupola\Base62;
18
19
/**
20
 * Base62 UUID identity.
21
 */
22
class Base62UuidIdentity extends AbstractUuidIdentity
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    final public static function fromString(string $value)
28
    {
29
        try {
30
            $decoded = \bin2hex((new Base62())->decode($value));
31
32
            static::uuidFromString(\sprintf('%s%s-%s-%s-%s-%s%s%s', ...\str_split($decoded, 4)));
33
        } catch (\Exception $exception) {
34
            throw new InvalidIdentityException(
35
                \sprintf('Provided identity value "%s" is not a valid bas62 UUID.', $value),
36
                0,
37
                $exception
38
            );
39
        }
40
41
        return new static($value);
42
    }
43
44
    /**
45
     * Get identity from UUID string.
46
     *
47
     * @param string $value
48
     *
49
     * @throws InvalidIdentityException
50
     *
51
     * @return mixed|static
52
     */
53
    final public static function fromUuid(string $value)
54
    {
55
        $uuid = static::uuidFromString($value);
56
57
        /** @var string $binaryUuid */
58
        $binaryUuid = \hex2bin(\str_replace('-', '', $uuid->toString()));
59
60
        return new static((new Base62())->encode($binaryUuid));
61
    }
62
}
63