Completed
Push — master ( 096858...7ec34d )
by Gabriel
03:50
created

HasUuidRecordManagerTrait   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 76.67%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 34
c 1
b 0
f 1
dl 0
loc 94
ccs 23
cts 30
cp 0.7667
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A resolveUuidVersion() 0 13 5
A uuidColumn() 0 3 1
A bootHasUuidRecordManagerTrait() 0 20 5
A uuidColumns() 0 3 1
A generateUuid() 0 10 2
1
<?php
2
3
namespace Nip\Records\Traits\HasUuid;
4
5
6
use Exception;
7
use Nip\Records\EventManager\Events\Event;
8
use Nip\Records\RecordManager;
9
use Nip\Utility\Uuid;
10
use Ramsey\Uuid\Exception\InvalidUuidStringException;
11
12
/**
13
 * Trait HasUuidRecordManagerTrait
14
 * @package Nip\Records\Traits\HasUuid
15
 */
16
trait HasUuidRecordManagerTrait
17
{
18
    /**
19
     * The UUID versions.
20
     *
21
     * @var array
22
     */
23
    protected $uuidVersions = [
24
        '1',
25
        '3',
26
        '4',
27
        '5',
28
        '6',
29
        'ordered',
30
    ];
31
32 3
    public function bootHasUuidRecordManagerTrait()
33
    {
34
        static::creating(function (Event $event) {
35 1
            $record = $event->getRecord();
36
            /** @var static|RecordManager $manager */
37 1
            $manager = $event->getManager();
38 1
            $columns = $manager->uuidColumns();
0 ignored issues
show
Bug introduced by
The method uuidColumns() does not exist on Nip\Records\RecordManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

38
            /** @scrutinizer ignore-call */ 
39
            $columns = $manager->uuidColumns();
Loading history...
39 1
            foreach ($columns as $column) {
40
                /* @var \Ramsey\Uuid\Uuid $uuid */
41 1
                $uuid = $manager->generateUuid();
0 ignored issues
show
Bug introduced by
The method generateUuid() does not exist on Nip\Records\RecordManager. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
                /** @scrutinizer ignore-call */ 
42
                $uuid = $manager->generateUuid();
Loading history...
42
43 1
                if (isset($record->{$column}) && !is_null($record->{$column})) {
44
                    try {
45 1
                        $uuid = $uuid->fromString(strtolower($record->{$column}));
46
                    } catch (InvalidUuidStringException $e) {
47
                        $uuid = $uuid->fromBytes($record->{$column});
0 ignored issues
show
Bug introduced by
The method fromBytes() does not exist on Ramsey\Uuid\UuidInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Ramsey\Uuid\Rfc4122\UuidInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                        /** @scrutinizer ignore-call */ 
48
                        $uuid = $uuid->fromBytes($record->{$column});
Loading history...
48
                    }
49
                }
50
51 1
                $record->{$column} = strtolower($uuid->toString());
52
            }
53 3
        });
54 2
    }
55
56
    /**
57
     * The name of the column that should be used for the UUID.
58
     *
59
     * @return string
60
     */
61 1
    public function uuidColumn(): string
62
    {
63 1
        return 'uuid';
64
    }
65
66
    /**
67
     * The names of the columns that should be used for the UUID.
68
     *
69
     * @return array
70
     */
71 1
    public function uuidColumns(): array
72
    {
73 1
        return [$this->uuidColumn()];
74
    }
75
76
    /**
77
     * Resolve the UUID version to use when setting the UUID value. Default to uuid4.
78
     *
79
     * @return string
80
     */
81 1
    public function resolveUuidVersion(): string
82
    {
83 1
        if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) {
84
            return $this->uuidVersion;
85
        }
86 1
        if (method_exists($this, 'uuidVersion')) {
87
            $version = $this->uuidVersion();
88
            if (in_array($version, $this->uuidVersions)) {
89
                return $version;
90
            }
91
        }
92
93 1
        return 4;
94
    }
95
96
    /**
97
     * @return \Ramsey\Uuid\UuidInterface
98
     * @throws Exception
99
     */
100 1
    protected function generateUuid()
101
    {
102 1
        $version = $this->resolveUuidVersion();
103
104
        switch ($version) {
105 1
            case 4:
106 1
                return Uuid::v4();
107
        }
108
109
        throw new Exception("UUID version [{$version}] not supported.");
110
    }
111
}