ChargeDef::isFixed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Database\Eloquent\Casts\Attribute;
6
use Illuminate\Database\Eloquent\Builder;
7
use Siak\Tontine\Model\Traits\HasCurrency;
8
9
use function intval;
10
11
class ChargeDef extends Base
12
{
13
    use HasCurrency;
0 ignored issues
show
Bug introduced by
The trait Siak\Tontine\Model\Traits\HasCurrency requires the property $amount which is not provided by Siak\Tontine\Model\ChargeDef.
Loading history...
14
15
    /**
16
     * @const
17
     */
18
    const TYPE_FEE = 0;
19
20
    /**
21
     * @const
22
     */
23
    const TYPE_FINE = 1;
24
25
    /**
26
     * @const
27
     */
28
    const PERIOD_NONE = 0;
29
30
    /**
31
     * @const
32
     */
33
    const PERIOD_ONCE = 1;
34
35
    /**
36
     * @const
37
     */
38
    const PERIOD_ROUND = 2;
39
40
    /**
41
     * @const
42
     */
43
    const PERIOD_SESSION = 3;
44
45
    /**
46
     * Indicates if the model should be timestamped.
47
     *
48
     * @var bool
49
     */
50
    public $timestamps = false;
51
52
    /**
53
     * The attributes that are mass assignable.
54
     *
55
     * @var array
56
     */
57
    protected $fillable = [
58
        'name',
59
        'type',
60
        'period',
61
        'amount',
62
        'active',
63
        'lendable',
64
    ];
65
66
    public function isFee(): Attribute
67
    {
68
        return Attribute::make(
69
            get: fn() => intval($this->type) === self::TYPE_FEE,
70
        );
71
    }
72
73
    public function isFine(): Attribute
74
    {
75
        return Attribute::make(
76
            get: fn() => intval($this->type) === self::TYPE_FINE,
77
        );
78
    }
79
80
    public function periodOnce(): Attribute
81
    {
82
        return Attribute::make(
83
            get: fn() => intval($this->period) === self::PERIOD_ONCE,
84
        );
85
    }
86
87
    public function periodRound(): Attribute
88
    {
89
        return Attribute::make(
90
            get: fn() => intval($this->period) === self::PERIOD_ROUND,
91
        );
92
    }
93
94
    public function periodSession(): Attribute
95
    {
96
        return Attribute::make(
97
            get: fn() => intval($this->period) === self::PERIOD_SESSION,
98
        );
99
    }
100
101
    public function isFixed(): Attribute
102
    {
103
        return Attribute::make(
104
            get: fn() => intval($this->period) !== self::PERIOD_NONE,
105
        );
106
    }
107
108
    public function isVariable(): Attribute
109
    {
110
        return Attribute::make(
111
            get: fn() => intval($this->period) === self::PERIOD_NONE,
112
        );
113
    }
114
115
    public function hasAmount(): Attribute
116
    {
117
        return Attribute::make(
118
            get: fn() => $this->is_fixed || $this->amount > 0,
0 ignored issues
show
Bug introduced by
The property is_fixed does not seem to exist on Siak\Tontine\Model\ChargeDef. 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...
119
        );
120
    }
121
122
    public function guild()
123
    {
124
        return $this->belongsTo(Guild::class);
125
    }
126
127
    public function charges()
128
    {
129
        return $this->hasMany(Charge::class, 'def_id');
130
    }
131
132
    /**
133
     * @param  Builder  $query
134
     *
135
     * @return Builder
136
     */
137
    public function scopeFee(Builder $query): Builder
138
    {
139
        return $query->where('type', self::TYPE_FEE);
140
    }
141
142
    /**
143
     * @param  Builder  $query
144
     *
145
     * @return Builder
146
     */
147
    public function scopeFine(Builder $query): Builder
148
    {
149
        return $query->where('type', self::TYPE_FINE);
150
    }
151
152
    /**
153
     * @param  Builder  $query
154
     *
155
     * @return Builder
156
     */
157
    public function scopeOnce(Builder $query): Builder
158
    {
159
        return $query->where('type', self::TYPE_FEE)
160
            ->where('period', self::PERIOD_ONCE);
161
    }
162
163
    /**
164
     * @param  Builder  $query
165
     *
166
     * @return Builder
167
     */
168
    public function scopeRound(Builder $query): Builder
169
    {
170
        return $query->where('type', self::TYPE_FEE)
171
            ->where('period', self::PERIOD_ROUND);
172
    }
173
174
    /**
175
     * @param  Builder  $query
176
     *
177
     * @return Builder
178
     */
179
    public function scopeSession(Builder $query): Builder
180
    {
181
        return $query->where('type', self::TYPE_FEE)
182
            ->where('period', self::PERIOD_SESSION);
183
    }
184
185
    /**
186
     * @param  Builder  $query
187
     * @param  bool     $active
188
     *
189
     * @return Builder
190
     */
191
    public function scopeActive(Builder $query, bool $active = true): Builder
192
    {
193
        return $query->where('active', $active);
194
    }
195
196
    /**
197
     * @param  Builder  $query
198
     *
199
     * @return Builder
200
     */
201
    public function scopeFixed(Builder $query): Builder
202
    {
203
        return $query->where('period', '!=', self::PERIOD_NONE);
204
    }
205
206
    /**
207
     * @param  Builder  $query
208
     *
209
     * @return Builder
210
     */
211
    public function scopeVariable(Builder $query): Builder
212
    {
213
        return $query->where('period', self::PERIOD_NONE);
214
    }
215
}
216