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

SnowflakeGenerator   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 164
Duplicated Lines 1.83 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 97.96%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 5
dl 3
loc 164
ccs 48
cts 49
cp 0.9796
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 3 82 16
A generate() 0 26 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 SnowflakeGenerator implements BinaryGenerator
20
{
21
    /**
22
     * @var int
23
     */
24
    private $generator;
25
26
    /**
27
     * @var int
28
     */
29
    private $generator_length;
30
31
    /**
32
     * @var int
33
     */
34
    private $sequence_length;
35
36
    /**
37
     * @var int
38
     */
39
    private $time_length;
40
41
    /**
42
     * @var int
43
     */
44
    private $time_offset;
45
46
    /**
47
     * @var int
48
     */
49
    private $last_time = 0;
50
51
    /**
52
     * @var int
53
     */
54
    private $sequence = 0;
55
56
    /**
57
     * Snowflake.
58
     *
59
     * The time offset allows to move the starting point of time in microseconds,
60
     * which reduces the size of the stored time:
61
     *  0             = 1970-01-01 00:00:00 (UTC)
62
     *  1577836800000 = 2020-01-01 00:00:00 (UTC)
63
     *
64
     * @param int $generator
65
     * @param int $generator_length
66
     * @param int $sequence_length
67
     * @param int $time_length
68
     * @param int $time_offset
69
     */
70 28
    public function __construct(
71
        $generator,
72
        $generator_length = 10,
73
        $sequence_length = 12,
74
        $time_length = 41,
75
        $time_offset = 1577836800000
76
    ) {
77
        // @codeCoverageIgnoreStart
78
        // can't reproduce this condition in tests
79 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...
80
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
81
        }
82
        // @codeCoverageIgnoreEnd
83
84 28
        if (!is_int($generator)) {
85 1
            throw new ArgumentTypeException(sprintf('Generator should be integer, got "%s" instead.', gettype($generator)));
86
        }
87
88 27
        if (!is_int($generator_length)) {
89 1
            throw new ArgumentTypeException(sprintf('Generator length should be integer, got "%s" instead.', gettype($generator_length)));
90
        }
91
92 26
        if (!is_int($sequence_length)) {
93 1
            throw new ArgumentTypeException(sprintf('Sequence length should be integer, got "%s" instead.', gettype($sequence_length)));
94
        }
95
96 25
        if (!is_int($time_length)) {
97 1
            throw new ArgumentTypeException(sprintf('Time length should be integer, got "%s" instead.', gettype($time_length)));
98
        }
99
100 24
        if (!is_int($time_offset)) {
101 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset)));
102
        }
103
104 23
        if ($generator < 0) {
105 1
            throw new ZeroArgumentException(sprintf('Generator should be grate then "0", got "%d" instead.', $generator));
106
        }
107
108 22
        if ($generator_length < 0) {
109 1
            throw new ZeroArgumentException(sprintf('Generator length should be grate then "0", got "%d" instead.', $generator_length));
110
        }
111
112 21
        if ($sequence_length < 0) {
113 1
            throw new ZeroArgumentException(sprintf('Sequence length should be grate then "0", got "%d" instead.', $sequence_length));
114
        }
115
116 20
        if ($time_length < 0) {
117 1
            throw new ZeroArgumentException(sprintf('Time length should be grate then "0", got "%d" instead.', $time_length));
118
        }
119
120 19
        if ($time_offset < 0) {
121 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then "0", got "%d" instead.', $time_offset));
122
        }
123
124 18
        if ($generator_length + $sequence_length + $time_length > 64 - 1) {
125 3
            throw new ArgumentRangeException(sprintf('Length of generator, sequence and time for UID should be less than or equal to "%d", got "%d" instead.', 64 - 1, $generator_length + $sequence_length + $time_length));
126
        }
127
128 15
        $max_generator_id = (int) bindec(str_repeat('1', $generator_length));
129
130 15
        if ($generator > $max_generator_id) {
131 10
            throw new ArgumentRangeException(sprintf('Generator should be grate then or equal to "%d", got "%d" instead.', $max_generator_id, $generator));
132
        }
133
134 5
        $now = (int) floor(microtime(true) * 1000);
135
136 5
        if ($time_offset > $now) {
137 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
138
        }
139
140 4
        $min_time_length = strlen(decbin($now - $time_offset));
141
142 4
        if ($time_length < $min_time_length) {
143
            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));
144
        }
145
146 4
        $this->generator = $generator;
147 4
        $this->generator_length = $generator_length;
148 4
        $this->sequence_length = $sequence_length;
149 4
        $this->time_length = $time_length;
150 4
        $this->time_offset = $time_offset;
151 4
    }
152
153
    /**
154
     * @return int
155
     */
156 4
    public function generate()
157
    {
158 4
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
159
160
        // @codeCoverageIgnoreStart
161
        // overflow validation is in the constructor,
162
        // but there is a chance that overflow will occur in the process of using this service
163
        if ($time >= 1 << $this->time_length) {
164
            throw new BitmapOverflowException(sprintf('Bitmap for time is overflow of %d bits.', $this->time_length));
165
        }
166
        // @codeCoverageIgnoreEnd
167
168 4
        if ($this->last_time === $time) {
169 1
            ++$this->sequence;
170
        } else {
171 4
            $this->last_time = $time;
172 4
            $this->sequence = 0;
173
        }
174
175 4
        $uid = 1 << 64 - 1;
176 4
        $uid |= $time << $this->generator_length + $this->sequence_length;
177 4
        $uid |= $this->generator << $this->sequence_length;
178 4
        $uid |= $this->sequence;
179
180 4
        return $uid;
181
    }
182
}
183