SwooleSequenceResolver   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 70
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A sequence() 0 28 4
1
<?php
2
3
/*
4
 * This file is part of the godruoyi/php-snowflake.
5
 *
6
 * (c) Godruoyi <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled.
9
 */
10
11
namespace Godruoyi\Snowflake;
12
13
class SwooleSequenceResolver implements SequenceResolver
14
{
15
    /**
16
     * The las ttimestamp.
17
     *
18
     * @var null
19
     */
20
    protected $lastTimeStamp = -1;
21
22
    /**
23
     * The sequence.
24
     *
25
     * @var int
26
     */
27
    protected $sequence = 0;
28
29
    /**
30
     * The swoole lock.
31
     *
32
     * @var mixed
33
     */
34
    protected $lock;
35
36
    /**
37
     * The cycle count.
38
     *
39
     * @var int
40
     */
41
    protected $count = 0;
42
43
    /**
44
     * Init swoole lock.
45
     */
46
    public function __construct()
47
    {
48
        $this->lock = new \swoole_lock(SWOOLE_MUTEX);
49
    }
50
51
    /**
52
     *  {@inheritdoc}
53
     */
54
    public function sequence(int $currentTime)
55
    {
56
        /*
57
         * If swoole lock failure,we return a bit number, This will cause the program to
58
         * perform the next millisecond operation.
59
         */
60
        if (!$this->lock->trylock()) {
61
            if ($this->count >= 10) {
62
                throw new \Exception('Swoole lock failure, Unable to get the program lock after many attempts.');
63
            }
64
65
            ++$this->count;
66
67
            return 999999;
68
        }
69
70
        if ($this->lastTimeStamp === $currentTime) {
71
            ++$this->sequence;
72
        } else {
73
            $this->sequence = 0;
74
        }
75
76
        $this->lastTimeStamp = $currentTime;
0 ignored issues
show
Documentation Bug introduced by
It seems like $currentTime of type integer is incompatible with the declared type null of property $lastTimeStamp.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
77
78
        $this->lock->unlock();
79
80
        return $this->sequence;
81
    }
82
}
83