Completed
Pull Request — master (#43)
by
unknown
02:11
created

HasBinaryUuid::newQueryForRestoration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Spatie\BinaryUuid;
4
5
use Ramsey\Uuid\Uuid;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Builder;
8
9
trait HasBinaryUuid
10
{
11
    protected static function bootHasBinaryUuid()
12
    {
13
        static::creating(function (Model $model) {
14
            if ($model->{$model->getKeyName()}) {
15
                return;
16
            }
17
18
            $model->{$model->getKeyName()} = static::generateUuid();
19
        });
20
    }
21
22
    public static function scopeWithUuid(Builder $builder, $uuid, $field = null): Builder
23
    {
24
        if ($field) {
25
            return static::scopeWithUuidRelation($builder, $uuid, $field);
26
        }
27
28
        if ($uuid instanceof Uuid) {
29
            $uuid = (string) $uuid;
30
        }
31
32
        $uuid = (array) $uuid;
33
34
        return $builder->whereKey(array_map(function (string $modelUuid) {
35
            return static::encodeUuid($modelUuid);
36
        }, $uuid));
37
    }
38
39
    public static function scopeWithUuidRelation(Builder $builder, $uuid, string $field): Builder
40
    {
41
        if ($uuid instanceof Uuid) {
42
            $uuid = (string) $uuid;
43
        }
44
45
        $uuid = (array) $uuid;
46
47
        return $builder->whereIn($field, array_map(function (string $modelUuid) {
48
            return static::encodeUuid($modelUuid);
49
        }, $uuid));
50
    }
51
52
    public static function generateUuid() : string
53
    {
54
        return Uuid::uuid1();
55
    }
56
57
    public static function encodeUuid($uuid): string
58
    {
59
        if (! Uuid::isValid($uuid)) {
60
            return $uuid;
61
        }
62
63
        if (! $uuid instanceof Uuid) {
64
            $uuid = Uuid::fromString($uuid);
65
        }
66
67
        return $uuid->getBytes();
68
    }
69
70
    public static function decodeUuid(string $binaryUuid): string
71
    {
72
        if (Uuid::isValid($binaryUuid)) {
73
            return $binaryUuid;
74
        }
75
76
        return Uuid::fromBytes($binaryUuid)->toString();
77
    }
78
79
    public function toArray()
80
    {
81
        if (! $this->exists) {
0 ignored issues
show
Bug introduced by
The property exists does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
82
            return parent::toArray();
83
        }
84
85
        return array_merge(parent::toArray(), [$this->getKeyName() => $this->uuid_text]);
0 ignored issues
show
Bug introduced by
The property uuid_text does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
86
    }
87
88
    public function getUuidTextAttribute(): ?string
89
    {
90
        if (! $this->exists) {
91
            return null;
92
        }
93
94
        return static::decodeUuid($this->{$this->getKeyName()});
95
    }
96
97
    public function setUuidTextAttribute(string $uuid)
98
    {
99
        $this->{$this->getKeyName()} = static::encodeUuid($uuid);
100
    }
101
102
    public function getQueueableId()
103
    {
104
        return base64_encode($this->{$this->getKeyName()});
105
    }
106
107
    public function newQueryForRestoration($id)
108
    {
109
        return $this->newQueryWithoutScopes()->whereKey(base64_decode($id));
0 ignored issues
show
Bug introduced by
It seems like newQueryWithoutScopes() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
110
    }
111
112
    public function getRouteKeyName()
113
    {
114
        return 'uuid_text';
115
    }
116
117
    public function getKeyName()
118
    {
119
        return 'uuid';
120
    }
121
122
    public function getIncrementing()
123
    {
124
        return false;
125
    }
126
127
    public function resolveRouteBinding($value)
128
    {
129
        return $this->withUuid($value)->first();
0 ignored issues
show
Bug introduced by
The method withUuid() does not exist on Spatie\BinaryUuid\HasBinaryUuid. Did you maybe mean scopeWithUuid()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
130
    }
131
}
132