@@ -80,8 +80,8 @@ class Database extends TimeLimit
80
80
public function __construct (string $ key , int $ limit , int $ seconds , UtopiaDB $ db )
81
81
{
82
82
$ this ->key = $ key ;
83
- $ time = ( int ) \date ( ' U ' , ( int ) ( \floor ( \ time () / $ seconds )) * $ seconds ); // todo: any good Idea without time()?
84
- $ this ->time = DateTime:: format (( new \ DateTime ())-> setTimestamp ( $ time ));
83
+ $ now = \ time ();
84
+ $ this ->timestamp = ( int )( $ now - ( $ now % $ seconds ));
85
85
$ this ->limit = $ limit ;
86
86
$ this ->db = $ db ;
87
87
}
@@ -121,12 +121,12 @@ public function setup(): void
121
121
* Checks if number of counts is bigger or smaller than current limit
122
122
*
123
123
* @param string $key
124
- * @param string $datetime
124
+ * @param int $timestamp
125
125
* @return int
126
126
*
127
127
* @throws \Exception
128
128
*/
129
- protected function count (string $ key , string $ datetime ): int
129
+ protected function count (string $ key , int $ timestamp ): int
130
130
{
131
131
if (0 == $ this ->limit ) { // No limit no point for counting
132
132
return 0 ;
@@ -136,11 +136,13 @@ protected function count(string $key, string $datetime): int
136
136
return $ this ->count ;
137
137
}
138
138
139
+ $ timestamp = $ this ->toDateTime ($ timestamp );
140
+
139
141
/** @var array<Document> $result */
140
- $ result = Authorization::skip (function () use ($ key , $ datetime ) {
142
+ $ result = Authorization::skip (function () use ($ key , $ timestamp ) {
141
143
return $ this ->db ->find (self ::COLLECTION , [
142
144
Query::equal ('key ' , [$ key ]),
143
- Query::equal ('time ' , [$ datetime ]),
145
+ Query::equal ('time ' , [$ timestamp ]),
144
146
]);
145
147
});
146
148
@@ -158,28 +160,29 @@ protected function count(string $key, string $datetime): int
158
160
159
161
/**
160
162
* @param string $key
161
- * @param string $datetime
163
+ * @param int $timestamp
162
164
* @return void
163
165
*
164
166
* @throws AuthorizationException|Structure|\Exception|\Throwable
165
167
*/
166
- protected function hit (string $ key , string $ datetime ): void
168
+ protected function hit (string $ key , int $ timestamp ): void
167
169
{
168
170
if (0 == $ this ->limit ) { // No limit no point for counting
169
171
return ;
170
172
}
171
173
172
- Authorization::skip (function () use ($ datetime , $ key ) {
174
+ $ timestamp = $ this ->toDateTime ($ timestamp );
175
+ Authorization::skip (function () use ($ timestamp , $ key ) {
173
176
$ data = $ this ->db ->findOne (self ::COLLECTION , [
174
177
Query::equal ('key ' , [$ key ]),
175
- Query::equal ('time ' , [$ datetime ]),
178
+ Query::equal ('time ' , [$ timestamp ]),
176
179
]);
177
180
178
181
if ($ data ->isEmpty ()) {
179
182
$ data = [
180
183
'$permissions ' => [],
181
184
'key ' => $ key ,
182
- 'time ' => $ datetime ,
185
+ 'time ' => $ timestamp ,
183
186
'count ' => 1 ,
184
187
'$collection ' => self ::COLLECTION ,
185
188
];
@@ -190,7 +193,7 @@ protected function hit(string $key, string $datetime): void
190
193
// Duplicate in case of race condition
191
194
$ data = $ this ->db ->findOne (self ::COLLECTION , [
192
195
Query::equal ('key ' , [$ key ]),
193
- Query::equal ('time ' , [$ datetime ]),
196
+ Query::equal ('time ' , [$ timestamp ]),
194
197
]);
195
198
196
199
if (!$ data ->isEmpty ()) {
@@ -246,17 +249,18 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array
246
249
/**
247
250
* Delete logs older than $timestamp seconds
248
251
*
249
- * @param string $datetime
252
+ * @param int $timestamp
250
253
* @return bool
251
254
*
252
255
* @throws AuthorizationException|\Exception
253
256
*/
254
- public function cleanup (string $ datetime ): bool
257
+ public function cleanup (int $ timestamp ): bool
255
258
{
256
- Authorization::skip (function () use ($ datetime ) {
259
+ $ timestamp = $ this ->toDateTime ($ timestamp );
260
+ Authorization::skip (function () use ($ timestamp ) {
257
261
do {
258
262
$ documents = $ this ->db ->find (self ::COLLECTION , [
259
- Query::lessThan ('time ' , $ datetime ),
263
+ Query::lessThan ('time ' , $ timestamp ),
260
264
]);
261
265
262
266
foreach ($ documents as $ document ) {
@@ -267,4 +271,9 @@ public function cleanup(string $datetime): bool
267
271
268
272
return true ;
269
273
}
274
+
275
+ protected function toDateTime (int $ timestamp ): string
276
+ {
277
+ return DateTime::format ((new \DateTime ())->setTimestamp ($ timestamp ));
278
+ }
270
279
}
0 commit comments