Skip to content

Commit 0cc982c

Browse files
committed
minor #40712 [Notifier]  Simplify LightSms implementation (jderusse)
This PR was merged into the 5.3-dev branch. Discussion ---------- [Notifier]  Simplify LightSms implementation | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - /cc @OskarStark Commits ------- 1240e9f [notifier] Simplify lightsms implementation
2 parents 8925dc7 + 1240e9f commit 0cc982c

File tree

1 file changed

+18
-41
lines changed

1 file changed

+18
-41
lines changed

src/Symfony/Component/Notifier/Bridge/LightSms/LightSmsTransport.php

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ protected function doSend(MessageInterface $message): SentMessage
100100
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
101101
}
102102

103-
$timestamp = time();
104103
$data = [
105104
'login' => $this->login,
106-
'phone' => $this->escapePhoneNumber($message->getPhone()),
107-
'text' => $message->getSubject(),
105+
'phone' => $phone = $this->escapePhoneNumber($message->getPhone()),
108106
'sender' => $this->from,
109-
'timestamp' => $timestamp,
107+
'text' => $message->getSubject(),
108+
'timestamp' => time(),
110109
];
111-
$data['signature'] = $this->generateSignature($data, $timestamp);
110+
$data['signature'] = $this->generateSignature($data);
112111

113112
$endpoint = sprintf('https://%s/external/get/send.php', $this->getEndpoint());
114113
$response = $this->client->request(
@@ -125,55 +124,33 @@ protected function doSend(MessageInterface $message): SentMessage
125124

126125
$content = $response->toArray(false);
127126

128-
// it happens if the host without www
129-
if (isset($content['']['error'])) {
130-
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg((int) $content['']['error']), $response);
131-
}
132-
133-
if (isset($content['error'])) {
134-
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg((int) $content['error']), $response);
135-
}
127+
if (0 !== (int) $content[$phone]['error'] ?? -1) {
128+
$errorCode = (int) $content['error'] ?? $content['']['error'] ?? $content[$phone]['error'] ?? -1;
129+
if (-1 === $errorCode) {
130+
throw new TransportException('Unable to send the SMS.', $response);
131+
}
136132

137-
$phone = $this->escapePhoneNumber($message->getPhone());
138-
if (32 === (int) $content[$phone]['error']) {
139-
throw new TransportException('Unable to send the SMS: '.$this->getErrorMsg((int) $content[$phone]['error']), $response);
133+
$error = self::ERROR_CODES[$errorCode] ?? self::ERROR_CODES[999];
134+
throw new TransportException('Unable to send the SMS: '.$error, $response);
140135
}
141136

142-
if (0 === (int) $content[$phone]['error']) {
143-
$sentMessage = new SentMessage($message, (string) $this);
144-
if (isset($content[$phone]['id_sms'])) {
145-
$sentMessage->setMessageId($content[$phone]['id_sms']);
146-
}
147-
148-
return $sentMessage;
137+
$sentMessage = new SentMessage($message, (string) $this);
138+
if (isset($content[$phone]['id_sms'])) {
139+
$sentMessage->setMessageId($content[$phone]['id_sms']);
149140
}
150141

151-
throw new TransportException('Unable to send the SMS.', $response);
142+
return $sentMessage;
152143
}
153144

154-
private function generateSignature(array $data, int $timestamp): string
145+
private function generateSignature(array $data): string
155146
{
156-
$params = [
157-
'timestamp' => $timestamp,
158-
'login' => $this->login,
159-
'phone' => $data['phone'],
160-
'sender' => $this->from,
161-
'text' => $data['text'],
162-
];
163-
164-
ksort($params);
165-
reset($params);
147+
ksort($data);
166148

167-
return md5(implode('', $params).$this->password);
149+
return md5(implode('', array_values($data)).$this->password);
168150
}
169151

170152
private function escapePhoneNumber(string $phoneNumber): string
171153
{
172154
return str_replace('+', '00', $phoneNumber);
173155
}
174-
175-
private function getErrorMsg(int $errorCode): string
176-
{
177-
return self::ERROR_CODES[$errorCode] ?? self::ERROR_CODES[999];
178-
}
179156
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy