Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
transportsms.php
1<?php
10
13
17
18Loc::loadMessages(__FILE__);
19
25{
27
29 protected $configuration;
30
32 protected $limiters;
33
35 protected $httpClient = array();
36
37 public function __construct()
38 {
39 $this->configuration = new Message\Configuration();
40 }
41
42 public function getName()
43 {
44 return Loc::getMessage('SENDER_INTEGRATION_SMS_TRANSPORT_NAME');
45 }
46
47 public function getCode()
48 {
49 return self::CODE;
50 }
51
58 {
59 return array(Recipient\Type::PHONE);
60 }
61
62 public function loadConfiguration()
63 {
65 }
66
68 {
69 $this->configuration = $configuration;
70 }
71
72 public function start()
73 {
74 $clientOptions = array(
75 'waitResponse' => false,
76 'socketTimeout' => 5,
77 );
78 $this->httpClient = new HttpClient($clientOptions);
79 $this->httpClient->setTimeout(5);
80 }
81
82 public function send(Message\Adapter $message)
83 {
84 $sender = $message->getConfiguration()->get('SENDER');
85 list($senderId, $from) = explode(':', $sender);
86 $authorId = $message->getConfiguration()->get('LETTER_CREATED_BY_ID');
87 $text = $message->getConfiguration()->get('MESSAGE_TEXT');
88 $text = $message->replaceFields($text);
89 $to = $message->getTo();
90
91 return Service::send($senderId, $from, $to, $text, $authorId);
92 }
93
94 public function end()
95 {
96
97 }
98
105 public function getLimiters(Message\iBase $message = null)
106 {
107 if (!empty($this->limiters))
108 {
109 $this->limiters[] = Transport\TimeLimiter::create()
110 ->withLetter($message);
111
112 return $this->limiters;
113 }
114
116 $smsSender = null;
117 if ($message)
118 {
119 if ($message instanceof Message\Adapter)
120 {
121 $smsSender = $message->getConfiguration()->getOption('SENDER')->getValue();
122 }
123 else
124 {
125 $smsSender = $message->getSmsSender();
126 }
127 }
128
129 $this->limiters = [];
130 $limitList = Service::getDailyLimits();
131 $senderNames = Service::getSenderNames();
132 foreach ($limitList as $limitSender => $limitData)
133 {
134 if ($smsSender && $smsSender !== $limitSender)
135 {
136 continue;
137 }
138 if (empty($limitData['limit']))
139 {
140 continue;
141 }
142
143 $this->limiters[] = Transport\CountLimiter::create()
144 ->withName('sms_per_day_' . $limitSender)
145 ->withCaption($senderNames[$limitSender])
146 ->withLimit($limitData['limit'])
147 ->withCurrent(
148 function () use ($limitSender)
149 {
150 $limitList = Service::getDailyLimits();
151 if (!isset($limitList[$limitSender]))
152 {
153 return 0;
154 }
155
156 if (!isset($limitList[$limitSender]['current']))
157 {
158 return 0;
159 }
160
161 return $limitList[$limitSender]['current'];
162 }
163
164 )
165 ->withUnit("1 " . Transport\iLimiter::DAYS)
166 ->setParameter('setupUri', Service::getLimitsUrl());
167 }
168
169 $this->limiters[] = Transport\TimeLimiter::create()
170 ->withLetter($message);
171
172 return $this->limiters;
173 }
174}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static send($senderId, $from, $to, $text, $authorId=1)
Definition service.php:99
saveConfiguration(Message\Configuration $configuration)
getLimiters(Message\iBase $message=null)