Completed
Push — master ( 43c791...19c3cb )
by Peter
18s queued 11s
created

Base64UID::random()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 23.461

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 32
ccs 3
cts 14
cp 0.2143
crap 23.461
rs 8.7857
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Base64UID;
12
13
class Base64UID
14
{
15
    /**
16
     * @var string
17
     */
18
    private static $default_charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-';
19
20
    /**
21
     * @param int    $length
22
     * @param string $charset
23
     *
24
     * @return string
25
     */
26 5
    public static function generate($length = 10, $charset = '')
27
    {
28 5
        $charset = $charset ?: self::$default_charset;
29 5
        $charset_size = strlen($charset);
30 5
        $uid = '';
31 5
        while ($length-- > 0) {
32 4
            $uid .= $charset[random_int(0, $charset_size - 1)];
33
        }
34
35 5
        return $uid;
36
    }
37
}
38