Skip to content

Commit

Permalink
Modified api-x:log command to output table
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulmatinsannl committed Dec 9, 2018
1 parent 5f39dc5 commit e75079f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 76 deletions.
50 changes: 29 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,35 @@ An unofficial laravel package for SmartSMSSolutions' API-x. The Package that hel
Below is the file structure of this package.

```
src/
Channels/
SmartSMSChannel.php
Commands/
Log/
ClearCommand.php
DisplayCommand.php
config/
api-x.php
Controllers/
LogController.php
Exceptions/
CouldNotSendNotification.php
InvalidConfiguration.php
Facades/
APIxFacade.php
resources/
views/
log.blade.php
APIx.php
APIxServiceProvider.php
\---src
| APIx.php
| APIxMessage.php
| APIxServiceProvider.php
|
+---Channels
| SmartSMSChannel.php
|
+---Commands
| \---Log
| ClearCommand.php
| DisplayCommand.php
|
+---config
| api-x.php
|
+---Controllers
| LogController.php
|
+---Exceptions
| CouldNotSendNotification.php
| InvalidConfiguration.php
|
+---Facades
| APIxFacade.php
|
\---resources
\---views
log.blade.php
```


Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"extra": {
"branch-alias": {
"1.1": "1.1-dev"
"1.2": "1.2-dev"
},
"laravel": {
"providers": [
Expand Down
39 changes: 24 additions & 15 deletions src/APIx.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,7 @@ public static function send($message = null)
dd($e->getMessage());
}

self::$response = (string)$response->getBody();
return self::$response;
return $response;
}

/**
Expand Down Expand Up @@ -250,25 +249,35 @@ private static function responseMessage($code)
*/
public static function logMessages()
{
$logMessage = null;
$logEntryDelimiter = config('api-x.log_entry_delimiter');
$messagesToLog = [];

foreach (explode(',', self::$recipient) as $recipient) {
$senderName = self::$senderName;
$senderName = self::$senderName ?? config('api-x.sender_name');
$message = self::$message;
$timestamp = Carbon::now()->toDateTimeString();

$logMessage .= <<< EOF
From: $senderName
To: $recipient
Date: $timestamp
$message
$logEntryDelimiter
EOF;
array_push($messagesToLog, [
'from' => $senderName,
'to' => $recipient,
'message' => self::$message,
'timestamp' => Carbon::now()->toDateTimeString()
]);
}

Storage::prepend(self::$logFilePath, $logMessage);
if (Storage::exists(self::$logFilePath)) {
$messageLog = Storage::get(self::$logFilePath);
$logMessages = json_decode($messageLog, true);

if (is_array($logMessages)) {
foreach($messagesToLog as $messageToLog) {
array_push($logMessages, $messageToLog);
}
Storage::put(self::$logFilePath, json_encode($logMessages));
} else {
Storage::put(self::$logFilePath, json_encode($messagesToLog));
}
} else {
Storage::put(self::$logFilePath, json_encode($messagesToLog));
}
}
}
5 changes: 4 additions & 1 deletion src/Channels/SmartSMSChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class SmartSMSChannel
* @param mixed $notifiable
* @param \Illuminate\Notifications\Notification $notification
*
* @return bool
* @throws \AbdulmatinSanni\APIx\Exceptions\InvalidConfiguration
* @throws \AbdulmatinSanni\APIx\Exceptions\CouldNotSendNotification
*/
Expand All @@ -29,7 +30,7 @@ public function send($notifiable, Notification $notification)
$notification = $notification->toSmartSMS($notifiable);

if (! $recipient || ! $notification) {
return;
return false;
}

$response = APIx::to($recipient)
Expand All @@ -40,5 +41,7 @@ public function send($notifiable, Notification $notification)
if ($response->getStatusCode() !== 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($response);
}

return true;
}
}
42 changes: 19 additions & 23 deletions src/Commands/Log/DisplayCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class DisplayCommand extends Command
*
* @var string
*/
protected $signature = 'api-x:log
{--l|latest : Displays only the last log entry}
protected $signature = 'api-x:log
{--l|latest : Displays only the last log entry}
{--t|limit= : Specifies the number of logs to display}';

/**
Expand All @@ -41,41 +41,37 @@ public function __construct()
*/
public function handle()
{
$log = null;
$messageLog = null;
$logPath = config('api-x.log_file_path');
$logEntryDelimiter = config('api-x.log_entry_delimiter');

$headers = ['From', 'To', 'Message', 'Timestamp'];

try {
$log = Storage::get($logPath);
$messageLog = Storage::get($logPath);
} catch (FileNotFoundException $exception) {
Storage::put($logPath, null);
}

$messages = json_decode($messageLog, true);

if ($this->option('latest')) {
$logs = explode(config('api-x.log_entry_delimiter'), $log)[0];
$this->info($logs);
return;
$messages = [$messages[count($messages) - 1]];
}

$limit = $this->option('limit');

if ($limit) {
$delimitedlogs = null;
$logs = explode(config('api-x.log_entry_delimiter'), $log);
$logLimit = ($limit < count($logs)) ? $limit : count($logs);
if ($this->option('limit')) {
$messages = array_slice(
$messages,
count($messages) - $this->option('limit')
);
}

for ($i = 0; $i < $logLimit; $i++) {
$delimitedlogs .= <<< EOF
$logs[$i]
$logEntryDelimiter
EOF;
}
if (! is_array($messages)) {
$this->error("No message in log.");

$this->info($delimitedlogs);
return;
return false ;
}

$this->info($log);
$this->table($headers, $messages);
}
}
16 changes: 1 addition & 15 deletions src/config/api-x.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,6 @@

'log_file_path' => env('SMARTSMSSOLUTIONS_LOG_FILE_PATH', 'logs/api-x.log'),

/*
|--------------------------------------------------------------------------
| Log Delimeter
|--------------------------------------------------------------------------
|
| Delimeter to be used for each log entry.
|
*/

'log_entry_delimiter' => env(
'SMARTSMSSOLUTIONS_LOG_ENTRY_DELIMETER',
'--------------------------------------------------------------------'
),

/*
|--------------------------------------------------------------------------
| Response Codes
Expand All @@ -92,7 +78,7 @@
*/

'response_codes' => [
"1000" => "Successfull",
"1000" => "Successful",
"1001" => "Invalid Token",
"1002" => "Error Sending SMS",
"1003" => "Insufficient Balance",
Expand Down

0 comments on commit e75079f

Please sign in to comment.