Skip to content

Commit

Permalink
Merge pull request #74 from arif98741/dev
Browse files Browse the repository at this point in the history
Log driver selection and Duplicate Log Error Fixed with Other changes
  • Loading branch information
arif98741 authored May 24, 2024
2 parents e26443a + 3014e87 commit 88400f7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 62 deletions.
37 changes: 10 additions & 27 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,15 @@ SMS::via(Ssl::class)->shootWithQueue("01XXXXXXXXX",'test sms');
</pre>

# 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.
Expand All @@ -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!');
Expand All @@ -145,27 +149,6 @@ array:6 [▼
--------------------------------------------------
</pre>

## MimSms

<pre>
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();
</pre>

## 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.
Expand Down
14 changes: 10 additions & 4 deletions src/Job/SendSmsJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log as LaravelLog;
use JsonException;
use Psr\Http\Message\ResponseInterface;
use Xenon\LaravelBDSms\Facades\Logger;

class SendSmsJob implements ShouldQueue
Expand Down Expand Up @@ -64,10 +64,11 @@ public function handle()
{

if ($this->jobDetails['method'] == 'post') {
$this->postMethodHandler();
$this->postMethodHandler();
} else {
$this->getMethodHandler();
}

$this->getMethodHandler();
}

/**
Expand Down Expand Up @@ -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);
}
}
}

Expand Down
81 changes: 50 additions & 31 deletions src/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

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;
use Xenon\LaravelBDSms\Handler\RenderException;
Expand Down Expand Up @@ -70,17 +73,30 @@ 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.
*/
private array $headers;
private bool $contentTypeJson;

/**
* 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;
}
Expand Down Expand Up @@ -196,6 +212,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
Expand All @@ -208,7 +225,6 @@ public function setHeaders(array $headers, bool $contentTypeJson = true): Sender
/**
* Send Message Finally
* @throws ParameterException
* @throws \JsonException
* @since v1.0.5
*/
public function send()
Expand Down Expand Up @@ -253,6 +269,7 @@ public function getMobile()
/**
* @param mixed $mobile
* @return Sender
* @throws RenderException
* @since v1.0.0
*/
public function setMobile($mobile): Sender
Expand All @@ -273,6 +290,7 @@ public function getMessage()
/**
* @param mixed $message
* @return Sender
* @throws RenderException
* @since v1.0.0
*/
public function setMessage($message = ''): Sender
Expand All @@ -285,6 +303,7 @@ public function setMessage($message = ''): Sender
/**
* @param string $url
* @return $this
* @throws RenderException
*/
public function setUrl(string $url)
{
Expand Down Expand Up @@ -313,12 +332,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. ");
}
Expand All @@ -339,18 +352,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']) {

Expand All @@ -362,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);
}
}
}

Expand Down

0 comments on commit 88400f7

Please sign in to comment.