From e75079fd6bdab14e916da44fa58d70aa57b9378f Mon Sep 17 00:00:00 2001 From: Abdulmatin Sanni Date: Sun, 9 Dec 2018 01:46:13 +0100 Subject: [PATCH] Modified api-x:log command to output table --- README.md | 50 +++++++++++++++++------------ composer.json | 2 +- src/APIx.php | 39 +++++++++++++--------- src/Channels/SmartSMSChannel.php | 5 ++- src/Commands/Log/DisplayCommand.php | 42 +++++++++++------------- src/config/api-x.php | 16 +-------- 6 files changed, 78 insertions(+), 76 deletions(-) diff --git a/README.md b/README.md index ee1c807..8667619 100644 --- a/README.md +++ b/README.md @@ -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 ``` diff --git a/composer.json b/composer.json index b69fb76..46f16d3 100644 --- a/composer.json +++ b/composer.json @@ -45,7 +45,7 @@ }, "extra": { "branch-alias": { - "1.1": "1.1-dev" + "1.2": "1.2-dev" }, "laravel": { "providers": [ diff --git a/src/APIx.php b/src/APIx.php index 5cafc95..52476d8 100644 --- a/src/APIx.php +++ b/src/APIx.php @@ -168,8 +168,7 @@ public static function send($message = null) dd($e->getMessage()); } - self::$response = (string)$response->getBody(); - return self::$response; + return $response; } /** @@ -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)); + } } } diff --git a/src/Channels/SmartSMSChannel.php b/src/Channels/SmartSMSChannel.php index f8b7435..19fb062 100644 --- a/src/Channels/SmartSMSChannel.php +++ b/src/Channels/SmartSMSChannel.php @@ -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 */ @@ -29,7 +30,7 @@ public function send($notifiable, Notification $notification) $notification = $notification->toSmartSMS($notifiable); if (! $recipient || ! $notification) { - return; + return false; } $response = APIx::to($recipient) @@ -40,5 +41,7 @@ public function send($notifiable, Notification $notification) if ($response->getStatusCode() !== 200) { throw CouldNotSendNotification::serviceRespondedWithAnError($response); } + + return true; } } diff --git a/src/Commands/Log/DisplayCommand.php b/src/Commands/Log/DisplayCommand.php index d2ea7d6..c1b4c01 100644 --- a/src/Commands/Log/DisplayCommand.php +++ b/src/Commands/Log/DisplayCommand.php @@ -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}'; /** @@ -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); } } diff --git a/src/config/api-x.php b/src/config/api-x.php index 7d52e2a..db3c00f 100644 --- a/src/config/api-x.php +++ b/src/config/api-x.php @@ -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 @@ -92,7 +78,7 @@ */ 'response_codes' => [ - "1000" => "Successfull", + "1000" => "Successful", "1001" => "Invalid Token", "1002" => "Error Sending SMS", "1003" => "Insufficient Balance",