LaravelSequenceResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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
use Illuminate\Contracts\Cache\Repository;
14
15
class LaravelSequenceResolver implements SequenceResolver
16
{
17
    /**
18
     * The laravel cache instance.
19
     *
20
     * @var \Illuminate\Contracts\Cache\Repository
21
     */
22
    protected $cache;
23
24
    /**
25
     * Init resolve instance, must connectioned.
26
     */
27
    public function __construct(Repository $cache)
28
    {
29
        $this->cache = $cache;
30
    }
31
32
    /**
33
     *  {@inheritdoc}
34
     */
35
    public function sequence(int $currentTime)
36
    {
37
        $key = $currentTime;
38
39
        if ($this->cache->add($key, 1, 1)) {
40
            return 0;
41
        }
42
43
        return $this->cache->increment($key, 1);
44
    }
45
}
46