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

TimeBinaryGenerator::__construct()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 52

Duplication

Lines 9
Ratio 17.31 %

Code Coverage

Tests 28
CRAP Score 11.0048

Importance

Changes 0
Metric Value
dl 9
loc 52
c 0
b 0
f 0
ccs 28
cts 29
cp 0.9655
rs 6.9006
cc 11
nc 11
nop 3
crap 11.0048

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:47 (UTC)
51
     *  41-bits = 11111111111111111111111111111111111111111     = 2199023255551  = 2039-09-07 16:47:35 (UTC)
52
     *  42-bits = 111111111111111111111111111111111111111111    = 4398046511103  = 2109-05-15 08:35:11 (UTC)
53
     *  43-bits = 1111111111111111111111111111111111111111111   = 8796093022207  = 2248-09-26 16:10:22 (UTC)
54
     *  44-bits = 11111111111111111111111111111111111111111111  = 17592186044415 = 2527-06-23 07:20:44 (UTC)
55
     *  45-bits = 111111111111111111111111111111111111111111111 = 35184372088831 = 3084-12-12 12:41:28 (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
     *  1577833200000 = 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 17 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...
69
            throw new ProcessorArchitectureException(sprintf('This generator require 64-bit mode of processor architecture. Your processor architecture support %d-bit mode.', PHP_INT_SIZE * 8));
70
        }
71
72 17
        if (!is_int($prefix_length)) {
73 1
            throw new ArgumentTypeException(sprintf('Length of prefix for UID should be integer, got "%s" instead.', gettype($prefix_length)));
74
        }
75
76 16
        if (!is_int($time_length)) {
77 1
            throw new ArgumentTypeException(sprintf('Length of time for UID should be integer, got "%s" instead.', gettype($time_length)));
78
        }
79
80 15
        if (!is_int($time_offset)) {
81 1
            throw new ArgumentTypeException(sprintf('Time offset should be integer, got "%s" instead.', gettype($time_offset)));
82
        }
83
84 14
        if ($prefix_length < 0) {
85 1
            throw new ZeroArgumentException(sprintf('Length of prefix for UID should be grate then or equal to "0", got "%d" instead.', $prefix_length));
86
        }
87
88 13
        if ($time_length < 0) {
89 1
            throw new ZeroArgumentException(sprintf('Length of time for UID should be grate then or equal to "0", got "%d" instead.', $time_length));
90
        }
91
92 12
        if ($time_offset < 0) {
93 1
            throw new ZeroArgumentException(sprintf('Time offset should be grate then or equal to "0", got "%d" instead.', $time_offset));
94
        }
95
96 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...
97 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));
98
        }
99
100 9
        $now = (int) floor(microtime(true) * 1000);
101
102 9
        if ($time_offset > $now) {
103 1
            throw new ArgumentRangeException(sprintf('Time offset should be grate then or equal to current time "%d", got "%d" instead.', $now, $time_offset));
104
        }
105
106 8
        $min_time_length = strlen(decbin($now));
107
108 8 View Code Duplication
        if ($time_length < $min_time_length - $time_offset) {
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...
109 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_offset, $time_length));
110
        }
111
112 6
        $this->time_length = $time_length;
113 6
        $this->time_offset = $time_offset;
114 6
        $this->suffix_length = $time_length - $prefix_length;
115 6
        $this->prefix_max_value = (int) bindec(str_repeat('1', $prefix_length));
116 6
        $this->suffix_max_value = (int) bindec(str_repeat('1', $this->suffix_length));
117 6
    }
118
119
    /**
120
     * @return int
121
     */
122 6
    public function generate()
123
    {
124 6
        $time = ((int) floor(microtime(true) * 1000) - $this->time_offset);
125
126
        // @codeCoverageIgnoreStart
127
        // overflow validation is in the constructor,
128
        // but there is a chance that overflow will occur in the process of using this service
129
        if ($time >= 1 << $this->time_length) {
130
            throw new BitmapOverflowException(sprintf('Bitmap for time is overflow of %d bits.', $this->time_length));
131
        }
132
        // @codeCoverageIgnoreEnd
133
134 6
        $prefix = random_int(0, $this->prefix_max_value);
135 6
        $suffix = random_int(0, $this->suffix_max_value);
136
137 6
        $uid = 1 << 64 - 1; // first bit is a bitmap limiter
138 6
        $uid |= $prefix << $this->time_length + $this->suffix_length;
139 6
        $uid |= $time << $this->suffix_length;
140 6
        $uid |= $suffix;
141
142 6
        return $uid;
143
    }
144
}
145