Passed
Push — main ( 3c28f3...bb1b4c )
by Thierry
06:38 queued 28s
created

GuestInvite::activeLabel()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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