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

RandomBinaryGenerator::generate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.9
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
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\Binary;
12
13
use GpsLab\Component\Base64UID\Exception\ArgumentTypeException;
14
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
15
16
class RandomBinaryGenerator implements BinaryGenerator
17
{
18
    /**
19
     * @var int
20
     */
21
    private $uid_bitmap_length;
22
23
    /**
24
     * @param int $uid_bitmap_length
25
     */
26 6 View Code Duplication
    public function __construct($uid_bitmap_length)
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 6
        if (!is_int($uid_bitmap_length)) {
29 1
            throw new ArgumentTypeException(sprintf('Length of bitmap for UID should be integer, got "%s" instead.', gettype($uid_bitmap_length)));
30
        }
31
32 5
        if ($uid_bitmap_length <= 0) {
33 2
            throw new ZeroArgumentException(sprintf('Length of bitmap for UID should be grate then "0", got "%d" instead.', $uid_bitmap_length));
34
        }
35
36 3
        $this->uid_bitmap_length = $uid_bitmap_length;
37 3
    }
38
39
    /**
40
     * @return int
41
     */
42 3
    public function generate()
43
    {
44 3
        $uid = 0;
45 3
        for ($i = 0; $i < $this->uid_bitmap_length; ++$i) {
46 3
            if (random_int(0, 1)) {
47 3
                $uid |= 1 << $i;
48
            }
49
        }
50
51 3
        return $uid;
52
    }
53
}
54