Completed
Pull Request — master (#8)
by Peter
01:19
created

RandomBytesGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Generator;
12
13
use GpsLab\Component\Base64UID\Exception\ArgumentTypeException;
14
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
15
16
class RandomBytesGenerator implements Generator
17
{
18
    /**
19
     * @var int
20
     */
21
    private $bytes_length;
22
23
    /**
24
     * @param int $bytes_length
25
     */
26 3 View Code Duplication
    public function __construct($bytes_length = 8)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28 3
        if (!is_int($bytes_length)) {
29 1
            throw new ArgumentTypeException(sprintf('Length of bytes should be integer, got "%s" instead.', gettype($bytes_length)));
30
        }
31
32 2
        if ($bytes_length <= 0) {
33 1
            throw new ZeroArgumentException(sprintf('Length of bytes should be grate then "0", got "%d" instead.', $bytes_length));
34
        }
35
36 1
        $this->bytes_length = $bytes_length;
37 1
    }
38
39
    /**
40
     * @return string
41
     */
42 1
    public function generate()
43
    {
44 1
        $uid = random_bytes($this->bytes_length);
45 1
        $uid = base64_encode($uid);
46 1
        $uid = str_replace('=', '', $uid);
47
48 1
        return $uid;
49
    }
50
}
51