Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
transportwebhook.php
1<?php
10
14
18
19Loc::loadMessages(__FILE__);
20
26{
28
29 const MAX_BUFFER_SIZE = 200;
30
32 protected $configuration;
33
35 protected $limiter;
36
38 protected $httpClient = array();
39
41 protected $buffer = array(
42 'uri' => null,
43 'list' => array()
44 );
45
49 public function __construct()
50 {
51 $this->configuration = new Message\Configuration();
52 }
53
59 public function getName()
60 {
61 return Loc::getMessage('SENDER_INTEGRATION_WEBHOOK_TRANSPORT_NAME');
62 }
63
69 public function getCode()
70 {
71 return self::CODE;
72 }
73
80 {
81 return array(Recipient\Type::EMAIL, Recipient\Type::PHONE);
82 }
83
89 public function loadConfiguration()
90 {
92 }
93
100 {
101 $this->configuration = $configuration;
102 }
103
107 public function start()
108 {
109 $clientOptions = array(
110 'waitResponse' => true,
111 'socketTimeout' => 5,
112 );
113 $this->httpClient = new HttpClient($clientOptions);
114 $this->httpClient->setTimeout(5);
115
116 $this->resetBuffer();
117 }
118
126 public function send(Message\Adapter $message)
127 {
128 $this->buffer['uri'] = $message->getConfiguration()->get('URI');
129 $this->buffer['list'][$message->getRecipientType()][] = $message->getTo();
130
131 $count = 0;
132 $types = $this->getSupportedRecipientTypes();
133 foreach ($types as $type)
134 {
135 if (!isset($this->buffer['list'][$type]))
136 {
137 continue;
138 }
139
140 $count += count($this->buffer['list'][$type]);
141 }
142
143 if ($count >= self::MAX_BUFFER_SIZE)
144 {
145 $this->flushBuffer();
146 }
147
148 return true;
149 }
150
154 public function end()
155 {
156 $this->flushBuffer();
157 }
158
159 protected function resetBuffer()
160 {
161 $this->buffer = array(
162 'uri' => null,
163 'list' => array()
164 );
165
166 $types = $this->getSupportedRecipientTypes();
167 foreach ($types as $type)
168 {
169 $this->buffer['list'][$type] = array();
170 }
171 }
172
173 protected function flushBuffer()
174 {
175 if (!$this->buffer['uri'])
176 {
177 return;
178 }
179
180 $count = count($this->buffer['list']);
181 if ($count === 0)
182 {
183 return;
184 }
185
186 $this->httpClient->post($this->buffer['uri'], array(
187 'list' => Json::encode($this->buffer['list']),
188 ));
189
190 $this->getCountLimiter()->inc($count);
191 $this->resetBuffer();
192 }
193
200 public function getLimiters(Message\iBase $message = null)
201 {
202 return array(
203 $this->getCountLimiter()
204 );
205 }
206
207 protected function getCountLimiter()
208 {
209 if ($this->limiter === null)
210 {
211 $this->limiter = Transport\CountLimiter::create()
212 ->withName('web_hook')
213 ->withLimit(5000)
214 ->withUnit("1 " . Transport\iLimiter::DAYS)
215 ->withUnitName(Loc::getMessage('SENDER_INTEGRATION_WEBHOOK_TRANSPORT_LIMIT_PER_DAY'));
216 }
217
218 return $this->limiter;
219 }
220}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
saveConfiguration(Message\Configuration $configuration)