Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
adapter.php
1<?php
10
15
20class Adapter implements iBase, iLimitation
21{
23 protected $transport;
24
26 protected static $list;
27
29 protected $startResult = null;
30
31
33 protected $isStarted = false;
34
36 protected $isEnded = false;
37
39 protected $limiters = null;
40
42 protected $sendCount = 0;
43
51 public static function getInstance($code)
52 {
53 return isset(self::$list[$code]) ? self::$list[$code] : self::create($code);
54 }
55
63 public static function create($code)
64 {
65 $transport = Factory::getTransport($code);
67 if (!$transport)
68 {
69 throw new ArgumentException($code);
70 }
71
72 return new static($transport);
73 }
74
80 public function __construct(iBase $transport)
81 {
82 $this->transport = $transport;
83 }
84
90 public function getName()
91 {
92 return $this->transport->getName();
93 }
94
100 public function getCode()
101 {
102 return $this->transport->getCode();
103 }
104
111 {
112 return $this->transport->getSupportedRecipientTypes();
113 }
114
121 public function setSendCount($sendCount)
122 {
123 $this->sendCount = $sendCount;
124 return $this;
125 }
126
132 public function getSendCount()
133 {
134 return $this->sendCount;
135 }
136
142 public function loadConfiguration()
143 {
144 return $this->transport->loadConfiguration();
145 }
146
153 {
154 $this->transport->saveConfiguration($configuration);
155 }
156 public function getConsentMaxRequests()
157 {
158 if($this->isConsentSupported())
159 {
160 return $this->transport->getConsentMaxRequests();
161 }
162 return null;
163 }
167 public function start()
168 {
169 if ($this->startResult !== null)
170 {
171 return $this->startResult;
172 }
173
174 $startResult = $this->transport->start();
175 if ($startResult === null)
176 {
177 $startResult = true;
178 }
179 $this->startResult = $startResult;
180
181 return $this->startResult;
182 }
183
191 public function send(Message\Adapter $message)
192 {
193 if (!$this->isStarted && $this->getSendCount())
194 {
195 \Bitrix\Sender\Log::stat('sending_started', $this->transport->getCode(), $message->getId());
196 }
197 $this->start();
198 $this->isStarted = true;
199
200 $result = $this->transport->send($message);
201 \Bitrix\Sender\Log::stat('item_sent', $message->getId(), $result ? 'Y' : 'N');
202 return $result;
203 }
207 public function end()
208 {
209 if ($this->isEnded)
210 {
211 return;
212 }
213
214 $this->transport->end();
215 $this->isEnded = true;
216 }
217
221 public function __destroy()
222 {
223 $this->end();
224 }
225
232 public function getDuration($message = null)
233 {
234 if (!($this->transport instanceof iDuration))
235 {
236 return 0;
237 }
238
239 return $this->transport->getDuration($message);
240 }
241
248 public function isConsentAvailable()
249 {
250 return $this->isConsentSupported();
251 }
252
257 public function isConsentSupported()
258 {
259 return $this->transport instanceof iConsent;
260 }
261
271 public function sendConsent($message, $data) : ?bool
272 {
273 if(!$this->isConsentAvailable())
274 {
275 return false;
276 }
277
278 $builder = ConsentFactory::getConsentBuilder($this->transport::CODE)->setFields($data);
279 return $this->sendConsentByBuilder($message, $builder);
280 }
281
290 public function sendTestConsent($message, $data): bool
291 {
292 if(!$this->isConsentSupported())
293 {
294 return false;
295 }
296 $builder = ConsentFactory::getTestMessageConsentBuilder($this->transport::CODE)->setFields($data);
297 return $this->sendConsentByBuilder($message, $builder);
298 }
299
301 {
302 return $builder? $this->transport->sendConsent($message, $builder) : false;
303 }
309 public function hasLimiters()
310 {
311 return count($this->getLimiters()) > 0;
312 }
313
320 public function getLimiters(Message\iBase $message = null)
321 {
322 if ($this->limiters === null)
323 {
324 if (!($this->transport instanceof iLimitation))
325 {
326 $this->limiters = array();
327 }
328 else
329 {
330 $this->limiters = $this->transport->getLimiters($message);
331 }
332 }
333
334 return $this->limiters;
335 }
336
343 public function isLimitsExceeded(Message\iBase $message = null)
344 {
345 foreach ($this->getLimiters($message) as $limiter)
346 {
347 if ($limiter->getCurrent() < $limiter->getLimit())
348 {
349 continue;
350 }
351
352 return true;
353 }
354
355 return false;
356 }
357
364 public function getExceededLimiter(Message\iBase $message = null)
365 {
366 foreach ($this->getLimiters($message) as $limiter)
367 {
368 if ($limiter->getCurrent() < $limiter->getLimit())
369 {
370 continue;
371 }
372
373 return $limiter;
374 }
375
376 return null;
377 }
378}
sendConsent($message, $data)
Definition adapter.php:271
send(Message\Adapter $message)
Definition adapter.php:191
sendConsentByBuilder(Message\Adapter $message, AbstractConsentMessageBuilder $builder)
Definition adapter.php:300
isLimitsExceeded(Message\iBase $message=null)
Definition adapter.php:343
getLimiters(Message\iBase $message=null)
Definition adapter.php:320
sendTestConsent($message, $data)
Definition adapter.php:290
__construct(iBase $transport)
Definition adapter.php:80
saveConfiguration(Message\Configuration $configuration)
Definition adapter.php:152
getExceededLimiter(Message\iBase $message=null)
Definition adapter.php:364