Completed
Push — master ( 447684...91c0e6 )
by Brent
01:21
created

HasBinaryUuid::scopeWithUuidRelation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 3
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::encodeUuid(Uuid::uuid1());
0 ignored issues
show
Documentation introduced by
\Ramsey\Uuid\Uuid::uuid1() is of type object<Ramsey\Uuid\UuidInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
19
        });
20
    }
21
22
    public static function scopeWithUuid(Builder $builder, $uuid, ?string $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 encodeUuid(string $uuid): string
53
    {
54
        if (! Uuid::isValid($uuid)) {
55
            return $uuid;
56
        }
57
58
        $uuid = str_replace('-', '', (string) $uuid);
59
60
        return
61
            substr(hex2bin($uuid), 6, 2)
62
            .substr(hex2bin($uuid), 4, 2)
63
            .substr(hex2bin($uuid), 0, 4)
64
            .substr(hex2bin($uuid), 8, 8);
65
    }
66
67
    public static function decodeUuid(string $binaryUuid): string
68
    {
69
        if (Uuid::isValid($binaryUuid)) {
70
            return $binaryUuid;
71
        }
72
73
        $uuidWithoutDashes = bin2hex(
74
            substr($binaryUuid, 4, 4)
75
            .substr($binaryUuid, 2, 2)
76
            .substr($binaryUuid, 0, 2)
77
            .substr($binaryUuid, 8, 8)
78
        );
79
80
        $uuidWithDashes = collect([8, 13, 18, 23])->reduce(function ($uuid, $position) {
81
            return substr_replace($uuid, '-', $position, 0);
82
        }, $uuidWithoutDashes);
83
84
        return $uuidWithDashes;
85
    }
86
87
    public function getUuidTextAttribute(): string
88
    {
89
        return static::decodeUuid($this->{$this->getKeyName()});
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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...
90
    }
91
92
    public function setUuidTextAttribute(string $uuid)
93
    {
94
        $this->{$this->getKeyName()} = static::encodeUuid($uuid);
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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...
95
    }
96
97
    public function getQueueableId()
98
    {
99
        return base64_encode($this->{$this->getKeyName()});
0 ignored issues
show
Bug introduced by
It seems like getKeyName() 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...
100
    }
101
102
    public function newQueryForRestoration($id)
103
    {
104
        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...
105
    }
106
}
107