Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
countlimiter.php
1<?php
9
15
16Loc::loadMessages(__FILE__);
17
22class 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 {
85 $interval = 60;
86 break;
87
88 case iLimiter::HOURS:
89 $interval = 3600;
90 break;
91
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 {
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 {
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}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29