From 6d964be3df54c0741187b75a7b46b7f38559466a Mon Sep 17 00:00:00 2001 From: phrshteh Date: Tue, 9 Jul 2024 16:39:56 +0330 Subject: [PATCH] feat: make sender parameter required for faraz sms --- config/sms.php | 2 +- src/Drivers/FarazSms/FarazSms.php | 10 ++++++++-- tests/FarazSmsTest.php | 8 ++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/config/sms.php b/config/sms.php index a771594..0bc4130 100644 --- a/config/sms.php +++ b/config/sms.php @@ -14,6 +14,6 @@ 'faraz_sms' => [ 'driver_class' => \Omalizadeh\Sms\Drivers\FarazSms\FarazSms::class, 'api_key' => '', - 'default_sender' => env('FARAZ_DEFAULT_SENDER', '+983000505') + 'default_sender' => env('FARAZ_DEFAULT_SENDER') ], ]; diff --git a/src/Drivers/FarazSms/FarazSms.php b/src/Drivers/FarazSms/FarazSms.php index 8f435bd..34ef6fc 100644 --- a/src/Drivers/FarazSms/FarazSms.php +++ b/src/Drivers/FarazSms/FarazSms.php @@ -97,8 +97,14 @@ public function getBulkSmsUrl(): string protected function mergeSmsOptions(array $data, array $options): array { + $sender = $options['sender'] ?? $this->getConfig('default_sender'); + + if (is_null($sender)) { + throw new InvalidParameterException('sender parameter is required for Faraz sms driver.'); + } + return array_merge($data, [ - 'sender' => $options['sender'] ?? $this->getConfig('default_sender'), + 'sender' => $sender, 'time' => $options['time'] ?? now()->addSecond(), ]); } @@ -117,7 +123,7 @@ protected function mergeTemplateOptions(array $data, array $options): array protected function callApi(string $url, array $data) { if (empty($apiKey = $this->getConfig('api_key'))) { - throw new InvalidConfigurationException('invalid api_key sms provider config'); + throw new InvalidConfigurationException('invalid api_key faraz sms provider config'); } $response = Http::asJson()->acceptJson()->withHeaders([ diff --git a/tests/FarazSmsTest.php b/tests/FarazSmsTest.php index b257527..2dc9798 100644 --- a/tests/FarazSmsTest.php +++ b/tests/FarazSmsTest.php @@ -74,4 +74,12 @@ public function test_invalid_response_structure_exception() $this->farazSms->send('09123456789', 'Test message', ['sender' => '1000']); } + + public function test_missing_sender_option_leads_to_exception() + { + $this->expectException(\Omalizadeh\Sms\Exceptions\InvalidParameterException::class); + $this->expectExceptionMessage('sender parameter is required for Faraz sms driver.'); + + $this->farazSms->send('09123456789', 'Test message'); + } }