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

Fund::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
use function trans;
11
12
class Fund extends Base
13
{
14
    use Traits\DateFormatter;
15
16
    /**
17
     * Indicates if the model should be timestamped.
18
     *
19
     * @var bool
20
     */
21
    public $timestamps = false;
22
23
    /**
24
     * The attributes that are mass assignable.
25
     *
26
     * @var array
27
     */
28
    protected $fillable = [
29
        'options',
30
        'def_id',
31
        'round_id',
32
        'start_sid',
33
        'end_sid',
34
        'interest_sid',
35
    ];
36
37
    /**
38
     * The model's default values for attributes.
39
     *
40
     * @var array
41
     */
42
    protected $attributes = [
43
        'options' => '{}',
44
    ];
45
46
    /**
47
     * Get the attributes that should be cast.
48
     *
49
     * @return array<string, string>
50
     */
51
    protected function casts(): array
52
    {
53
        return [
54
            'options' => 'array',
55
            'start_date' => 'datetime:Y-m-d',
56
            'end_date' => 'datetime:Y-m-d',
57
            'interest_date' => 'datetime:Y-m-d',
58
        ];
59
    }
60
61
    /**
62
     * The relationships that should always be loaded.
63
     *
64
     * @var array
65
     */
66
    protected $with = [
67
        'def',
68
    ];
69
70
    public function title(): Attribute
71
    {
72
        return Attribute::make(
73
            get: fn() => match(true) {
74
                $this->def->type_user => $this->def->title,
0 ignored issues
show
Bug introduced by
The property type_user does not seem to exist on Siak\Tontine\Model\FundDef. 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...
75
                $this->pool !== null => $this->pool->title,
0 ignored issues
show
Bug introduced by
The property title does not seem to exist on Siak\Tontine\Model\Pool. 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...
76
                default => trans('tontine.fund.labels.default'),
77
            },
78
        );
79
    }
80
81
    public function notes(): Attribute
82
    {
83
        return Attribute::make(
84
            get: fn() => $this->def->notes,
85
        );
86
    }
87
88
    /**
89
     * Get the profit amount.
90
     *
91
     * @return Attribute
92
     */
93
    protected function profit(): Attribute
94
    {
95
        return Attribute::make(
96
            get: fn() => $this->options['profit']['amount'] ?? 0,
97
            set: function(int $amount) {
98
                $options = $this->options;
99
                $options['profit']['amount'] = $amount;
100
                // Return the fields to be set on the model.
101
                return ['options' => json_encode($options)];
102
            },
103
        );
104
    }
105
106
    /**
107
     * The "booted" method of the model.
108
     *
109
     * @return void
110
     */
111
    protected static function booted()
112
    {
113
        // Scope for fund sessions. Always applied by default.
114
        static::addGlobalScope('sessions', function (Builder $query) {
115
            $query->addSelect([
116
                'funds.*',
117
                'v.end_date',
118
                'v.start_date',
119
                'v.interest_date',
120
                'v.sessions_count',
121
            ])->join(DB::raw('v_funds as v'), 'v.fund_id', '=', 'funds.id');
122
        });
123
    }
124
125
    /**
126
     * @param Builder $query
127
     * @param Carbon $startDate
128
     * @param Carbon $endDate
129
     *
130
     * @return Builder
131
     */
132
    private function filterOnDates(Builder $query, Carbon $startDate, Carbon $endDate): Builder
133
    {
134
        return $query->where('v.end_date', '>=', $startDate)
135
            ->where('v.start_date', '<=', $endDate);
136
    }
137
138
    /**
139
     * Scope to active pools for a given round.
140
     *
141
     * @param Builder $query
142
     * @param Round $round
143
     *
144
     * @return Builder
145
     */
146
    public function scopeOfRound(Builder $query, Round $round): Builder
147
    {
148
        $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...
149
        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...
150
    }
151
152
    /**
153
     * Scope to active pools for a given session.
154
     *
155
     * @param Builder $query
156
     * @param Session $session
157
     *
158
     * @return Builder
159
     */
160
    public function scopeOfSession(Builder $query, Session $session): Builder
161
    {
162
        $query->whereHas('def', fn($q) => $q->where('guild_id', $session->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...
163
        return $this->filterOnDates($query, $session->day_date, $session->day_date);
0 ignored issues
show
Bug introduced by
$session->day_date of type string is incompatible with the type Carbon\Carbon expected by parameter $endDate of Siak\Tontine\Model\Fund::filterOnDates(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        return $this->filterOnDates($query, $session->day_date, /** @scrutinizer ignore-type */ $session->day_date);
Loading history...
Bug introduced by
$session->day_date of type string is incompatible with the type Carbon\Carbon expected by parameter $startDate of Siak\Tontine\Model\Fund::filterOnDates(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

163
        return $this->filterOnDates($query, /** @scrutinizer ignore-type */ $session->day_date, $session->day_date);
Loading history...
164
    }
165
166
    public function def()
167
    {
168
        return $this->belongsTo(FundDef::class, 'def_id');
169
    }
170
171
    public function start()
172
    {
173
        return $this->belongsTo(Session::class, 'start_sid');
174
    }
175
176
    public function end()
177
    {
178
        return $this->belongsTo(Session::class, 'end_sid');
179
    }
180
181
    public function interest()
182
    {
183
        return $this->belongsTo(Session::class, 'interest_sid');
184
    }
185
186
    public function round()
187
    {
188
        return $this->belongsTo(Round::class);
189
    }
190
191
    public function pool()
192
    {
193
        return $this->belongsTo(Pool::class);
194
    }
195
196
    public function sessions()
197
    {
198
        return $this->belongsToMany(Session::class, 'v_fund_session');
199
    }
200
201
    public function savings()
202
    {
203
        return $this->hasMany(Saving::class);
204
    }
205
206
    /**
207
     * @param  Builder  $query
208
     *
209
     * @return Builder
210
     */
211
    public function scopeReal(Builder $query): Builder
212
    {
213
        return $query->whereNull('pool_id');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $query->whereNull('pool_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...
214
    }
215
}
216