Skip to content

Commit

Permalink
sms logger added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Apr 8, 2024
1 parent 39ee1b5 commit 68b62e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
],
"require": {
"php": "^8.1",
"ext-curl": "*",
"ext-json": "*",
"illuminate/contracts": "^10.0",
"guzzlehttp/guzzle": "^7.2"
},
Expand Down
4 changes: 3 additions & 1 deletion src/Contracts/SmsDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ abstract class SmsDriver
/**
* @throws ValidationException
*/
public function setConfig(array $config = []): void
public function setConfig(array $config = [], string $mode = 'sandbox'): void
{
$this->mode = $mode;

$this->config = array_merge($config, $this->mergeConfig());

$this->validate();
Expand Down
24 changes: 18 additions & 6 deletions src/SmsChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@ class SmsChannel
*/
private mixed $driver;

private string $driver_code;

public Response $response;

/**
* @throws DriverNotFoundException
* @throws ValidationException
*/
private function initDriver(string $driver = null): void
{
$active = $driver ?? config('sms.default');
Expand All @@ -32,25 +38,31 @@ private function initDriver(string $driver = null): void

$driverClass = config("sms.vendors.{$active}.driver");

if ($driverClass == null || ! class_exists($driverClass)) {
if ($driverClass == null || !class_exists($driverClass)) {
throw new DriverNotFoundException("No driver configuration found by `{$active}` name.");
}

$this->driver_code = $active;

$mode = config('sms.mode', 'sandbox');

$config = config("sms.vendors.{$active}.{$mode}", []);

$this->driver = App::make($driverClass);

$this->driver->setConfig($config);
$this->driver->setConfig($config, $mode);

$this->driver->mode = $mode;
}

private function logSmsResponse(): void
{
if (config('sms.log', false)) {
Log::debug('SMS Vendor Response: ', ['status_code' => $this->response->status(), 'response' => $this->response->body()]);
Log::channel('sms')->info('Response: ', [
'vendor' => $this->driver_code,
'model' => $this->driver->mode,
'status_code' => $this->response->status(),
'response' => $this->response->body()
]);
}
}

Expand All @@ -73,8 +85,8 @@ private function validate(SmsMessage $message): void
*/
public function send(object $notifiable, Notification $notification): void
{
if (! method_exists($notification, 'toSms')) {
throw new BadMethodCallException(get_class($notification)." notification is missing the toSms(object $notifiable): SmsMessage method.");
if (!method_exists($notification, 'toSms')) {
throw new BadMethodCallException(get_class($notification) . " notification is missing the toSms(object $notifiable): SmsMessage method.");
}

try {
Expand Down
18 changes: 16 additions & 2 deletions src/SmsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laraflow\Sms;

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\ServiceProvider;

Expand All @@ -13,9 +14,11 @@ class SmsServiceProvider extends ServiceProvider
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/sms.php',
__DIR__ . '/../config/sms.php',
'sms'
);

$this->extendLoggerChannel();
}

/**
Expand All @@ -24,7 +27,7 @@ public function register(): void
public function boot(): void
{
$this->publishes([
__DIR__.'/../config/sms.php' => config_path('sms.php'),
__DIR__ . '/../config/sms.php' => config_path('sms.php'),
], 'sms-config');

$this->extendNotificationChannel();
Expand All @@ -40,4 +43,15 @@ private function extendNotificationChannel(): void
return $app->make(SmsChannel::class);
});
}

private function extendLoggerChannel()
{
Config::set('sms', [
'driver' => 'daily',
'path' => storage_path('logs/sms.log'),
'level' => 'info',
'days' => 14,
'replace_placeholders' => true,
]);
}
}

0 comments on commit 68b62e0

Please sign in to comment.