Skip to content

Commit

Permalink
feat: make sender required
Browse files Browse the repository at this point in the history
  • Loading branch information
phrshteh committed Jul 1, 2024
1 parent 6e7d069 commit 6fd233a
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/Drivers/FarazSms/FarazSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 6fd233a

Please sign in to comment.