Completed
Push — master ( ccee6a...5f509c )
by Michael
01:47
created

GeneratesUuid::resolveUuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dyrynda\Database\Support;
4
5
/**
6
 * UUID generation trait.
7
 *
8
 * Include this trait in any Eloquent model where you wish to automatically set
9
 * a UUID field. When saving, if the UUID field has not been set, generate a
10
 * new UUID value, which will be set on the model and saved by Eloquent.
11
 *
12
 * @copyright 2017 Michael Dyrynda
13
 * @author    Michael Dyrynda <[email protected]>
14
 * @license   MIT
15
 */
16
trait GeneratesUuid
17
{
18
    /**
19
     * The UUID versions.
20
     *
21
     * @var array
22
     */
23
    protected $uuidVersions = [
24
        'uuid1',
25
        'uuid3',
26
        'uuid4',
27
        'uuid5',
28
    ];
29
30
    /**
31
     * Determine whether an attribute should be cast to a native type.
32
     *
33
     * @param  string  $key
34
     * @param  array|string|null  $types
35
     * @return bool
36
     */
37
    abstract public function hasCast($key, $types = null);
38
39
    /**
40
     * Boot the trait, adding a creating observer.
41
     *
42
     * When persisting a new model instance, we resolve the UUID field, then set
43
     * a fresh UUID, taking into account if we need to cast to binary or not.
44
     *
45
     * @return void
46
     */
47
    public static function bootGeneratesUuid()
48
    {
49 6
        static::creating(function ($model) {
50
            /* @var \Illuminate\Database\Eloquent\Model|static $model */
51 6
            $uuid = $model->resolveUuid();
0 ignored issues
show
Bug introduced by
The method resolveUuid does only exist in Dyrynda\Database\Support\GeneratesUuid, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
52
53 6
            if (isset($model->attributes['uuid']) && ! is_null($model->attributes['uuid'])) {
54
                /* @var \Ramsey\Uuid\Uuid $uuid */
55 4
                $uuid = $uuid->fromString(strtolower($model->attributes['uuid']));
56
            }
57
58 6
            $model->attributes['uuid'] = $model->hasCast('uuid') ? $uuid->getBytes() : $uuid->toString();
59 6
        });
60 2
    }
61
62
    /**
63
     * Resolve a UUID instance for the configured version.
64
     *
65
     * @return \Ramsey\Uuid\Uuid
66
     */
67 6
    public function resolveUuid()
68
    {
69 6
        return call_user_func("\Ramsey\Uuid\Uuid::{$this->resolveUuidVersion()}");
70
    }
71
72
    /**
73
     * Resolve the UUID version to use when setting the UUID value. Default to uuid4.
74
     *
75
     * @return string
76
     */
77 11
    public function resolveUuidVersion()
78
    {
79 11
        if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) {
80 4
            return $this->uuidVersion;
81
        }
82
83 7
        return 'uuid4';
84
    }
85
86
    /**
87
     * Scope queries to find by UUID.
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Builder  $query
90
     * @param  string  $uuid
91
     *
92
     * @return \Illuminate\Database\Eloquent\Builder
93
     */
94 2
    public function scopeWhereUuid($query, $uuid)
95
    {
96 2
        if ($this->hasCast('uuid')) {
97 1
            $uuid = $this->resolveUuid()->fromString($uuid)->getBytes();
98
        }
99
100 2
        return $query->where('uuid', $uuid);
101
    }
102
103
    /**
104
     * Cast an attribute to a native PHP type.
105
     *
106
     * @param  string  $key
107
     * @param  mixed  $value
108
     * @return mixed
109
     */
110 2
    protected function castAttribute($key, $value)
111
    {
112 2
        if ($key === 'uuid' && ! is_null($value)) {
113 2
            return $this->resolveUuid()->fromBytes($value)->toString();
114
        }
115
116
        return parent::castAttribute($key, $value);
117
    }
118
}
119