Passed
Push — main ( 656b91...db5ad9 )
by Thierry
15:24
created

Closing::fund()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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 json_encode;
9
use function trans;
10
11
class Closing extends Base
12
{
13
    /**
14
     * @var string
15
     */
16
    const TYPE_ROUND = 'r';
17
18
    /**
19
     * @var string
20
     */
21
    const TYPE_INTEREST = 'i';
22
23
    /**
24
     * Indicates if the model should be timestamped.
25
     *
26
     * @var bool
27
     */
28
    public $timestamps = false;
29
30
    /**
31
     * The attributes that are mass assignable.
32
     *
33
     * @var array
34
     */
35
    protected $fillable = [
36
        'type',
37
        'options',
38
        'session_id',
39
        'fund_id',
40
    ];
41
42
    /**
43
     * The attributes that should be cast.
44
     *
45
     * @var array
46
     */
47
    protected $casts = [
48
        'options' => 'array',
49
    ];
50
51
    /**
52
     * The model's default values for attributes.
53
     *
54
     * @var array
55
     */
56
    protected $attributes = [
57
        'options' => '{}',
58
    ];
59
60
    /**
61
     * Get the profit amount.
62
     *
63
     * @return Attribute
64
     */
65
    protected function profit(): Attribute
66
    {
67
        return Attribute::make(
68
            get: fn() => $this->options['profit']['amount'] ?? 0,
69
            set: function(int $amount) {
70
                $options = $this->options;
71
                $options['profit']['amount'] = $amount;
72
                // Return the fields to be set on the model.
73
                return ['options' => json_encode($options)];
74
            },
75
        );
76
    }
77
78
    /**
79
     * @return Attribute
80
     */
81
    protected function title(): Attribute
82
    {
83
        return Attribute::make(
84
            get: fn() => trans('meeting.closing.titles.' . $this->type),
85
        );
86
    }
87
88
    /**
89
     * @return Attribute
90
     */
91
    protected function label(): Attribute
92
    {
93
        return Attribute::make(
94
            get: fn() => $this->type === Closing::TYPE_INTEREST ? 'interest' : 'round',
95
        );
96
    }
97
98
    /**
99
     * @return Attribute
100
     */
101
    protected function isRound(): Attribute
102
    {
103
        return Attribute::make(
104
            get: fn() => $this->type === Closing::TYPE_ROUND,
105
        );
106
    }
107
108
    public function session()
109
    {
110
        return $this->belongsTo(Session::class);
111
    }
112
113
    public function fund()
114
    {
115
        return $this->belongsTo(Fund::class)->withoutGlobalScope('user');
116
    }
117
118
    /**
119
     * @param  Builder  $query
120
     *
121
     * @return Builder
122
     */
123
    public function scopeRound(Builder $query): Builder
124
    {
125
        return $query->where('type', self::TYPE_ROUND);
126
    }
127
128
    /**
129
     * @param  Builder  $query
130
     *
131
     * @return Builder
132
     */
133
    public function scopeInterest(Builder $query): Builder
134
    {
135
        return $query->where('type', self::TYPE_INTEREST);
136
    }
137
}
138