Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
transportwebhook.php
1
<?php
9
namespace
Bitrix\Sender\Integration\Sender\WebHook
;
10
11
use
Bitrix\Main\Localization\Loc
;
12
use
Bitrix\Main\Web\HttpClient
;
13
use
Bitrix\Main\Web\Json
;
14
15
use
Bitrix\Sender\Message
;
16
use
Bitrix\Sender\Transport
;
17
use
Bitrix\Sender\Recipient
;
18
19
Loc::loadMessages
(__FILE__);
20
25
class
TransportWebHook
implements
Transport\iBase
,
Transport\iLimitation
26
{
27
const
CODE
=
self::CODE_WEB_HOOK
;
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
79
public
function
getSupportedRecipientTypes
()
80
{
81
return
array(Recipient\Type::EMAIL, Recipient\Type::PHONE);
82
}
83
89
public
function
loadConfiguration
()
90
{
91
return
$this->configuration
;
92
}
93
99
public
function
saveConfiguration
(
Message
\
Configuration
$configuration
)
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
}
Bitrix\Main\Config\Configuration
Definition
configuration.php:7
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\loadMessages
static loadMessages($file)
Definition
loc.php:64
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\Web\HttpClient
Definition
httpclient.php:24
Bitrix\Main\Web\Json
Definition
json.php:11
Bitrix\Sender\Entity\Message
Definition
message.php:27
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook
Definition
transportwebhook.php:26
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\CODE
const CODE
Definition
transportwebhook.php:27
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\__construct
__construct()
Definition
transportwebhook.php:49
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\flushBuffer
flushBuffer()
Definition
transportwebhook.php:173
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\$httpClient
$httpClient
Definition
transportwebhook.php:38
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\getName
getName()
Definition
transportwebhook.php:59
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\getSupportedRecipientTypes
getSupportedRecipientTypes()
Definition
transportwebhook.php:79
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\MAX_BUFFER_SIZE
const MAX_BUFFER_SIZE
Definition
transportwebhook.php:29
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\send
send(Message\Adapter $message)
Definition
transportwebhook.php:126
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\resetBuffer
resetBuffer()
Definition
transportwebhook.php:159
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\end
end()
Definition
transportwebhook.php:154
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\loadConfiguration
loadConfiguration()
Definition
transportwebhook.php:89
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\$configuration
$configuration
Definition
transportwebhook.php:32
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\getLimiters
getLimiters(Message\iBase $message=null)
Definition
transportwebhook.php:200
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\getCode
getCode()
Definition
transportwebhook.php:69
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\$limiter
$limiter
Definition
transportwebhook.php:35
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\saveConfiguration
saveConfiguration(Message\Configuration $configuration)
Definition
transportwebhook.php:99
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\getCountLimiter
getCountLimiter()
Definition
transportwebhook.php:207
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\$buffer
$buffer
Definition
transportwebhook.php:41
Bitrix\Sender\Integration\Sender\WebHook\TransportWebHook\start
start()
Definition
transportwebhook.php:107
Bitrix\Sender\Message\Adapter
Definition
adapter.php:21
Bitrix\Sender\Message\iBase
Definition
ibase.php:16
Bitrix\Sender\Message\iBase\CODE_WEB_HOOK
const CODE_WEB_HOOK
Definition
ibase.php:19
Bitrix\Sender\Transport\iLimitation
Definition
ilimitation.php:18
Bitrix\Sender\Transport\iLimiter\DAYS
const DAYS
Definition
ilimiter.php:18
Bitrix\Sender\Integration\Sender\WebHook
Definition
messagewebhook.php:9
Bitrix\Sender\Message
Definition
adapter.php:9
Bitrix\Sender\Recipient
Definition
agent.php:8
Bitrix\Sender\Transport
Definition
adapter.php:9
modules
sender
lib
integration
sender
webhook
transportwebhook.php
Создано системой
1.10.0