|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kurozora\Cooldown\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Kurozora\Cooldown\Models\Cooldown; |
|
9
|
|
|
|
|
10
|
|
|
class PendingCooldown |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string $name */ |
|
13
|
|
|
private $name; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Model|null $model */ |
|
16
|
|
|
private $model; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
* @param Model|null $model |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct($name, $model = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->name = $name; |
|
25
|
|
|
$this->model = $model; |
|
26
|
|
|
|
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Sets the cooldown for the given time period string. |
|
32
|
|
|
* e.g.: '5 minutes', '10 days' etc. |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $time |
|
35
|
|
|
* @return Cooldown |
|
36
|
|
|
*/ |
|
37
|
|
|
public function for($time) |
|
38
|
|
|
{ |
|
39
|
|
|
$date = now()->add($time); |
|
40
|
|
|
|
|
41
|
|
|
return $this->createOrUpdateCooldownWithDate($date); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Sets the cooldown to the given date. |
|
46
|
|
|
* |
|
47
|
|
|
* @param Carbon $date |
|
48
|
|
|
* @return Cooldown |
|
49
|
|
|
*/ |
|
50
|
|
|
public function until($date) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->createOrUpdateCooldownWithDate($date); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Deletes the cooldown instance to reset it. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function reset() |
|
59
|
|
|
{ |
|
60
|
|
|
$cooldown = $this->findCooldownWithName($this->name); |
|
61
|
|
|
|
|
62
|
|
|
if(!$cooldown) return; |
|
63
|
|
|
|
|
64
|
|
|
try { |
|
65
|
|
|
$cooldown->delete(); |
|
66
|
|
|
} |
|
67
|
|
|
catch(Exception $e) { } |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks whether the cooldown period has passed. |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function passed() |
|
76
|
|
|
{ |
|
77
|
|
|
$cooldown = $this->findCooldownWithName($this->name); |
|
78
|
|
|
|
|
79
|
|
|
if(!$cooldown) |
|
80
|
|
|
return true; |
|
81
|
|
|
|
|
82
|
|
|
if(now() < $cooldown->expires_at) |
|
83
|
|
|
return false; |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
$cooldown->delete(); |
|
87
|
|
|
} |
|
88
|
|
|
catch(Exception $e) { } |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Checks whether the cooldown period hasn't passed yet. |
|
95
|
|
|
* |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function notPassed() |
|
99
|
|
|
{ |
|
100
|
|
|
$cooldown = $this->findCooldownWithName($this->name); |
|
101
|
|
|
|
|
102
|
|
|
if(!$cooldown) |
|
103
|
|
|
return false; |
|
104
|
|
|
|
|
105
|
|
|
if(now() < $cooldown->expires_at) |
|
106
|
|
|
return true; |
|
107
|
|
|
|
|
108
|
|
|
try { |
|
109
|
|
|
$cooldown->delete(); |
|
110
|
|
|
} |
|
111
|
|
|
catch(Exception $e) { } |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
return false; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Returns the datetime at which the cooldown expires. |
|
118
|
|
|
* |
|
119
|
|
|
* @return Carbon|null |
|
120
|
|
|
*/ |
|
121
|
|
|
public function expiresAt() |
|
122
|
|
|
{ |
|
123
|
|
|
if($this->passed()) return null; |
|
124
|
|
|
|
|
125
|
|
|
$cooldown = $this->findCooldownWithName($this->name); |
|
126
|
|
|
|
|
127
|
|
|
return $cooldown->expires_at; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Returns the underlying Cooldown model, if any. |
|
132
|
|
|
* |
|
133
|
|
|
* @return Cooldown|null |
|
134
|
|
|
*/ |
|
135
|
|
|
public function get() |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->findCooldownWithName($this->name); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns the first cooldown instance with the given name. |
|
142
|
|
|
* |
|
143
|
|
|
* @param string $name |
|
144
|
|
|
* @return null|Cooldown |
|
145
|
|
|
*/ |
|
146
|
|
|
private function findCooldownWithName($name) |
|
147
|
|
|
{ |
|
148
|
|
|
return Cooldown::where('name', $name) |
|
149
|
|
|
->where('model_type', $this->modelType()) |
|
150
|
|
|
->where('model_id', $this->modelId()) |
|
151
|
|
|
->first(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Creates a cooldown instance with the given date. |
|
156
|
|
|
* |
|
157
|
|
|
* @param Carbon $date |
|
158
|
|
|
* @return Cooldown |
|
159
|
|
|
*/ |
|
160
|
|
|
private function createOrUpdateCooldownWithDate($date) |
|
161
|
|
|
{ |
|
162
|
|
|
// Update existing cooldown |
|
163
|
|
|
if($cooldown = $this->findCooldownWithName($this->name)) |
|
164
|
|
|
{ |
|
165
|
|
|
$cooldown->expires_at = $date; |
|
166
|
|
|
$cooldown->save(); |
|
167
|
|
|
|
|
168
|
|
|
return $cooldown; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Create a new cooldown |
|
172
|
|
|
return Cooldown::create([ |
|
173
|
|
|
'name' => $this->name, |
|
174
|
|
|
'expires_at' => $date, |
|
175
|
|
|
'model_type' => $this->modelType(), |
|
176
|
|
|
'model_id' => $this->modelId() |
|
177
|
|
|
]); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Returns the model's type. |
|
182
|
|
|
* |
|
183
|
|
|
* @return string|null |
|
184
|
|
|
*/ |
|
185
|
|
|
private function modelType() |
|
186
|
|
|
{ |
|
187
|
|
|
if($this->model === null) return null; |
|
188
|
|
|
|
|
189
|
|
|
return get_class($this->model); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Returns the model's ID. |
|
194
|
|
|
* |
|
195
|
|
|
* @return int|null |
|
196
|
|
|
*/ |
|
197
|
|
|
private function modelId() |
|
198
|
|
|
{ |
|
199
|
|
|
if($this->model === null) return null; |
|
200
|
|
|
|
|
201
|
|
|
return (int) $this->model->id; |
|
202
|
|
|
} |
|
203
|
|
|
} |