Completed
Push — master ( 3f89d7...f790e9 )
by Mike
30:28 queued 29:00
created

AuthorBeginsWithScoped   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 54
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 6 1
A books() 0 4 1
A printers() 0 4 1
A profile() 0 4 1
A getLatestBookAttribute() 0 7 1
A scopeStartsWithA() 0 4 1
A scopeNameStartsWith() 0 4 1
1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2
3
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Scopes\NameBeginsWith;
4
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
9
use Illuminate\Database\Eloquent\Relations\HasOne;
10
11
class AuthorBeginsWithScoped extends Model
12
{
13
    use Cachable;
14
15
    protected $table = "authors";
16
    protected $casts = [
17
        "finances" => "array",
18
    ];
19
    protected $fillable = [
20
        'name',
21
        'email',
22
        "finances",
23
    ];
24
25
    protected static function boot()
26
    {
27
        parent::boot();
28
29
        static::addGlobalScope(new NameBeginsWith);
30
    }
31
32
    public function books() : HasMany
33
    {
34
        return $this->hasMany(Book::class);
35
    }
36
37
    public function printers() : HasManyThrough
38
    {
39
        return $this->hasManyThrough(Printer::class, Book::class);
40
    }
41
42
    public function profile() : HasOne
43
    {
44
        return $this->hasOne(Profile::class);
45
    }
46
47
    public function getLatestBookAttribute()
48
    {
49
        return $this
50
            ->books()
51
            ->latest("id")
52
            ->first();
53
    }
54
55
    public function scopeStartsWithA(Builder $query) : Builder
56
    {
57
        return $query->where('name', 'LIKE', 'A%');
58
    }
59
60
    public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder
61
    {
62
        return $query->where("name", "LIKE", "{$startOfName}%");
63
    }
64
}
65