Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
dummyhttp.php
1<?php
3
11
16{
17 public const ID = 'dummy_http';
18
19 public function getId()
20 {
21 return static::ID;
22 }
23
24 public function getName()
25 {
26 return 'Dummy HTTP SMS';
27 }
28
29 public function getShortName()
30 {
31 return $this->getName();
32 }
33
34 public function getFromList()
35 {
36 return [
37 [
38 'id' => 'test',
39 'name' => 'test',
40 ]
41 ];
42 }
43
44 public function sendMessage(array $messageFields): Sender\Result\SendMessage
45 {
46 $messageFields['MESSAGE_BODY'] = $this->prepareMessageBodyForSend($messageFields['MESSAGE_BODY']);
47 $result = new Sender\Result\SendMessage();
48 $host = $this->getOption('remoteHost');
49 if (!$host)
50 {
51 return $result->addError(new Error('Provider is not configured'));
52 }
53 $requestBody = Json::encode($messageFields);
54
55 $result->setServiceRequest(new DTO\Request([
56 'method' => HttpClient::HTTP_POST,
57 'uri' => $host,
58 'body' => $requestBody,
59 ]));
60 $httpClient = new HttpClient();
61 $queryResult = $httpClient->query(HttpClient::HTTP_POST, $host, $requestBody);
62 if (!$queryResult)
63 {
64 $error = $httpClient->getError();
65 $errorCode = array_key_first($error);
66 $result->setServiceResponse(new DTO\Response([
67 'error' => Sender\Util::getHttpClientErrorString($httpClient)
68 ]));
69 return $result->addError(new Error($error[$errorCode], $errorCode));
70 }
71
72 $result->setServiceResponse(new DTO\Response([
73 'statusCode' => $httpClient->getStatus(),
74 'headers' => $httpClient->getHeaders()->toArray(),
75 'body' => $httpClient->getResult(),
76 ]));
77 $responseCode = $httpClient->getStatus();
78 if ($responseCode !== 200)
79 {
80 return $result->addError(new Error("HTTP response code {$responseCode}", "HTTP_{$responseCode}"));
81 }
82
83 $responseBody = $httpClient->getResult();
84 if ($responseBody == '')
85 {
86 return $result->addError(new Error("Empty response", "EMPTY_RESPONSE"));
87 }
88
89 try
90 {
91 $decoded = Json::decode($responseBody);
92 $result->setExternalId($decoded['message_id']);
93 $result->setAccepted();
94 }
95 catch (\Throwable $e)
96 {
97 $result->addError(new Error("JSON decode error", "JSON_ERROR"));
98 }
99 return $result;
100 }
101
102 public function canUse()
103 {
104 return true;
105 }
106
107 public function isRegistered()
108 {
109 return true;
110 }
111
112 public function register(array $fields)
113 {
114 $this->setOption('remoteHost', $fields['remoteHost']);
115
116 return new Result();
117 }
118
119 public function getOwnerInfo()
120 {
121 // TODO: Implement getOwnerInfo() method.
122 }
123
124 public function getRemoteHost()
125 {
126 return $this->getOption('remoteHost');
127 }
128
129 public function getExternalManageUrl()
130 {
131 return '';
132 }
133
134 public function getMessageStatus(array $messageFields)
135 {
136 $result = new Sender\Result\MessageStatus();
137 $result->setId($messageFields['ID']);
138 $result->setExternalId($messageFields['ID']);
139 $result->setStatusCode(\Bitrix\MessageService\MessageStatus::DELIVERED);
140
141 return $result;
142 }
143
144 public static function resolveStatus($serviceStatus)
145 {
146 switch ($serviceStatus)
147 {
148 case 'read':
149 case 'sent':
150 return MessageStatus::SENT;
151 case 'enqueued':
153 case 'delayed':
155 case 'delivered':
157 case 'undelivered':
159 case 'failed':
160 case 'cancelled':
162 }
163
164 return parent::resolveStatus($serviceStatus);
165 }
166}
getOption($optionName, $defaultValue=null)
prepareMessageBodyForSend(string $text)
Definition base.php:176
static getHttpClientErrorString(HttpClient $httpClient)
Definition util.php:24