GuestInvite::scopeRefused()   A
last analyzed

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
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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 now;
9
use function trans;
10
11
class GuestInvite extends Base
12
{
13
    use Traits\DateFormatter;
14
15
    /**
16
     * @const int
17
     */
18
    const STATUS_PENDING = 0;
19
20
    /**
21
     * @const int
22
     */
23
    const STATUS_REFUSED = 1;
24
25
    /**
26
     * @const int
27
     */
28
    const STATUS_ACCEPTED = 2;
29
30
    /**
31
     * @const int
32
     */
33
    const STATUS_CANCELLED = 3;
34
35
    /**
36
     * @var array<string>
37
     */
38
    static protected $statuses = ['pending', 'refused', 'accepted', 'cancelled'];
39
40
    /**
41
     * The attributes that are mass assignable.
42
     *
43
     * @var array
44
     */
45
    protected $fillable = [
46
        'status',
47
        'active',
48
        'expires_at',
49
    ];
50
51
    /**
52
     * Get the attributes that should be cast.
53
     *
54
     * @return array<string, string>
55
     */
56
    protected function casts(): array
57
    {
58
        return [
59
            'expires_at' => 'datetime',
60
        ];
61
    }
62
63
    public function host()
64
    {
65
        return $this->belongsTo(User::class, 'host_id');
66
    }
67
68
    public function guest()
69
    {
70
        return $this->belongsTo(User::class, 'guest_id');
71
    }
72
73
    public function guilds()
74
    {
75
        return $this->belongsToMany(Guild::class,
76
            'guest_options', 'invite_id', 'guild_id')
77
            ->as('options')
78
            ->withPivot('access')
79
            ->using(GuestOptions::class);
80
    }
81
82
    /**
83
     * @return Attribute
84
     */
85
    protected function statusLabel(): Attribute
86
    {
87
        $status = $this->status === self::STATUS_PENDING && $this->expires_at < now() ?
88
            'expired' : (self::$statuses[$this->status] ?? 'unknown');
89
        return Attribute::make(
90
            get: fn() => trans("tontine.invite.status.$status"),
91
        );
92
    }
93
94
    /**
95
     * @return Attribute
96
     */
97
    protected function activeLabel(): Attribute
98
    {
99
        return Attribute::make(
100
            get: function() {
101
                if($this->status === self::STATUS_PENDING) {
102
                    $label = $this->expires_at < now() ? 'expired' : 'expires';
103
                    return trans("tontine.invite.active.$label", [
104
                        'date' => $this->date('expires_at', 'format_medium'),
105
                    ]);
106
                }
107
                if($this->status === self::STATUS_ACCEPTED) {
108
                    $label = $this->active ? 'active' : 'inactive';
109
                    return trans("tontine.invite.active.$label", [
110
                        'date' => $this->date('updated_at', 'format_medium'),
111
                    ]);
112
                }
113
                return null;
114
            },
115
        );
116
    }
117
118
    /**
119
     * @return Attribute
120
     */
121
    protected function isExpired(): Attribute
122
    {
123
        return Attribute::make(
124
            get: fn() => $this->status === self::STATUS_PENDING && $this->expires_at < now(),
125
        );
126
    }
127
128
    /**
129
     * @return Attribute
130
     */
131
    protected function isPending(): Attribute
132
    {
133
        return Attribute::make(
134
            get: fn() => $this->status === self::STATUS_PENDING,
135
        );
136
    }
137
138
    /**
139
     * @return Attribute
140
     */
141
    protected function isRefused(): Attribute
142
    {
143
        return Attribute::make(
144
            get: fn() => $this->status === self::STATUS_REFUSED,
145
        );
146
    }
147
148
    /**
149
     * @return Attribute
150
     */
151
    protected function isAccepted(): Attribute
152
    {
153
        return Attribute::make(
154
            get: fn() => $this->status === self::STATUS_ACCEPTED,
155
        );
156
    }
157
158
    /**
159
     * @return Attribute
160
     */
161
    protected function isCancelled(): Attribute
162
    {
163
        return Attribute::make(
164
            get: fn() => $this->status === self::STATUS_CANCELLED,
165
        );
166
    }
167
168
    /**
169
     * @param Builder $query
170
     *
171
     * @return Builder
172
     */
173
    public function scopeExpired(Builder $query): Builder
174
    {
175
        return $query->where('expires_at', '<', now());
176
    }
177
178
    /**
179
     * @param Builder $query
180
     *
181
     * @return Builder
182
     */
183
    public function scopePending(Builder $query): Builder
184
    {
185
        return $query->where('expires_at', '>=', now())
186
            ->where('status', self::STATUS_PENDING);
187
    }
188
189
    /**
190
     * @param Builder $query
191
     *
192
     * @return Builder
193
     */
194
    public function scopeRefused(Builder $query): Builder
195
    {
196
        return $query->where('status', self::STATUS_REFUSED);
197
    }
198
199
    /**
200
     * @param Builder $query
201
     *
202
     * @return Builder
203
     */
204
    public function scopeAccepted(Builder $query): Builder
205
    {
206
        return $query->where('status', self::STATUS_ACCEPTED);
207
    }
208
209
    /**
210
     * @param Builder $query
211
     *
212
     * @return Builder
213
     */
214
    public function scopeCancelled(Builder $query): Builder
215
    {
216
        return $query->where('status', self::STATUS_CANCELLED);
217
    }
218
219
    /**
220
     * @param Builder $query
221
     * @param User $user
222
     *
223
     * @return Builder
224
     */
225
    public function scopeOfUser(Builder $query, User $user): Builder
226
    {
227
        return $query->where(fn(Builder $_query) =>
228
            $_query->orWhere('host_id', $user->id)
229
                ->orWhere('guest_id', $user->id));
230
    }
231
}
232