Passed
Pull Request — main (#63)
by Thierry
06:38
created

Round::getEndAtAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Siak\Tontine\Model;
4
5
use Illuminate\Database\Eloquent\Casts\Attribute;
6
7
class Round extends Base
8
{
9
    use Traits\HasProperty;
0 ignored issues
show
introduced by
The trait Siak\Tontine\Model\Traits\HasProperty requires some properties which are not provided by Siak\Tontine\Model\Round: $property, $content
Loading history...
10
11
    /**
12
     * @const
13
     */
14
    const STATUS_PENDING = 0;
15
16
    /**
17
     * @const
18
     */
19
    const STATUS_OPENED = 1;
20
21
    /**
22
     * @const
23
     */
24
    const STATUS_CLOSED = 2;
25
26
    /**
27
     * Indicates if the model should be timestamped.
28
     *
29
     * @var bool
30
     */
31
    public $timestamps = false;
32
33
    /**
34
     * The attributes that are mass assignable.
35
     *
36
     * @var array
37
     */
38
    protected $fillable = [
39
        'title',
40
        'status',
41
        'notes',
42
    ];
43
44
    public function startAt(): Attribute
45
    {
46
        return Attribute::make(
47
            get: function() {
48
                $startSession = $this->sessions()->orderBy('start_at')->first();
49
                return !$startSession ? null : $startSession->start_at;
50
            },
51
        );
52
    }
53
54
    public function endAt(): Attribute
55
    {
56
        return Attribute::make(
57
            get: function() {
58
                $endSession = $this->sessions()->orderByDesc('start_at')->first();
59
                return !$endSession ? null : $endSession->start_at;
60
            },
61
        );
62
    }
63
64
    public function pending(): Attribute
65
    {
66
        return Attribute::make(
67
            get: fn() => $this->status === self::STATUS_PENDING,
68
        );
69
    }
70
71
    public function opened(): Attribute
72
    {
73
        return Attribute::make(
74
            get: fn() => $this->status === self::STATUS_OPENED,
75
        );
76
    }
77
78
    public function closed(): Attribute
79
    {
80
        return Attribute::make(
81
            get: fn() => $this->status === self::STATUS_CLOSED,
82
        );
83
    }
84
85
    public function tontine()
86
    {
87
        return $this->belongsTo(Tontine::class);
88
    }
89
90
    public function pools()
91
    {
92
        return $this->hasMany(Pool::class);
93
    }
94
95
    public function sessions()
96
    {
97
        return $this->hasMany(Session::class);
98
    }
99
100
    public function bills()
101
    {
102
        return $this->hasMany(RoundBill::class);
103
    }
104
}
105