Passed
Push — main ( f9c7ef...02eb29 )
by Thierry
05:46
created

Pool::dateStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Carbon\Carbon;
6
use Illuminate\Database\Eloquent\Casts\Attribute;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Support\Facades\DB;
9
10
class Pool extends Base
11
{
12
    use Traits\DateFormatter;
13
14
    /**
15
     * Indicates if the model should be timestamped.
16
     *
17
     * @var bool
18
     */
19
    public $timestamps = false;
20
21
    /**
22
     * The attributes that are mass assignable.
23
     *
24
     * @var array
25
     */
26
    protected $fillable = [
27
        'round_id',
28
        'start_sid',
29
        'end_sid',
30
    ];
31
32
    /**
33
     * The relationships that should always be loaded.
34
     *
35
     * @var array
36
     */
37
    protected $with = [
38
        'def',
39
    ];
40
41
    /**
42
     * Get the attributes that should be cast.
43
     *
44
     * @return array<string, string>
45
     */
46
    protected function casts(): array
47
    {
48
        return [
49
            'start_date' => 'datetime:Y-m-d',
50
            'end_date' => 'datetime:Y-m-d',
51
        ];
52
    }
53
54
    public function title(): Attribute
55
    {
56
        return Attribute::make(get: fn() => $this->def->title);
57
    }
58
59
    public function amount(): Attribute
60
    {
61
        return Attribute::make(get: fn() => $this->def->amount);
62
    }
63
64
    public function notes(): Attribute
65
    {
66
        return Attribute::make(get: fn() => $this->def->notes);
67
    }
68
69
    public function depositFixed(): Attribute
70
    {
71
        return Attribute::make(
72
            get: fn() => ($this->def->properties['deposit']['fixed'] ?? true) === true,
73
        );
74
    }
75
76
    public function depositLendable(): Attribute
77
    {
78
        return Attribute::make(
79
            get: fn() => ($this->def->properties['deposit']['lendable'] ?? false) === true,
80
        );
81
    }
82
83
    public function remitPlanned(): Attribute
84
    {
85
        return Attribute::make(
86
            get: fn() => ($this->def->properties['remit']['planned'] ?? true) === true,
87
        );
88
    }
89
90
    public function remitAuction(): Attribute
91
    {
92
        return Attribute::make(
93
            get: fn() => ($this->def->properties['remit']['auction'] ?? false) === true,
94
        );
95
    }
96
97
    /**
98
     * The "booted" method of the model.
99
     *
100
     * @return void
101
     */
102
    protected static function booted()
103
    {
104
        // Scope for pool sessions. Always applied by default.
105
        static::addGlobalScope('sessions', function (Builder $query) {
106
            $query->addSelect([
107
                'pools.*',
108
                'v.end_date',
109
                'v.start_date',
110
                'v.sessions_count',
111
                'v.disabled_sessions_count'
112
            ])->join(DB::raw('v_pools as v'), 'v.pool_id', '=', 'pools.id');
113
        });
114
    }
115
116
    /**
117
     * @param Builder $query
118
     * @param Carbon $startDate
119
     * @param Carbon $endDate
120
     *
121
     * @return Builder
122
     */
123
    private function filterOnDates(Builder $query, Carbon $startDate, Carbon $endDate): Builder
124
    {
125
        return $query->where('v.end_date', '>=', $startDate)
126
            ->where('v.start_date', '<=', $endDate);
127
    }
128
129
    /**
130
     * Scope to active pools for a given round.
131
     *
132
     * @param Builder $query
133
     * @param Round $round
134
     *
135
     * @return Builder
136
     */
137
    public function scopeOfRound(Builder $query, Round $round): Builder
138
    {
139
        $query->whereHas('def', fn($q) => $q->where('guild_id', $round->guild_id));
0 ignored issues
show
Bug introduced by
The property guild_id does not exist on Siak\Tontine\Model\Round. Did you mean guild?
Loading history...
140
        return $this->filterOnDates($query, $round->start_date, $round->end_date);
0 ignored issues
show
Bug introduced by
The property start_date does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property end_date does not seem to exist on Siak\Tontine\Model\Round. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
141
    }
142
143
    /**
144
     * @param Builder $query
145
     *
146
     * @return Builder
147
     */
148
    public function scopeRemitPlanned(Builder $query): Builder
149
    {
150
        return $query->whereHas('def', fn($q) => $q->where('properties->remit->planned', true));
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereHas(...ion(...) { /* ... */ }) 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...
151
    }
152
153
    public function subscriptions()
154
    {
155
        return $this->hasMany(Subscription::class);
156
    }
157
158
    public function def()
159
    {
160
        return $this->belongsTo(PoolDef::class, 'def_id');
161
    }
162
163
    public function start()
164
    {
165
        return $this->belongsTo(Session::class, 'start_sid');
166
    }
167
168
    public function end()
169
    {
170
        return $this->belongsTo(Session::class, 'end_sid');
171
    }
172
173
    public function round()
174
    {
175
        return $this->belongsTo(Round::class);
176
    }
177
178
    public function sessions()
179
    {
180
        return $this->belongsToMany(Session::class, 'v_pool_session');
181
    }
182
183
    public function disabled_sessions()
184
    {
185
        return $this->belongsToMany(Session::class, 'pool_session_disabled');
186
    }
187
}
188