HasUuidKey::getKeyType()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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