From 6fd233a76dbc70f792b64098a2f8b1dcad310909 Mon Sep 17 00:00:00 2001 From: phrshteh Date: Mon, 1 Jul 2024 20:09:05 +0330 Subject: [PATCH] feat: make sender required --- src/Drivers/FarazSms/FarazSms.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Drivers/FarazSms/FarazSms.php b/src/Drivers/FarazSms/FarazSms.php index 0d08638..4203097 100644 --- a/src/Drivers/FarazSms/FarazSms.php +++ b/src/Drivers/FarazSms/FarazSms.php @@ -40,22 +40,23 @@ public function sendTemplate(string $phoneNumber, $template, array $options = [] $template = is_string($template) ? $template : (string)$template; $data = [ - 'to' => $phoneNumber, + 'recipient' => $phoneNumber, 'code' => $template, ]; + $data = $this->mergeSmsOptions($data, $options); $data = $this->mergeTemplateOptions($data, $options); $responseJson = $this->callApi($this->getTemplateSmsUrl(), $data); - if (empty($responseJson[1])) { + if (empty($responseJson['data']['message_id'])) { throw new SendingSmsFailedException( 'sent sms details not found in response', - $responseJson[0], + $responseJson['status'], ); } - return new SentSmsInfo($responseJson[1], 0); + return new SentSmsInfo($responseJson['data']['message_id'], 0); } public function sendBulk(array $phoneNumbers, string $message, array $options = []): BulkSentSmsInfo @@ -76,7 +77,7 @@ public function sendBulk(array $phoneNumbers, string $message, array $options = ); } - return new BulkSentSmsInfo($responseJson['data']['message_id'], 0); + return new BulkSentSmsInfo([$responseJson['data']['message_id']], 0); } public function getSingleSmsUrl(): string @@ -100,20 +101,25 @@ protected function mergeSmsOptions(array $data, array $options): array return $data; } + if (!isset($options['sender'])) { + throw new InvalidParameterException('sender parameter is required.'); + } + return array_merge($data, [ - 'sender' => $options['sender'] ?? null, - 'time' => $options['time'] ?? null, + 'sender' => $options['sender'], + 'time' => $options['time'] ?? now()->addSecond(), ]); } protected function mergeTemplateOptions(array $data, array $options): array { - //TODO:: token or variable? test it if (!isset($options['token'])) { - throw new InvalidParameterException('variable option is required when using sms with template'); + throw new InvalidParameterException('token option is required when using sms with template'); } - return array_merge($data, $options['variable']); + return array_merge($data, [ + 'variable' => $options['token'] + ]); } protected function callApi(string $url, array $data)