Uuid4::create()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4286
cc 1
eloc 4
nc 1
nop 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