HasUuidKey   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getKeyType() 0 3 1
A getIncrementing() 0 3 1
A boot() 0 11 2
1
<?php
2
3
namespace Bmatovu\Uuid\Traits;
4
5
use Bmatovu\Uuid\Support\Util;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait HasUuidKey
9
{
10
    /**
11
     * Get the auto-incrementing key type.
12
     *
13
     * @return string
14
     */
15 1
    public function getKeyType()
16
    {
17 1
        return 'string';
18
    }
19
20
    /**
21
     * Get the value indicating whether the IDs are incrementing.
22
     *
23
     * @return bool
24
     */
25 6
    public function getIncrementing()
26
    {
27 6
        return false;
28
    }
29
30
    /**
31
     * The "booting" method of the model.
32
     *
33
     * @return void
34
     */
35 7
    protected static function boot(): void
36
    {
37 7
        parent::boot();
38
39
        static::creating(function (Model $model): void {
40 4
            if (! $model->getKey()) {
41 3
                $version = config('uuid.version');
42 3
                $namespace = config('uuid.namespace');
43 3
                $name = config('uuid.name');
44
45 3
                $model->{$model->getKeyName()} = Util::generateUuid($version, $namespace, $name)->toString();
46
            }
47 7
        });
48 7
    }
49
}
50