1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clarkeash\Doorman\Models; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
|
8
|
|
|
class Invite extends BaseInvite |
9
|
|
|
{ |
10
|
21 |
|
public function setForAttribute($for) |
11
|
|
|
{ |
12
|
21 |
|
if (is_string($for)) { |
13
|
13 |
|
$this->attributes['for'] = strtolower($for); |
14
|
|
|
} else { |
15
|
8 |
|
$this->attributes['for'] = null; |
16
|
|
|
} |
17
|
21 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Has the invite expired. |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
20 |
|
public function hasExpired(): bool |
25
|
|
|
{ |
26
|
20 |
|
if (is_null($this->valid_until)) { |
27
|
17 |
|
return false; |
28
|
|
|
} |
29
|
|
|
|
30
|
5 |
|
return $this->valid_until->isPast(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Is the invite full. |
35
|
|
|
* |
36
|
|
|
* @return bool |
37
|
|
|
*/ |
38
|
22 |
|
public function isFull(): bool |
39
|
|
|
{ |
40
|
22 |
|
if ($this->max == 0) { |
41
|
4 |
|
return false; |
42
|
|
|
} |
43
|
|
|
|
44
|
20 |
|
return $this->uses >= $this->max; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Is the invite restricted to a user. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
17 |
|
public function isRestricted(): bool |
53
|
|
|
{ |
54
|
17 |
|
return !is_null($this->for); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Is the invite restricted for a particular user. |
60
|
|
|
* |
61
|
|
|
* @param string $email |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
10 |
|
public function isRestrictedFor($email): bool |
66
|
|
|
{ |
67
|
10 |
|
return strtolower($email) == $this->for; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Can the invite be used anymore. |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
1 |
|
public function isUseless(): bool |
76
|
|
|
{ |
77
|
1 |
|
return $this->hasExpired() || $this->isFull(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Scope a query to only include expired invites. |
82
|
|
|
* |
83
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
86
|
|
|
*/ |
87
|
3 |
|
public function scopeExpired(Builder $query): Builder |
88
|
|
|
{ |
89
|
3 |
|
return $query->where('valid_until', '<', Carbon::now(config('app.timezone'))); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Scope a query to only include full invites. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
96
|
|
|
* |
97
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
98
|
|
|
*/ |
99
|
3 |
|
public function scopeFull(Builder $query): Builder |
100
|
|
|
{ |
101
|
3 |
|
return $query->where('max', '!=', 0)->whereRaw('uses = max'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Scope a query to only include useless invites. |
106
|
|
|
* |
107
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
110
|
|
|
*/ |
111
|
2 |
|
public function scopeUseless(Builder $query): Builder |
112
|
|
|
{ |
113
|
|
|
return $query |
114
|
|
|
->where(function ($q) { |
115
|
2 |
|
$this->scopeExpired($q); |
116
|
2 |
|
}) |
117
|
|
|
->orWhere(function ($q) { |
118
|
2 |
|
$this->scopeFull($q); |
119
|
2 |
|
}); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|