ShortUuidIdentity   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromUuid() 0 5 1
A fromString() 0 13 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 PascalDeVink\ShortUuid\ShortUuid;
18
19
/**
20
 * Short UUID identity.
21
 */
22
class ShortUuidIdentity extends AbstractUuidIdentity
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    final public static function fromString(string $value)
28
    {
29
        try {
30
            static::assertUuidVariant((new ShortUuid())->decode($value));
31
        } catch (\Exception $exception) {
32
            throw new InvalidIdentityException(
33
                \sprintf('Provided identity value "%s" is not a valid short UUID.', $value),
34
                0,
35
                $exception
36
            );
37
        }
38
39
        return new static($value);
40
    }
41
42
    /**
43
     * Get identity from UUID string.
44
     *
45
     * @param string $value
46
     *
47
     * @throws InvalidIdentityException
48
     *
49
     * @return mixed|static
50
     */
51
    final public static function fromUuid(string $value)
52
    {
53
        $uuid = static::uuidFromString($value);
54
55
        return new static((new ShortUuid())->encode($uuid));
56
    }
57
}
58