Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
limitation.php
1<?php
3
6
8{
9 private static $options;
10 private static $optionName = 'sender.limitation.daily';
11 private static $defaultLimit = 0;
12
13 public static function getDailyLimits()
14 {
15 $limits = array();
16 $counts = MessageTable::getAllDailyCount();
17
18 foreach (SmsManager::getSenders() as $sender)
19 {
20 $sid = $sender->getId();
21 foreach ($sender->getFromList() as $from)
22 {
23 $key = $sid.':'.$from['id'];
24 $limits[$key] = array(
25 'senderId' => $sid,
26 'fromId' => $from['id'],
27 'limit' => static::getDailyLimit($sid, $from['id']),
28 'current' => isset($counts[$key]) ? $counts[$key] : 0
29 );
30 }
31 }
32
33 return $limits;
34 }
35
36 public static function getDailyLimit($senderId, $fromId)
37 {
38 $key = $senderId . ':' . $fromId;
39 return static::getOption($key, static::$defaultLimit);
40 }
41
42 public static function setDailyLimit($senderId, $fromId, $limit)
43 {
44 $key = $senderId . ':' . $fromId;
45 static::setOption($key, (int)$limit);
46 MessageTable::returnDeferredToQueue($senderId, $fromId);
47 return true;
48 }
49
50 public static function checkDailyLimit($senderId, $fromId)
51 {
52 $limit = static::getDailyLimit($senderId, $fromId);
53 if ($limit > 0)
54 {
55 $current = MessageTable::getDailyCount($senderId, $fromId);
56
57 return ($current < $limit);
58 }
59 return true;
60 }
61
62 public static function getRetryTime()
63 {
64 $time = static::getOption('retryTime', [
65 'h' => 9,
66 'i' => 0,
67 'auto' => true,
68 'tz' => ''
69 ]);
70
71 return $time;
72 }
73
74 public static function setRetryTime(array $params)
75 {
76 $result = [
77 'h' => (int)$params['h'],
78 'i' => (int)$params['i'],
79 'auto' => (bool)$params['auto'],
80 'tz' => (string)$params['tz'],
81 ];
82
83 static::setOption('retryTime', $result);
84 return true;
85 }
86
92 private static function setOptions(array $options)
93 {
94 static::$options = $options;
95 Option::set('messageservice', static::$optionName, serialize($options));
96 return true;
97 }
98
104 private static function getOptions()
105 {
106 if (static::$options === null)
107 {
108 $optionsString = Option::get('messageservice', static::$optionName);
109 if (\CheckSerializedData($optionsString))
110 {
111 static::$options = unserialize($optionsString, ['allowed_classes' => false]);
112 }
113
114 if (!is_array(static::$options))
115 {
116 static::$options = array();
117 }
118 }
119 return static::$options;
120 }
121
130 private static function setOption($optionName, $optionValue)
131 {
132 $options = static::getOptions();
133 if (!isset($options[$optionName]) || $options[$optionName] !== $optionValue)
134 {
135 $options[$optionName] = $optionValue;
136 static::setOptions($options);
137 }
138 return true;
139 }
140
148 private static function getOption($optionName, $defaultValue = null)
149 {
150 $options = static::getOptions();
151 return isset($options[$optionName]) ? $options[$optionName] : $defaultValue;
152 }
153
158 public static function clearOptions()
159 {
160 static::$options = array();
161 Option::delete('messageservice', array('name' => static::$optionName));
162 return true;
163 }
164
165}
static getDailyLimit($senderId, $fromId)
static setDailyLimit($senderId, $fromId, $limit)
static checkDailyLimit($senderId, $fromId)