Util   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 23
c 0
b 0
f 0
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateUuid() 0 22 6
A validateUuid() 0 10 3
1
<?php
2
3
namespace Bmatovu\Uuid\Support;
4
5
use Ramsey\Uuid\Exception\InvalidUuidStringException;
6
use Ramsey\Uuid\Uuid;
7
use Ramsey\Uuid\UuidInterface;
8
9
class Util
10
{
11
    /**
12
     * Generate UUID by version.
13
     *
14
     * @param int                               $version UUID Version
15
     * @param string|\Ramsey\Uuid\UuidInterface $ns The UUID namespace in which to create the named UUID
16
     * @param string                            $name The name to create a UUID for
17
     *
18
     * @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException
19
     *
20
     * @return \Ramsey\Uuid\UuidInterface UUID
21
     */
22 5
    public static function generateUuid(int $version = 4, $ns = null, string $name = null): UuidInterface
23
    {
24 5
        $ns = $ns ? $ns : Uuid::NAMESPACE_DNS;
25
26 5
        $name = $name ? $name : php_uname('n');
27
28
        switch ($version) {
29 5
            case 1:
30 1
                $uuid = Uuid::uuid1();
31 1
            break;
32 5
            case 3:
33 1
                $uuid = Uuid::uuid3($ns, $name);
34 1
            break;
35 5
            case 5:
36 1
                $uuid = Uuid::uuid5($ns, $name);
37 1
            break;
38
            default:
39 5
                $uuid = Uuid::uuid4();
40 5
            break;
41
        }
42
43 5
        return $uuid;
44
    }
45
46
    /**
47
     * Validate UUID plus version.
48
     *
49
     * @param  string   $str
50
     * @param  int|null $version UUID Version
51
     *
52
     * @return bool True for valid.
53
     */
54 1
    public static function validateUuid(string $str, int $version = null)
55
    {
56 1
        if ($version === null) {
57 1
            return Uuid::isValid($str);
58
        }
59
60
        try {
61 1
            return Uuid::fromString($str)->getVersion() === $version;
0 ignored issues
show
Deprecated Code introduced by
The function Ramsey\Uuid\DeprecatedUuidInterface::getVersion() has been deprecated: Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getVersion()}. ( Ignorable by Annotation )

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

61
            return /** @scrutinizer ignore-deprecated */ Uuid::fromString($str)->getVersion() === $version;

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

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

Loading history...
62 1
        } catch (InvalidUuidStringException $e) {
63 1
            return false;
64
        }
65
    }
66
}
67