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

FloatingTimeGenerator::__construct()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 41

Duplication

Lines 9
Ratio 21.95 %

Code Coverage

Tests 21
CRAP Score 9.0076

Importance

Changes 0
Metric Value
dl 9
loc 41
c 0
b 0
f 0
ccs 21
cts 22
cp 0.9545
rs 7.7084
cc 9
nc 9
nop 2
crap 9.0076
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\ArgumentRangeException;
14
use GpsLab\Component\Base64UID\Exception\ArgumentTypeException;
15
use GpsLab\Component\Base64UID\Exception\ProcessorArchitectureException;
16
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
17
18
class FloatingTimeGenerator implements BinaryGenerator
19
{
20
    /**
21
     * @var int
22
     */
23
    private $time_length;
24
25
    /**
26
     * @var int
27
     */
28
    private $time_offset;
29
30
    /**
31
     * Bitmap with floating time.
32
     *
33
     * The time length defines the limit of the stored date:
34
     *  40-bits = 1111111111111111111111111111111111111111      = 1099511627775  = 2004-11-03 19:53:47 (UTC)
35
     *  41-bits = 11111111111111111111111111111111111111111     = 2199023255551  = 2039-09-07 16:47:35 (UTC)
36
     *  42-bits = 111111111111111111111111111111111111111111    = 4398046511103  = 2109-05-15 08:35:11 (UTC)
37
     *  43-bits = 1111111111111111111111111111111111111111111   = 8796093022207  = 2248-09-26 16:10:22 (UTC)
38
     *  44-bits = 11111111111111111111111111111111111111111111  = 17592186044415 = 2527-06-23 07:20:44 (UTC)
39
     *  45-bits = 111111111111111111111111111111111111111111111 = 35184372088831 = 3084-12-12 12:41:28 (UTC)
40
     *
41
     * The time offset allows to move the starting point of time in microseconds,
42
     * which reduces the size of the stored time:
43
     *  0             = 1970-01-01 00:00:00 (UTC)
44
     *  1577833200000 = 2020-01-01 00:00:00 (UTC)
45
     *
46
     * @param int $time_length
47
     * @param int $time_offset
48
     */
49 15
    public function __construct($time_length = 45, $time_offset = 0)
50
    {
51 15 View Code Duplication
        if (PHP_INT_SIZE * 8 < 64) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
53
        }
54
55 15
        if (!is_int($time_length)) {
56 1
            throw new ArgumentTypeException(sprintf('Length of time for UID should be integer, got "%s" instead.', gettype($time_length)));
57
        }
58
59 14
        if (!is_int($time_offset)) {
60 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset)));
61
        }
62
63 13
        if ($time_length < 0) {
64 1
            throw new ZeroArgumentException(sprintf('Length of time for UID should be grate then or equal to "0", got "%d" instead.', $time_length));
65
        }
66
67 12
        if ($time_offset < 0) {
68 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then or equal to "0", got "%d" instead.', $time_length));
69
        }
70
71 11 View Code Duplication
        if ($time_length > 64 - 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72 2
            throw new ArgumentRangeException(sprintf('Length of time and prefix for UID should be less than or equal to "%d", got "%d" instead.', 64 - 1, $time_length));
73
        }
74
75 9
        $now = (int) floor(microtime(true) * 1000);
76
77 9
        if ($time_offset > $now) {
78 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
79
        }
80
81 8
        $min_time_length = strlen(decbin($now));
82
83 8 View Code Duplication
        if ($time_length < $min_time_length - $time_offset) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84 2
            throw new ArgumentRangeException(sprintf('Length of time for UID should be grate then or equal to "%d", got "%d" instead.', $min_time_length - $time_offset, $time_length));
85
        }
86
87 6
        $this->time_length = $time_length;
88 6
        $this->time_offset = $time_offset;
89 6
    }
90
91
    /**
92
     * @return int
93
     */
94 6
    public function generate()
95
    {
96 6
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
97
98 6
        $prefix_length = random_int(0, 64 - $this->time_length - 1);
99 6
        $prefix = random_int(0, (int) bindec(str_repeat('1', $prefix_length)));
100
101 6
        $suffix_length = 64 - $this->time_length - 1 - $prefix_length;
102 6
        $suffix = random_int(0, (int) bindec(str_repeat('1', $suffix_length)));
103
104 6
        $uid = 1 << 64 - 1; // first bit is a bitmap limiter
105 6
        $uid |= $prefix << $this->time_length + $suffix_length;
106 6
        $uid |= $time << $suffix_length;
107 6
        $uid |= $suffix;
108
109 6
        return $uid;
110
    }
111
}
112