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

SnowflakeGenerator::__construct()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 52

Duplication

Lines 3
Ratio 5.77 %

Code Coverage

Tests 27
CRAP Score 11.0055

Importance

Changes 0
Metric Value
dl 3
loc 52
c 0
b 0
f 0
ccs 27
cts 28
cp 0.9643
rs 6.9006
cc 11
nc 11
nop 3
crap 11.0055

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\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 13 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...
81
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
82
        }
83
84 13
        if (!is_int($data_center)) {
85 1
            throw new ArgumentTypeException(sprintf('Data center should be integer, got "%s" instead.', gettype($data_center)));
86
        }
87
88 12
        if (!is_int($machine)) {
89 1
            throw new ArgumentTypeException(sprintf('Machine should be integer, got "%s" instead.', gettype($data_center)));
90
        }
91
92 11
        if (!is_int($time_offset)) {
93 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($data_center)));
94
        }
95
96 10
        if ($data_center < 0) {
97 1
            throw new ZeroArgumentException(sprintf('Data center should be grate then "0", got "%d" instead.', $data_center));
98
        }
99
100 9
        if ($machine < 0) {
101 1
            throw new ZeroArgumentException(sprintf('Machine should be grate then "0", got "%d" instead.', $machine));
102
        }
103
104 8
        if ($time_offset < 0) {
105 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then "0", got "%d" instead.', $time_offset));
106
        }
107
108 7
        $max_data_center = (int) bindec(str_repeat('1', self::$DATA_CENTER_LENGTH));
109
110 7
        if ($data_center > $max_data_center) {
111 1
            throw new ArgumentRangeException(sprintf('Data center number should be grate then or equal to "%d", got "%d" instead.', $max_data_center, $data_center));
112
        }
113
114 6
        $max_machine = (int) bindec(str_repeat('1', self::$MACHINE_LENGTH));
115
116 6
        if ($machine > $max_machine) {
117 1
            throw new ArgumentRangeException(sprintf('Data center number should be grate then or equal to "%d", got "%d" instead.', $max_machine, $machine));
118
        }
119
120 5
        $now = (int) floor(microtime(true) * 1000);
121
122 5
        if ($time_offset > $now) {
123 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
124
        }
125
126 4
        $this->data_center = $data_center;
127 4
        $this->machine = $machine;
128 4
        $this->time_offset = $time_offset;
129 4
    }
130
131
    /**
132
     * @return int
133
     */
134 4
    public function generate()
135
    {
136 4
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
137
138 4
        if ($this->last_time === $time) {
139 1
            ++$this->sequence;
140
        } else {
141 4
            $this->last_time = $time;
142
        }
143
144 4
        $uid = 1 << 64 - 1;
145 4
        $uid |= $time << self::$DATA_CENTER_LENGTH + self::$MACHINE_LENGTH + self::$SEQUENCE_LENGTH;
146 4
        $uid |= $this->data_center << self::$MACHINE_LENGTH + self::$SEQUENCE_LENGTH;
147 4
        $uid |= $this->machine << self::$SEQUENCE_LENGTH;
148 4
        $uid |= $this->sequence;
149
150 4
        return $uid;
151
    }
152
}
153