KsuidIdentity   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 19 3
1
<?php
2
3
/*
4
 * identity-extra (https://github.com/phpgears/identity-extra).
5
 * Identity objects for PHP.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/identity-extra
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Identity\Extra;
15
16
use Gears\Identity\AbstractIdentity;
17
use Gears\Identity\Exception\InvalidIdentityException;
18
use Tuupola\Ksuid;
19
use Tuupola\KsuidFactory;
20
21
/**
22
 * K-Sortable Globally Unique IDs identity.
23
 */
24
class KsuidIdentity extends AbstractIdentity
25
{
26
    /**
27
     * {@inheritdoc}
28
     *
29
     * @throws InvalidIdentityException
30
     */
31
    final public static function fromString(string $value)
32
    {
33
        if (\strlen($value) !== Ksuid::ENCODED_SIZE) {
34
            throw new InvalidIdentityException(
35
                \sprintf('Provided identity value "%s" is not a valid KSUID.', $value)
36
            );
37
        }
38
39
        try {
40
            KsuidFactory::fromString($value);
41
        } catch (\Exception $exception) {
42
            throw new InvalidIdentityException(
43
                \sprintf('Provided identity value "%s" is not a valid KSUID.', $value),
44
                0,
45
                $exception
46
            );
47
        }
48
49
        return new static($value);
50
    }
51
}
52