From 07c740df6a0a883deed21b8080791721a7f7a0a2 Mon Sep 17 00:00:00 2001 From: Ariful Islam Date: Fri, 24 May 2024 16:29:39 +0600 Subject: [PATCH 1/2] fix: removed duplicate log insert error --- src/Job/SendSmsJob.php | 6 +++--- src/Sender.php | 46 ++++++++++++++++++++++-------------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/src/Job/SendSmsJob.php b/src/Job/SendSmsJob.php index 7e6d4d8..b7022a4 100644 --- a/src/Job/SendSmsJob.php +++ b/src/Job/SendSmsJob.php @@ -11,7 +11,6 @@ use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Config; use JsonException; -use Psr\Http\Message\ResponseInterface; use Xenon\LaravelBDSms\Facades\Logger; class SendSmsJob implements ShouldQueue @@ -64,10 +63,11 @@ public function handle() { if ($this->jobDetails['method'] == 'post') { - $this->postMethodHandler(); + $this->postMethodHandler(); + } else { + $this->getMethodHandler(); } - $this->getMethodHandler(); } /** diff --git a/src/Sender.php b/src/Sender.php index ade6acf..2f575c9 100644 --- a/src/Sender.php +++ b/src/Sender.php @@ -14,6 +14,8 @@ use Exception; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\File; +use JsonException; use Xenon\LaravelBDSms\Facades\Logger; use Xenon\LaravelBDSms\Handler\ParameterException; use Xenon\LaravelBDSms\Handler\RenderException; @@ -70,17 +72,27 @@ class Sender private $queueName = 'default'; + /* + |------------------------------------------------------------------------------------------ + | Instance of Sender class + |------------------------------------------------------------------------------------------ + | This is the static method that controls the access to the singleton instance. On the first run, + | it creates a singleton object and places it into the static field. + | On subsequent runs, it returns the client existing object stored in the static field. This implementation + | lets you subclass the Singleton class while keeping just one instance of each subclass around. + */ /** - * This is the static method that controls the access to the singleton - * instance. On the first run, it creates a singleton object and places it - * into the static field. On subsequent runs, it returns the client existing - * object stored in the static field. - * - * This implementation lets you subclass the Singleton class while keeping - * just one instance of each subclass around. + * @throws RenderException */ public static function getInstance(): Sender { + if (!File::exists(config_path('sms.php'))) { + throw new RenderException("missing config/sms.php. Be sure to run + 'php artisan vendor:publish --provider=Xenon\LaravelBDSms\LaravelBDSmsServiceProvider' + and also set provider using setProvider() method. Set default provider from config/sms.php if + you use Xenon\LaravelBDSms\Facades\SMS::shoot() facade. You can also clear your cache"); + } + if (!isset(self::$instance)) { self::$instance = new self; } @@ -196,6 +208,7 @@ public function getQueue() * @param array $headers * @param bool $contentTypeJson * @return Sender + * @throws RenderException * @since v1.0.55.0-beta */ public function setHeaders(array $headers, bool $contentTypeJson = true): Sender @@ -208,7 +221,6 @@ public function setHeaders(array $headers, bool $contentTypeJson = true): Sender /** * Send Message Finally * @throws ParameterException - * @throws \JsonException * @since v1.0.5 */ public function send() @@ -253,6 +265,7 @@ public function getMobile() /** * @param mixed $mobile * @return Sender + * @throws RenderException * @since v1.0.0 */ public function setMobile($mobile): Sender @@ -273,6 +286,7 @@ public function getMessage() /** * @param mixed $message * @return Sender + * @throws RenderException * @since v1.0.0 */ public function setMessage($message = ''): Sender @@ -285,6 +299,7 @@ public function setMessage($message = ''): Sender /** * @param string $url * @return $this + * @throws RenderException */ public function setUrl(string $url) { @@ -313,12 +328,6 @@ public function setProvider($providerClass): Sender try { - if ($providerClass === null) { - throw new RenderException("Provider is empty. Be sure to run 'php artisan vendor:publish --provider=Xenon\LaravelBDSms\LaravelBDSmsServiceProvider' - and also set provider using setProvider() method. Set default provider from config/sms.php - if you use Xenon\LaravelBDSms\Facades\SMS::shoot() facade. You can also clear your cache"); - } - if (!class_exists($providerClass)) { throw new RenderException("Sms Gateway Provider '$providerClass' not found. "); } @@ -339,18 +348,11 @@ public function setProvider($providerClass): Sender * @param $config * @param $response * @return void - * @throws \JsonException + * @throws JsonException * @throws RenderException */ private function logGenerate($config, $response): void { - if ($config == null) - { - throw new RenderException("Provider is empty. Be sure to run 'php artisan vendor:publish --provider=Xenon\LaravelBDSms\LaravelBDSmsServiceProvider' - and also set provider using setProvider() method. Set default provider from config/sms.php if - you use Xenon\LaravelBDSms\Facades\SMS::shoot() facade. You can also clear your cache"); - - } if ($config['sms_log']) { From 3014e87ecf5f173136eb31596f0f7546d125aba2 Mon Sep 17 00:00:00 2001 From: Ariful Islam Date: Fri, 24 May 2024 17:03:22 +0600 Subject: [PATCH 2/2] feat: log driver selection feature added - database, or file --- readme.md | 37 ++++++++++--------------------------- src/Job/SendSmsJob.php | 8 +++++++- src/Sender.php | 35 ++++++++++++++++++++++++++--------- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/readme.md b/readme.md index 03679e4..c32d98e 100644 --- a/readme.md +++ b/readme.md @@ -103,11 +103,15 @@ SMS::via(Ssl::class)->shootWithQueue("01XXXXXXXXX",'test sms'); # Log Generate -You can generate log in database for every sms api request and save in database. For doing this. Follow below points -1. Be confirm you have completed **step-2** and **step-3** -2. Run command ``php artisan migrate``. This will create ``lbs_log`` table in your database -3. Go to your project directory and locate ``config/sms.php`` -4. Find and make true ``'sms_log' => true,`` +You can generate log for every sms api request and save in database or file. For doing this. Follow below points +1. Laravelbdsms stores log in two drivers(`database, file`). `database` is default. You can change it from _config/sms.php_ +2. Find and make true `'sms_log' => true,` +3. Be confirm you have completed **step-2** and **step-3** +4. For `database` driver + 1. Change log driver to `log_driver =>'database'` from `config/sms.php` + 2. Run command `php artisan migrate`. This will create `lbs_log` table in your database +5. For `file` driver + 1. Change log driver to `log_driver =>'file'` from `config/sms.php` Otherwise, if you want more control, you can use the underlying sender object. This will not touch any laravel facade or service provider. @@ -119,7 +123,7 @@ use Xenon\LaravelBDSms\Provider\Ssl; use Xenon\LaravelBDSms\Sender; $sender = Sender::getInstance(); -$sender->setProvider(Ssl::class); +$sender->setProvider(Ssl::class); //change this provider class according to need $sender->setMobile('017XXYYZZAA'); //$sender->setMobile(['017XXYYZZAA','018XXYYZZAA']); $sender->setMessage('helloooooooo boss!'); @@ -145,27 +149,6 @@ array:6 [▼ -------------------------------------------------- -## MimSms - -
-use Xenon\LaravelBDSms\Provider\MimSms;
-use Xenon\LaravelBDSms\Sender;
-
-$sender = Sender::getInstance();
-$sender->setProvider(MimSms::class);
-$sender->setMobile('017XXYYZZAA');
-$sender->setMessage('This is test message');
-$sender->setQueue(true); //if you want to sent sms from queue
-$sender->setConfig(
-   [
-       'api_key' => 'api_key_goes_here',
-       'type' => 'text',
-       'senderid' => 'approved_send_id',
-   ]
-);
-
-$status = $sender->send();
-
## Sms Send Using Custom Gateway We have tried to added most of the gateways of Bangladesh in this package as much as possible. But still if you don't find your expected gateway in this list, then use Custom Gateway using following code snippet. diff --git a/src/Job/SendSmsJob.php b/src/Job/SendSmsJob.php index b7022a4..b98e86c 100644 --- a/src/Job/SendSmsJob.php +++ b/src/Job/SendSmsJob.php @@ -10,6 +10,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Facades\Log as LaravelLog; use JsonException; use Xenon\LaravelBDSms\Facades\Logger; @@ -140,7 +141,12 @@ private function insertLoggerLog(array $log): void { $config = Config::get('sms'); if ($config['sms_log']) { - Logger::createLog($log); + + if ($config['log_driver'] === 'database') { + Logger::createLog($log); + } else if ($config['log_driver'] === 'file') { + LaravelLog::info('laravelbdsms',$log); + } } } diff --git a/src/Sender.php b/src/Sender.php index 2f575c9..ae100aa 100644 --- a/src/Sender.php +++ b/src/Sender.php @@ -15,6 +15,7 @@ use Exception; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Log as LaravelLog; use JsonException; use Xenon\LaravelBDSms\Facades\Logger; use Xenon\LaravelBDSms\Handler\ParameterException; @@ -81,6 +82,9 @@ class Sender | On subsequent runs, it returns the client existing object stored in the static field. This implementation | lets you subclass the Singleton class while keeping just one instance of each subclass around. */ + private array $headers; + private bool $contentTypeJson; + /** * @throws RenderException */ @@ -364,15 +368,28 @@ private function logGenerate($config, $response): void $providerResponse = $object->response; - Logger::createLog([ - 'provider' => get_class($this->provider), - 'request_json' => json_encode([ - 'config' => $config['providers'][get_class($this->provider)], - 'mobile' => $this->getMobile(), - 'message' => $this->getMessage() - ], JSON_THROW_ON_ERROR), - 'response_json' => json_encode($providerResponse, JSON_THROW_ON_ERROR) - ]); + $providerClass = get_class($this->provider); + $requestData = [ + 'config' => $config['providers'][$providerClass], + 'mobile' => $this->getMobile(), + 'message' => $this->getMessage() + ]; + + if ($config['log_driver'] === 'database') { + $logData = [ + 'provider' => $providerClass, + 'request_json' => json_encode($requestData, JSON_THROW_ON_ERROR), + 'response_json' => json_encode($providerResponse, JSON_THROW_ON_ERROR) + ]; + Logger::createLog($logData); + } elseif ($config['log_driver'] === 'file') { + $logData = [ + 'provider' => $providerClass, + 'request_json' => $requestData, + 'response_json' => $providerResponse, + ]; + LaravelLog::info('laravelbdsms', $logData); + } } }