Category::scopeGlobal()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Casts\Attribute;
7
8
use function trans;
9
10
class Category extends Base
11
{
12
    /**
13
     * @const
14
     */
15
    const TYPE_OUTFLOW = 'outflow';
16
17
    /**
18
     * Indicates if the model should be timestamped.
19
     *
20
     * @var bool
21
     */
22
    public $timestamps = false;
23
24
    /**
25
     * The attributes that are mass assignable.
26
     *
27
     * @var array
28
     */
29
    protected $fillable = [
30
        'name',
31
        'item_type',
32
        'active',
33
    ];
34
35
    /**
36
     * @return Attribute
37
     */
38
    protected function name(): Attribute
39
    {
40
        return Attribute::make(
41
            get: fn($value) => !$this->guild ? trans('meeting.category.types.' . $value) : $value,
42
        );
43
    }
44
45
    /**
46
     * @return Attribute
47
     */
48
    protected function isOther(): Attribute
49
    {
50
        return Attribute::make(
51
            get: fn() => $this->attributes['name'] === 'other',
52
        );
53
    }
54
55
    public function guild()
56
    {
57
        return $this->belongsTo(Guild::class);
58
    }
59
60
    /**
61
     * @param  Builder  $query
62
     *
63
     * @return Builder
64
     */
65
    public function scopeGlobal(Builder $query): Builder
66
    {
67
        return $query->whereNull('guild_id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereNull('guild_id') could return the type Illuminate\Database\Query\Builder which is incompatible with the type-hinted return Illuminate\Database\Eloquent\Builder. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70
    /**
71
     * @param  Builder  $query
72
     *
73
     * @return Builder
74
     */
75
    public function scopeOutflow(Builder $query): Builder
76
    {
77
        return $query->where('item_type', self::TYPE_OUTFLOW);
78
    }
79
80
    /**
81
     * @param  Builder  $query
82
     *
83
     * @return Builder
84
     */
85
    public function scopeActive(Builder $query): Builder
86
    {
87
        return $query->where('active', true);
88
    }
89
}
90