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

ShortUuidAggregateIdentity::fromString()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 3
nc 2
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 PascalDeVink\ShortUuid\ShortUuid;
18
use Ramsey\Uuid\Uuid;
19
20
/**
21
 * Base immutable short UUID aggregate identity.
22
 */
23
class ShortUuidAggregateIdentity extends AbstractAggregateIdentity
24
{
25
    /**
26
     * {@inheritdoc}
27
     *
28
     * @throws InvalidIdentityException
29
     */
30
    public static function fromString(string $value)
31
    {
32
        $uuid = (new ShortUuid())->decode($value);
33
        if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
34
            throw new InvalidIdentityException(
35
                \sprintf('Provided identity value "%s" is not a valid short UUID', $value)
36
            );
37
        }
38
39
        return new static($value);
40
    }
41
}
42