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

SnowflakeGenerator::__construct()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 55

Duplication

Lines 3
Ratio 5.45 %

Code Coverage

Tests 26
CRAP Score 11

Importance

Changes 0
Metric Value
dl 3
loc 55
ccs 26
cts 26
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\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