AbstractUuidIdentity   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 2
b 0
f 0
dl 0
loc 48
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A uuidFromString() 0 15 2
A assertUuidVariant() 0 15 4
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 Ramsey\Uuid\UuidInterface;
20
21
/**
22
 * Immutable UUID based identity.
23
 */
24
abstract class AbstractUuidIdentity extends AbstractIdentity
25
{
26
    /**
27
     * @param string $uuidString
28
     *
29
     * @throws InvalidUuidStringException
30
     *
31
     * @return UuidInterface
32
     */
33
    final protected static function uuidFromString(string $uuidString): UuidInterface
34
    {
35
        try {
36
            $uuid = Uuid::fromString($uuidString);
37
        } catch (InvalidUuidStringException $exception) {
38
            throw new InvalidIdentityException(
39
                \sprintf('Provided identity value "%s" is not a valid UUID.', $uuidString),
40
                0,
41
                $exception
42
            );
43
        }
44
45
        static::assertUuidVariant($uuid);
46
47
        return $uuid;
48
    }
49
50
    /**
51
     * Assert correct UUID variant.
52
     *
53
     * @param UuidInterface $uuid
54
     *
55
     * @throws InvalidUuidStringException
56
     */
57
    final protected static function assertUuidVariant(UuidInterface $uuid): void
58
    {
59
        if (\interface_exists('Ramsey\Uuid\DeprecatedUuidInterface')) {
60
            /** @var \Ramsey\Uuid\Rfc4122\FieldsInterface $fields */
61
            $fields = $uuid->getFields();
0 ignored issues
show
Bug introduced by
The method getFields() does not exist on Ramsey\Uuid\UuidInterface. Did you maybe mean getFieldsHex()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

61
            /** @scrutinizer ignore-call */ 
62
            $fields = $uuid->getFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
62
            $variant = $fields->getVariant();
63
            $version = $fields->getVersion();
64
        } else {
65
            $variant = $uuid->getVariant();
66
            $version = $uuid->getVersion();
67
        }
68
69
        if ($variant !== Uuid::RFC_4122 || !\in_array($version, \range(1, 6), true)) {
70
            throw new InvalidIdentityException(
71
                \sprintf('Provided identity value "%s" is not a valid UUID.', $uuid->toString())
72
            );
73
        }
74
    }
75
}
76