Completed
Push — master ( 106757...7f586e )
by Julián
08:01
created

Base62UuidIdentity::fromString()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 9.1448
c 0
b 0
f 0
cc 5
nc 4
nop 1
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 Ramsey\Uuid\Exception\InvalidUuidStringException;
18
use Ramsey\Uuid\Uuid;
19
use Tuupola\Base62;
20
21
/**
22
 * Base62 UUID identity.
23
 */
24
class Base62UuidIdentity extends AbstractIdentity
25
{
26
    private const UUID_HEX_LENGTH = 32;
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @throws InvalidIdentityException
32
     */
33
    final public static function fromString(string $value)
34
    {
35
        $decoded = \bin2hex((new Base62())->decode($value));
36
        if (\strlen($decoded) !== static::UUID_HEX_LENGTH) {
37
            throw new InvalidIdentityException(
38
                \sprintf('Provided identity value "%s" is not a valid bas62 UUID', $value)
39
            );
40
        }
41
42
        try {
43
            $uuid = Uuid::fromString(
44
                \sprintf('%s%s-%s-%s-%s-%s%s%s', ...\str_split($decoded, 4))
45
            );
46
        } catch (InvalidUuidStringException $exception) {
47
            throw new InvalidIdentityException(
48
                \sprintf('Provided identity value "%s" is not a valid bas62 UUID', $value),
49
                0,
50
                $exception
51
            );
52
        }
53
54
        if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
55
            throw new InvalidIdentityException(
56
                \sprintf('Provided identity value "%s" is not a valid bas62 UUID', $value)
57
            );
58
        }
59
60
        return new static($value);
61
    }
62
63
    /**
64
     * Get identity from UUID string.
65
     *
66
     * @param string $value
67
     *
68
     * @return mixed|static
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Base62UuidIdentity.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
69
     */
70
    final public static function fromUuid(string $value)
71
    {
72
        try {
73
            $uuid = Uuid::fromString($value);
74
        } catch (InvalidUuidStringException $exception) {
75
            throw new InvalidIdentityException(
76
                \sprintf('Provided identity value "%s" is not a valid UUID', $value),
77
                0,
78
                $exception
79
            );
80
        }
81
82
        if ($uuid->getVariant() !== Uuid::RFC_4122 || !\in_array($uuid->getVersion(), \range(1, 5), true)) {
83
            throw new InvalidIdentityException(
84
                \sprintf('Provided identity value "%s" is not a valid UUID', $value)
85
            );
86
        }
87
88
        /** @var string $binaryUuid */
89
        $binaryUuid = \hex2bin(\str_replace('-', '', $uuid->toString()));
0 ignored issues
show
Deprecated Code introduced by
The method Ramsey\Uuid\UuidInterface::toString() has been deprecated with message: In ramsey/uuid 4.0.0, this method will be replaced with the __toString() magic method, which is currently available in the Uuid concrete class. The new recommendation is to cast Uuid objects to string, rather than calling `toString()`.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
90
91
        return new static((new Base62())->encode($binaryUuid));
92
    }
93
}
94