1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
RestrictionManager.php
См. документацию.
1<?php
2
3namespace Bitrix\MessageService\Restriction;
4
5use Bitrix\Main\Application;
6use Bitrix\Main\Config\Option;
7use Bitrix\Main\Loader;
8use Bitrix\Main\Type\Date;
9use Bitrix\MessageService\Internal\Entity\RestrictionTable;
10use Bitrix\MessageService\Message;
11
13{
14 private const OPTION_NAME = 'network_restrictions_enable';
15
17 private array $restrictions;
18 private Base $notPassedRestriction;
19
20 public static function canUse(): bool
21 {
22 return Option::get('messageservice', self::OPTION_NAME, 'N') === 'Y';
23 }
24
25 public static function enableRestrictions(): void
26 {
27 Option::set('messageservice', self::OPTION_NAME, 'Y');
28 }
29
30 public static function disableRestrictions(): void
31 {
32 Option::set('messageservice', self::OPTION_NAME, 'N');
33 }
34
35 public function __construct(Message $message)
36 {
37 $this->restrictions = $this->registerRestrictions($message);
38 }
39
40 public function isCanSendMessage(): bool
41 {
42 if (empty($this->restrictions) || !self::canUse())
43 {
44 return true;
45 }
46
47 try
48 {
49 $this->lockRestrictions();
50
51 $codes = array_keys($this->restrictions);
52 $query = RestrictionTable::query()
53 ->setSelect([
54 'CODE',
55 'COUNTER',
56 'ADDITIONAL_PARAMS'
57 ])
58 ->whereIn('CODE', $codes)
59 ->where('DATE_CREATE', new Date())
60 ->setLimit(count($this->restrictions))
61 ;
62
63 foreach($query->exec() as $row)
64 {
65 $code = $row['CODE'];
66
67 $this->restrictions[$code]
68 ->setCounter($row['COUNTER'])
69 ->setAdditionalParams($row['ADDITIONAL_PARAMS'])
70 ;
71 }
72
73 if (!$this->checkRestrictions())
74 {
75 return false;
76 }
77
78 $connection = Application::getConnection();
79 $connection->startTransaction();
80
81 foreach($this->restrictions as $restriction)
82 {
83 if (!$restriction->increase())
84 {
85 $this->notPassedRestriction = $restriction;
86 $connection->rollbackTransaction();
87
88 return false;
89 }
90 }
91 $connection->commitTransaction();
92
93 return true;
94 }
95 finally
96 {
97 $this->unlockRestrictions();
98 if (isset($this->notPassedRestriction))
99 {
100 $this->notPassedRestriction->log();
101 }
102 }
103 }
104
108 private function registerRestrictions(Message $message): array
109 {
110 //TODO Put it in configs
112 $restrictions = [
113 new SmsPerUser($message),
117 new IpPerUser($message),
118 new IpPerPhone($message),
119 new SmsPerIp($message),
120 ];
121
122 $result = [];
123 foreach($restrictions as $restriction)
124 {
125 if ($restriction->canUse())
126 {
127 $result[$restriction->getEntityId()] = $restriction;
128 }
129 }
130
131 return $result;
132 }
133
134 private function lockRestrictions(): void
135 {
136 foreach($this->restrictions as $restriction)
137 {
138 $restriction->lock();
139 }
140 }
141
142 private function unlockRestrictions(): void
143 {
144 foreach($this->restrictions as $restriction)
145 {
146 $restriction->unlock();
147 }
148 }
149
150 private function checkRestrictions(): bool
151 {
152 foreach($this->restrictions as $restriction)
153 {
154 if (!$restriction->isCanSend())
155 {
156 $this->notPassedRestriction = $restriction;
157 return false;
158 }
159 }
160
161 return true;
162 }
163}
$connection
Определения actionsdefinitions.php:38
Определения date.php:9
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$query
Определения get_search.php:11
if(!is_null($config))($config as $configItem)(! $configItem->isVisible()) $code
Определения options.php:195
$message
Определения payment.php:8
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936