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

SnowflakeGenerator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 138
Duplicated Lines 2.17 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 3
loc 138
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 3 55 11
A generate() 0 18 2

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\ProcessorArchitectureException;
16
use GpsLab\Component\Base64UID\Exception\ZeroArgumentException;
17
18
class SnowflakeGenerator implements BinaryGenerator
19
{
20
    /**
21
     * TODO use private const after drop PHP < 7.1.
22
     *
23
     * @var int
24
     */
25
    private static $DATA_CENTER_LENGTH = 5; // data center value 0-31
26
27
    /**
28
     * TODO use private const after drop PHP < 7.1.
29
     *
30
     * @var int
31
     */
32
    private static $MACHINE_LENGTH = 7; // machine value 0-127
33
34
    /**
35
     * TODO use private const after drop PHP < 7.1.
36
     *
37
     * @var int
38
     */
39
    private static $SEQUENCE_LENGTH = 6; // sequence value 0-63
40
41
    /**
42
     * @var int
43
     */
44
    private $data_center;
45
46
    /**
47
     * @var int
48
     */
49
    private $machine;
50
51
    /**
52
     * @var int
53
     */
54
    private $time_offset;
55
56
    /**
57
     * @var int
58
     */
59
    private $last_time = 0;
60
61
    /**
62
     * @var int
63
     */
64
    private $sequence = 0;
65
66
    /**
67
     * Snowflake.
68
     *
69
     * The time offset allows to move the starting point of time in microseconds,
70
     * which reduces the size of the stored time:
71
     *  0             = 1970-01-01 00:00:00 (UTC)
72
     *  1577833200000 = 2020-01-01 00:00:00 (UTC)
73
     *
74
     * @param int $data_center
75
     * @param int $machine
76
     * @param int $time_offset
77
     */
78 13
    public function __construct($data_center, $machine, $time_offset = 0)
79
    {
80
        // @codeCoverageIgnoreStart
81
        // can't reproduce this condition in tests
82 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...
83
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
84
        }
85
        // @codeCoverageIgnoreEnd
86
87 13
        if (!is_int($data_center)) {
88 1
            throw new ArgumentTypeException(sprintf('Data center should be integer, got "%s" instead.', gettype($data_center)));
89
        }
90
91 12
        if (!is_int($machine)) {
92 1
            throw new ArgumentTypeException(sprintf('Machine should be integer, got "%s" instead.', gettype($data_center)));
93
        }
94
95 11
        if (!is_int($time_offset)) {
96 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($data_center)));
97
        }
98
99 10
        if ($data_center < 0) {
100 1
            throw new ZeroArgumentException(sprintf('Data center should be grate then "0", got "%d" instead.', $data_center));
101
        }
102
103 9
        if ($machine < 0) {
104 1
            throw new ZeroArgumentException(sprintf('Machine should be grate then "0", got "%d" instead.', $machine));
105
        }
106
107 8
        if ($time_offset < 0) {
108 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then "0", got "%d" instead.', $time_offset));
109
        }
110
111 7
        $max_data_center = (int) bindec(str_repeat('1', self::$DATA_CENTER_LENGTH));
112
113 7
        if ($data_center > $max_data_center) {
114 1
            throw new ArgumentRangeException(sprintf('Data center number should be grate then or equal to "%d", got "%d" instead.', $max_data_center, $data_center));
115
        }
116
117 6
        $max_machine = (int) bindec(str_repeat('1', self::$MACHINE_LENGTH));
118
119 6
        if ($machine > $max_machine) {
120 1
            throw new ArgumentRangeException(sprintf('Data center number should be grate then or equal to "%d", got "%d" instead.', $max_machine, $machine));
121
        }
122
123 5
        $now = (int) floor(microtime(true) * 1000);
124
125 5
        if ($time_offset > $now) {
126 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
127
        }
128
129 4
        $this->data_center = $data_center;
130 4
        $this->machine = $machine;
131 4
        $this->time_offset = $time_offset;
132 4
    }
133
134
    /**
135
     * @return int
136
     */
137 4
    public function generate()
138
    {
139 4
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
140
141 4
        if ($this->last_time === $time) {
142 1
            ++$this->sequence;
143
        } else {
144 4
            $this->last_time = $time;
145
        }
146
147 4
        $uid = 1 << 64 - 1;
148 4
        $uid |= $time << self::$DATA_CENTER_LENGTH + self::$MACHINE_LENGTH + self::$SEQUENCE_LENGTH;
149 4
        $uid |= $this->data_center << self::$MACHINE_LENGTH + self::$SEQUENCE_LENGTH;
150 4
        $uid |= $this->machine << self::$SEQUENCE_LENGTH;
151 4
        $uid |= $this->sequence;
152
153 4
        return $uid;
154
    }
155
}
156