Uuid4   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 3
c 3
b 0
f 2
lcom 1
cbo 1
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A generateBytes() 0 9 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 2/14/15
5
 * Time: 11:05 AM
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Uuid\Versions;
12
13
use NilPortugues\Uuid\UuidInterface;
14
15
/**
16
 * Class Uuid4
17
 * @package NilPortugues\Uuid\Versions
18
 */
19
class Uuid4 extends AbstractUuid implements UuidInterface
20
{
21
    /**
22
     * @param string|null $namespace
23
     * @param string|null $name
24
     *
25
     * @return string
26
     */
27
    public static function create($namespace = null, $name = null)
28
    {
29
        $bytes = self::generateBytes(16);
30
        $hex   = \bin2hex($bytes);
31
32
        return self::toString(self::uuidFromHashedName($hex, 4));
33
    }
34
35
    /**
36
     * Generates random bytes for use in version 4 UUIDs
37
     *
38
     * @param int $length
39
     *
40
     * @return string
41
     */
42
    private static function generateBytes($length)
43
    {
44
        $bytes = '';
45
        for ($i = 1; $i <= $length; $i++) {
46
            $bytes = \chr(\mt_rand(0, 255)) . $bytes;
47
        }
48
49
        return $bytes;
50
    }
51
}
52