Completed
Push — master ( 4e0b70...c818a0 )
by Peter
01:42 queued 12s
created

TimeBinaryGenerator::__construct()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 55

Duplication

Lines 6
Ratio 10.91 %

Code Coverage

Tests 27
CRAP Score 11

Importance

Changes 0
Metric Value
dl 6
loc 55
ccs 27
cts 27
cp 1
rs 6.8351
c 0
b 0
f 0
cc 11
nc 11
nop 3
crap 11

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\BitmapOverflowException;
16
use GpsLab\Component\Base64UID\Exception\ProcessorArchitectureException;
17
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
18
19
class TimeBinaryGenerator implements BinaryGenerator
20
{
21
    /**
22
     * @var int
23
     */
24
    private $suffix_length;
25
26
    /**
27
     * @var int
28
     */
29
    private $time_length;
30
31
    /**
32
     * @var int
33
     */
34
    private $time_offset;
35
36
    /**
37
     * @var int
38
     */
39
    private $prefix_max_value;
40
41
    /**
42
     * @var int
43
     */
44
    private $suffix_max_value;
45
46
    /**
47
     * Bitmap of a random prefix + time + random suffix.
48
     *
49
     * The time length defines the limit of the stored date:
50
     *  40-bits = 1111111111111111111111111111111111111111      = 1099511627775  = 2004-11-03 19:53:48 (UTC)
51
     *  41-bits = 11111111111111111111111111111111111111111     = 2199023255551  = 2039-09-07 15:47:36 (UTC)
52
     *  42-bits = 111111111111111111111111111111111111111111    = 4398046511103  = 2109-05-15 07:35:11 (UTC)
53
     *  43-bits = 1111111111111111111111111111111111111111111   = 8796093022207  = 2248-09-26 15:10:22 (UTC)
54
     *  44-bits = 11111111111111111111111111111111111111111111  = 17592186044415 = 2527-06-23 06:20:44 (UTC)
55
     *  45-bits = 111111111111111111111111111111111111111111111 = 35184372088831 = 3084-12-12 12:41:29 (UTC)
56
     *
57
     * The time offset allows to move the starting point of time in microseconds,
58
     * which reduces the size of the stored time:
59
     *  0             = 1970-01-01 00:00:00 (UTC)
60
     *  1577836800000 = 2020-01-01 00:00:00 (UTC)
61
     *
62
     * @param int $prefix_length
63
     * @param int $time_length
64
     * @param int $time_offset
65
     */
66 17
    public function __construct($prefix_length = 9, $time_length = 45, $time_offset = 0)
67
    {
68
        // @codeCoverageIgnoreStart
69
        // can't reproduce this condition in tests
70 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...
71
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
72
        }
73
        // @codeCoverageIgnoreEnd
74
75 17
        if (!is_int($prefix_length)) {
76 1
            throw new ArgumentTypeException(sprintf('Length of prefix for UID should be integer, got "%s" instead.', gettype($prefix_length)));
77
        }
78
79 16
        if (!is_int($time_length)) {
80 1
            throw new ArgumentTypeException(sprintf('Length of time for UID should be integer, got "%s" instead.', gettype($time_length)));
81
        }
82
83 15
        if (!is_int($time_offset)) {
84 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset)));
85
        }
86
87 14
        if ($prefix_length < 0) {
88 1
            throw new ZeroArgumentException(sprintf('Length of prefix for UID should be grate then or equal to "0", got "%d" instead.', $prefix_length));
89
        }
90
91 13
        if ($time_length < 0) {
92 1
            throw new ZeroArgumentException(sprintf('Length of time for UID should be grate then or equal to "0", got "%d" instead.', $time_length));
93
        }
94
95 12
        if ($time_offset < 0) {
96 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then or equal to "0", got "%d" instead.', $time_offset));
97
        }
98
99 11 View Code Duplication
        if ($prefix_length + $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...
100 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, $prefix_length + $time_length));
101
        }
102
103 9
        $now = (int) floor(microtime(true) * 1000);
104
105 9
        if ($time_offset > $now) {
106 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
107
        }
108
109 8
        $min_time_length = strlen(decbin($now - $time_offset));
110
111 8
        if ($time_length < $min_time_length) {
112 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_length));
113
        }
114
115 6
        $this->time_length = $time_length;
116 6
        $this->time_offset = $time_offset;
117 6
        $this->suffix_length = 64 - 1 - $prefix_length - $time_length;
118 6
        $this->prefix_max_value = (int) bindec(str_repeat('1', $prefix_length));
119 6
        $this->suffix_max_value = (int) bindec(str_repeat('1', $this->suffix_length));
120 6
    }
121
122
    /**
123
     * @return int
124
     */
125 6
    public function generate()
126
    {
127 6
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
128
129
        // @codeCoverageIgnoreStart
130
        // overflow validation is in the constructor,
131
        // but there is a chance that overflow will occur in the process of using this service
132
        if ($time >= 1 << $this->time_length) {
133
            throw new BitmapOverflowException(sprintf('Bitmap for time is overflow of %d bits.', $this->time_length));
134
        }
135
        // @codeCoverageIgnoreEnd
136
137 6
        $prefix = random_int(0, $this->prefix_max_value);
138 6
        $suffix = random_int(0, $this->suffix_max_value);
139
140 6
        $uid = 1 << 64 - 1; // first bit is a bitmap limiter
141 6
        $uid |= $prefix << $this->time_length + $this->suffix_length;
142 6
        $uid |= $time << $this->suffix_length;
143 6
        $uid |= $suffix;
144
145 6
        return $uid;
146
    }
147
}
148