Passed
Push — master ( f28993...808c23 )
by Darko
11:06
created

ConsoleInfo::getWithGenre()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\BelongsTo;
7
use Illuminate\Database\Eloquent\Relations\HasMany;
8
use Laravel\Scout\Searchable;
9
10
/**
11
 * App\Models\ConsoleInfo.
12
 *
13
 * @property int $id
14
 * @property string $title
15
 * @property string|null $asin
16
 * @property string|null $url
17
 * @property int|null $salesrank
18
 * @property string|null $platform
19
 * @property string|null $publisher
20
 * @property int|null $genres_id
21
 * @property string|null $esrb
22
 * @property string|null $releasedate
23
 * @property string|null $review
24
 * @property bool $cover
25
 * @property \Carbon\Carbon|null $created_at
26
 * @property \Carbon\Carbon|null $updated_at
27
 *
28
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereAsin($value)
29
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereCover($value)
30
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereCreatedAt($value)
31
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereEsrb($value)
32
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereGenresId($value)
33
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereId($value)
34
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo wherePlatform($value)
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo wherePublisher($value)
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereReleasedate($value)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereReview($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereSalesrank($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereTitle($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereUpdatedAt($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo whereUrl($value)
42
 *
43
 * @mixin \Eloquent
44
 *
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo newModelQuery()
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo newQuery()
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ConsoleInfo query()
48
 */
49
class ConsoleInfo extends Model
50
{
51
    use Searchable;
0 ignored issues
show
Bug introduced by
The trait Laravel\Scout\Searchable requires the property $queryCallback which is not provided by App\Models\ConsoleInfo.
Loading history...
52
53
    /**
54
     * @var string
55
     */
56
    protected $table = 'consoleinfo';
57
58
    /**
59
     * @var bool
60
     */
61
    protected $dateFormat = false;
62
63
    /**
64
     * @var array
65
     */
66
    protected $guarded = [];
67
68
    public function searchableAs(): string
69
    {
70
        return 'ix_consoleinfo_title_platform_ft';
71
    }
72
73
    public function toSearchableArray(): array
74
    {
75
        return [
76
            'title' => $this->title,
77
            'platform' => $this->platform,
78
        ];
79
    }
80
81
    // ========================================
82
    // Relationships
83
    // ========================================
84
85
    /**
86
     * Get the genre for the console info.
87
     */
88
    public function genre(): BelongsTo
89
    {
90
        return $this->belongsTo(Genre::class, 'genres_id');
91
    }
92
93
    /**
94
     * Get the releases for the console info.
95
     */
96
    public function releases(): HasMany
97
    {
98
        return $this->hasMany(Release::class, 'consoleinfo_id');
99
    }
100
101
    // ========================================
102
    // Query Scopes
103
    // ========================================
104
105
    /**
106
     * Scope a query to only include consoles with covers.
107
     */
108
    public function scopeWithCover($query)
109
    {
110
        return $query->where('cover', 1);
111
    }
112
113
    /**
114
     * Scope a query to only include consoles for a specific platform.
115
     */
116
    public function scopeForPlatform($query, string $platform)
117
    {
118
        return $query->where('platform', $platform);
119
    }
120
121
    // ========================================
122
    // Static Helper Methods
123
    // ========================================
124
125
    /**
126
     * Get console info by ID with genre.
127
     */
128
    public static function getWithGenre(int $id): ?self
129
    {
130
        return static::query()
131
            ->where('consoleinfo.id', $id)
132
            ->select('consoleinfo.*', 'genres.title as genres')
133
            ->leftJoin('genres', 'genres.id', '=', 'consoleinfo.genres_id')
134
            ->first();
135
    }
136
137
    /**
138
     * Find by ASIN.
139
     */
140
    public static function findByAsin(string $asin): ?self
141
    {
142
        return static::where('asin', $asin)->first();
143
    }
144
}
145