Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
countlimiter.php
1
<?php
8
namespace
Bitrix\Sender\Transport
;
9
10
use
Bitrix\Main\ArgumentException
;
11
use
Bitrix\Main\Config\Option
;
12
use
Bitrix\Main\SystemException
;
13
use
Bitrix\Main\Type\DateTime
;
14
use
Bitrix\Main\Localization\Loc
;
15
16
Loc::loadMessages
(__FILE__);
17
22
class
CountLimiter
implements
iLimiter
23
{
25
private
$name;
26
28
private
$caption;
29
31
private
$parameters = array();
32
34
private
$limit;
35
37
private
$current;
38
40
private
$interval;
41
43
private
$unit;
44
46
private
$unitName;
47
48
private
$hidden =
false
;
49
55
public
static
function
create
()
56
{
57
return
new
static
();
58
}
59
60
private
static
function
parseUnit($unit)
61
{
62
$unit = explode(
' '
, $unit);
63
$num = (int) $unit[0];
64
$unit = $unit[1] ??
''
;
65
66
return
array(
67
'num'
=> $num,
68
'unit'
=> $unit
69
);
70
}
71
78
public
static
function
getUnitInterval
($unit)
79
{
80
$parsed = self::parseUnit($unit);
81
82
switch
($parsed[
'unit'
])
83
{
84
case
iLimiter::MINUTES
:
85
$interval = 60;
86
break
;
87
88
case
iLimiter::HOURS
:
89
$interval = 3600;
90
break
;
91
92
case
iLimiter::MONTHS
:
93
$interval = 86400 * 31;
94
break
;
95
96
case
iLimiter::DAYS
:
97
default
:
98
$interval = 86400;
99
break
;
100
}
101
102
return
$interval * $parsed[
'num'
];
103
}
104
108
public
function
__construct
()
109
{
110
}
111
118
public
function
withName
($name)
119
{
120
$this->name = $name;
121
return
$this;
122
}
123
130
public
function
withCaption
($caption)
131
{
132
$this->caption = $caption;
133
return
$this;
134
}
135
142
public
function
withLimit
($limit)
143
{
144
$this->limit = (int) $limit;
145
return
$this;
146
}
147
154
public
function
withInterval
($interval)
155
{
156
$this->interval = (int) $interval;
157
return
$this;
158
}
159
166
public
function
withUnit
($unit)
167
{
168
$this->unit = $unit;
169
$this->interval =
self::getUnitInterval
($unit);
170
return
$this;
171
}
172
179
public
function
withUnitName
($unitName)
180
{
181
$this->unitName = $unitName;
182
return
$this;
183
}
184
192
public
function
withCurrent
($callable)
193
{
194
if
(!is_callable($callable))
195
{
196
throw
new
ArgumentException
(
'Wrong type of parameter \'callable\'.'
);
197
}
198
199
$this->current = $callable;
200
return
$this;
201
}
202
208
public
function
getInitialLimit
()
209
{
210
return
$this->limit;
211
}
212
218
public
function
getLimit
()
219
{
220
$limit = (int) $this->limit;
221
$percentage = (int) $this->
getParameter
(
'percentage'
);
222
if
($percentage > 0 && $percentage <= 100)
223
{
224
$limit *= $percentage / 100;
225
}
226
227
return
$limit;
228
}
229
235
public
function
getUnit
()
236
{
237
return
$this->unit;
238
}
239
245
public
function
getUnitName
()
246
{
247
if
($this->unitName)
248
{
249
return
$this->unitName;
250
}
251
252
$parsed = self::parseUnit($this->unit);
253
switch
($parsed[
'unit'
])
254
{
255
case
iLimiter::MINUTES
:
256
$format =
'idiff'
;
257
break
;
258
259
case
iLimiter::HOURS
:
260
$format =
'Hdiff'
;
261
break
;
262
263
case
iLimiter::MONTHS
:
264
$format =
'mdiff'
;
265
break
;
266
267
case
iLimiter::DAYS
:
268
default
:
269
$format =
'ddiff'
;
270
break
;
271
}
272
273
274
$formatted = \FormatDate($format, $this->getCurrentTimestamp() - $this->interval);
275
if
(mb_substr($formatted, 0, 2) ==
'1 '
)
276
{
277
$formatted = mb_substr($formatted, 2);
278
}
279
280
return
Loc::getMessage
(
'SENDER_TRANSPORT_COUNT_LIMIT_UNIT_DATE_AT'
) .
' '
. $formatted;
281
}
282
288
public
function
getCaption
()
289
{
290
return
$this->caption;
291
}
292
298
public
function
getName
()
299
{
300
return
$this->name;
301
}
302
308
public
function
getText
()
309
{
310
return
Loc::getMessage
(
311
'SENDER_TRANSPORT_COUNT_LIMIT_TEXT_MONTHLY'
,
312
[
313
'%current%'
=> number_format($this->
getAvailable
(), 0,
'.'
,
' '
),
314
'%limit%'
=> number_format($this->
getLimit
(), 0,
'.'
,
' '
),
315
'%setupUri%'
=> $this->
getParameter
(
'setupUri'
),
316
'%setupCaption%'
=> $this->
getParameter
(
'setupCaption'
),
317
]
318
);
319
}
320
327
public
function
getParameter
($name)
328
{
329
return
isset($this->parameters[$name]) ? $this->parameters[$name] :
null
;
330
}
331
339
public
function
setParameter
($name, $value)
340
{
341
$this->parameters[$name] = $value;
342
return
$this;
343
}
344
352
public
function
inc
($amount = 1)
353
{
354
if
($this->
getTimestamp
())
355
{
356
$isNewPeriod = ($this->getCurrentTimestamp() - $this->
getTimestamp
()) >= $this->interval;
357
}
358
else
359
{
360
$isNewPeriod =
true
;
361
}
362
363
364
if
($isNewPeriod)
365
{
366
$this->setCurrent(0);
367
$this->setTimestamp($this->getCurrentTimestamp());
368
}
369
370
$current = $this->
getCurrent
() + $amount;
371
if
($current >= $this->
getLimit
())
372
{
373
throw
new
SystemException
(
Loc::getMessage
(
374
'SENDER_TRANSPORT_COUNT_LIMIT_EXCEEDED'
,
375
array(
'%limit%'
=> $this->
getLimit
(),
'%unit%'
=> $this->
getUnitName
())
376
));
377
}
378
379
if
(!$isNewPeriod)
380
{
381
$this->setCurrent($current);
382
}
383
384
return
$this;
385
}
386
392
public
function
getAvailable
()
393
{
394
$available = $this->
getLimit
() - $this->
getCurrent
();
395
return
(!$available || $available < 0) ? 0 : $available;
396
}
397
403
public
function
getCurrent
()
404
{
405
if
(is_callable($this->current))
406
{
407
return
call_user_func($this->current);
408
}
409
410
return
(
int
) Option::get(
'main'
, $this->getCounterOptionName(), 0);
411
}
412
419
private
function
setCurrent($value)
420
{
421
Option::set(
'main'
, $this->getCounterOptionName(), $value);
422
return
$this;
423
}
424
431
private
function
setTimestamp($value)
432
{
433
Option::set(
'main'
, $this->getDateOptionName(), $value);
434
return
$this;
435
}
436
442
public
function
getTimestamp
()
443
{
444
return
(
int
) Option::get(
'main'
, $this->getDateOptionName(), 0);
445
}
446
447
private
function
getCurrentTimestamp()
448
{
449
$dateTime =
new
DateTime
();
450
return
$dateTime->getTimestamp();
451
}
452
453
private
function
getCounterOptionName()
454
{
455
return
"~sender_limit_count_"
. $this->name;
456
}
457
458
private
function
getDateOptionName()
459
{
460
return
"~sender_limit_date_"
. $this->name;
461
}
462
468
public
function
setHidden
(
bool
$hidden):
CountLimiter
469
{
470
$this->hidden = $hidden;
471
return
$this;
472
}
476
public
function
isHidden
()
477
{
478
return
$this->hidden;
479
}
480
}
Bitrix\Main\ArgumentException
Definition
exception.php:34
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Sender\Transport\CountLimiter
Definition
countlimiter.php:23
Bitrix\Sender\Transport\CountLimiter\__construct
__construct()
Definition
countlimiter.php:108
Bitrix\Sender\Transport\CountLimiter\withLimit
withLimit($limit)
Definition
countlimiter.php:142
Bitrix\Sender\Transport\CountLimiter\withUnit
withUnit($unit)
Definition
countlimiter.php:166
Bitrix\Sender\Transport\CountLimiter\withCaption
withCaption($caption)
Definition
countlimiter.php:130
Bitrix\Sender\Transport\CountLimiter\getInitialLimit
getInitialLimit()
Definition
countlimiter.php:208
Bitrix\Sender\Transport\CountLimiter\create
static create()
Definition
countlimiter.php:55
Bitrix\Sender\Transport\CountLimiter\inc
inc($amount=1)
Definition
countlimiter.php:352
Bitrix\Sender\Transport\CountLimiter\setParameter
setParameter($name, $value)
Definition
countlimiter.php:339
Bitrix\Sender\Transport\CountLimiter\getName
getName()
Definition
countlimiter.php:298
Bitrix\Sender\Transport\CountLimiter\withCurrent
withCurrent($callable)
Definition
countlimiter.php:192
Bitrix\Sender\Transport\CountLimiter\withName
withName($name)
Definition
countlimiter.php:118
Bitrix\Sender\Transport\CountLimiter\getUnitInterval
static getUnitInterval($unit)
Definition
countlimiter.php:78
Bitrix\Sender\Transport\CountLimiter\getUnitName
getUnitName()
Definition
countlimiter.php:245
Bitrix\Sender\Transport\CountLimiter\getUnit
getUnit()
Definition
countlimiter.php:235
Bitrix\Sender\Transport\CountLimiter\withInterval
withInterval($interval)
Definition
countlimiter.php:154
Bitrix\Sender\Transport\CountLimiter\setHidden
setHidden(bool $hidden)
Definition
countlimiter.php:468
Bitrix\Sender\Transport\CountLimiter\getText
getText()
Definition
countlimiter.php:308
Bitrix\Sender\Transport\CountLimiter\getTimestamp
getTimestamp()
Definition
countlimiter.php:442
Bitrix\Sender\Transport\CountLimiter\getAvailable
getAvailable()
Definition
countlimiter.php:392
Bitrix\Sender\Transport\CountLimiter\getCaption
getCaption()
Definition
countlimiter.php:288
Bitrix\Sender\Transport\CountLimiter\isHidden
isHidden()
Definition
countlimiter.php:476
Bitrix\Sender\Transport\CountLimiter\getCurrent
getCurrent()
Definition
countlimiter.php:403
Bitrix\Sender\Transport\CountLimiter\getLimit
getLimit()
Definition
countlimiter.php:218
Bitrix\Sender\Transport\CountLimiter\withUnitName
withUnitName($unitName)
Definition
countlimiter.php:179
Bitrix\Sender\Transport\CountLimiter\getParameter
getParameter($name)
Definition
countlimiter.php:327
Bitrix\Sender\Transport\iLimiter
Definition
ilimiter.php:16
Bitrix\Sender\Transport\iLimiter\HOURS
const HOURS
Definition
ilimiter.php:19
Bitrix\Sender\Transport\iLimiter\MONTHS
const MONTHS
Definition
ilimiter.php:17
Bitrix\Sender\Transport\iLimiter\MINUTES
const MINUTES
Definition
ilimiter.php:20
Bitrix\Sender\Transport\iLimiter\DAYS
const DAYS
Definition
ilimiter.php:18
Bitrix\Sender\Transport
Definition
adapter.php:9
modules
sender
lib
transport
countlimiter.php
Создано системой
1.10.0