Completed
Pull Request — master (#36)
by Jean-Philippe
02:13
created

GeneratesUuid   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 103
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
hasCast() 0 1 ?
A bootGeneratesUuid() 0 14 4
A resolveUuid() 0 4 1
A resolveUuidVersion() 0 8 3
A scopeWhereUuid() 0 8 2
A castAttribute() 0 8 3
1
<?php
2
3
namespace Dyrynda\Database\Support;
4
5
use Ramsey\Uuid\Uuid;
6
7
/**
8
 * UUID generation trait.
9
 *
10
 * Include this trait in any Eloquent model where you wish to automatically set
11
 * a UUID field. When saving, if the UUID field has not been set, generate a
12
 * new UUID value, which will be set on the model and saved by Eloquent.
13
 *
14
 * @copyright 2017 Michael Dyrynda
15
 * @author    Michael Dyrynda <[email protected]>
16
 * @license   MIT
17
 *
18
 * @property  string  $uuidVersion
19
 */
20
trait GeneratesUuid
21
{
22
    /**
23
     * The UUID versions.
24
     *
25
     * @var array
26
     */
27
    protected $uuidVersions = [
28
        'uuid1',
29
        'uuid3',
30
        'uuid4',
31
        'uuid5',
32
    ];
33
34
    /**
35
     * Determine whether an attribute should be cast to a native type.
36
     *
37
     * @param  string  $key
38
     * @param  array|string|null  $types
39
     * @return bool
40
     */
41
    abstract public function hasCast($key, $types = null);
42
43
    /**
44
     * Boot the trait, adding a creating observer.
45
     *
46
     * When persisting a new model instance, we resolve the UUID field, then set
47
     * a fresh UUID, taking into account if we need to cast to binary or not.
48
     *
49
     * @return void
50
     */
51
    public static function bootGeneratesUuid()
52
    {
53 6
        static::creating(function ($model) {
54
            /* @var \Illuminate\Database\Eloquent\Model|static $model */
55 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...
56
57 6
            if (isset($model->attributes['uuid']) && ! is_null($model->attributes['uuid'])) {
58
                /* @var \Ramsey\Uuid\Uuid $uuid */
59 4
                $uuid = $uuid->fromString(strtolower($model->attributes['uuid']));
60 4
            }
61
62 6
            $model->attributes['uuid'] = $model->hasCast('uuid') ? $uuid->getBytes() : $uuid->toString();
63 6
        });
64 2
    }
65
66
    /**
67
     * Resolve a UUID instance for the configured version.
68
     *
69
     * @return \Ramsey\Uuid\Uuid
70
     */
71 6
    public function resolveUuid()
72
    {
73 6
        return call_user_func([Uuid::class, $this->resolveUuidVersion()]);
74
    }
75
76
    /**
77
     * Resolve the UUID version to use when setting the UUID value. Default to uuid4.
78
     *
79
     * @return string
80
     */
81 11
    public function resolveUuidVersion()
82
    {
83 11
        if (property_exists($this, 'uuidVersion') && in_array($this->uuidVersion, $this->uuidVersions)) {
84 4
            return $this->uuidVersion;
85
        }
86
87 7
        return 'uuid4';
88
    }
89
90
    /**
91
     * Scope queries to find by UUID.
92
     *
93
     * @param  \Illuminate\Database\Eloquent\Builder  $query
94
     * @param  string  $uuid
95
     *
96
     * @return \Illuminate\Database\Eloquent\Builder
97
     */
98 2
    public function scopeWhereUuid($query, $uuid)
99
    {
100 2
        if ($this->hasCast('uuid')) {
101 1
            $uuid = $this->resolveUuid()->fromString($uuid)->getBytes();
102 1
        }
103
104 2
        return $query->where('uuid', $uuid);
105
    }
106
107
    /**
108
     * Cast an attribute to a native PHP type.
109
     *
110
     * @param  string  $key
111
     * @param  mixed  $value
112
     * @return mixed
113
     */
114 2
    protected function castAttribute($key, $value)
115
    {
116 2
        if ($key === 'uuid' && ! is_null($value)) {
117 2
            return $this->resolveUuid()->fromBytes($value)->toString();
118
        }
119
120
        return parent::castAttribute($key, $value);
121
    }
122
}
123