Completed
Push — master ( 3a35ab...03a056 )
by Julián
06:43
created

HashUuidAggregateIdentity::fromString()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
/*
4
 * aggregate (https://github.com/phpgears/aggregate).
5
 * Aggregate base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/aggregate
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Aggregate;
15
16
use Gears\Identity\Exception\InvalidIdentityException;
17
use Hashids\Hashids;
18
use Ramsey\Uuid\Exception\InvalidUuidStringException;
19
use Ramsey\Uuid\Uuid;
20
21
/**
22
 * Base immutable hashed UUID aggregate identity.
23
 */
24
class HashUuidAggregateIdentity extends AbstractAggregateIdentity
25
{
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws InvalidIdentityException
30
     */
31
    public static function fromString(string $value)
32
    {
33
        try {
34
            $uuid = Uuid::fromString((new Hashids())->decodeHex($value));
35
        } catch (InvalidUuidStringException $exception) {
36
            throw new InvalidIdentityException(
37
                \sprintf('Provided identity value "%s" is not a valid hashed UUID', $value),
38
                0,
39
                $exception
40
            );
41
        }
42
43
        if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
44
            throw new InvalidIdentityException(
45
                \sprintf('Provided identity value "%s" is not a valid hashed UUID', $value)
46
            );
47
        }
48
49
        return new static($value);
50
    }
51
}
52