Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
RestrictionManager.php
1<?php
2
4
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),
114 new SmsPerPhone($message),
115 new PhonePerUser($message),
116 new UserPerPhone($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}
static getConnection($name="")