Snowflake::boolSnowflake()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * 雪花算法 Trait
4
 *
5
 * @author herry@<[email protected]>
6
 * @version 1.0
7
 * @copyright © 2020 MuCTS.com All Rights Reserved.
8
 */
9
10
namespace MuCTS\Laravel\Snowflake\Models\Traits;
11
12
13
use MuCTS\Laravel\Snowflake\Facades\Snowflake as SnowflakeFacade;
14
15
/**
16
 * Trait Snowflake
17
 * @package MuCTS\Laravel\Snowflake\Models\Traits
18
 */
19
trait Snowflake
20
{
21
    protected static function boolSnowflake()
22
    {
23
        static::saving(function ($model) {
24
            if (is_null($model->getKey())) {
25
                $model->setIncrementing(false);
26
                $keyName = $model->getKeyName();
27
                $id = SnowflakeFacade::next();
28
                $model->setAttribute($keyName, $id);
29
            }
30
        });
31
    }
32
}