From 4960d9e0c98959358658081249fe935e2147a44f Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Fri, 10 Apr 2020 12:44:20 +0430 Subject: [PATCH 1/2] refactor conditionals, remove use Kavenegar\Enums\ApiLogs and use Kavenegar\Enums\General and because never used, remove close tag and use short array syntax --- src/KavenegarApi.php | 283 +++++++++++++++++++------------------------ 1 file changed, 126 insertions(+), 157 deletions(-) diff --git a/src/KavenegarApi.php b/src/KavenegarApi.php index ff0e946..1a11f9a 100644 --- a/src/KavenegarApi.php +++ b/src/KavenegarApi.php @@ -5,15 +5,13 @@ use Kavenegar\Exceptions\ApiException; use Kavenegar\Exceptions\HttpException; use Kavenegar\Exceptions\RuntimeException; -use Kavenegar\Enums\ApiLogs ; -use Kavenegar\Enums\General; class KavenegarApi { const APIPATH = "%s://api.kavenegar.com/v1/%s/%s/%s.json/"; const VERSION = "1.2.2"; - public function __construct($apiKey,$insecure=false) - { + + public function __construct($apiKey, $insecure = false) { if (!extension_loaded('curl')) { die('cURL library is not loaded'); exit; @@ -24,24 +22,20 @@ public function __construct($apiKey,$insecure=false) } $this->apiKey = trim($apiKey); $this->insecure = $insecure; - } - - protected function get_path($method, $base = 'sms') - { - return sprintf(self::APIPATH,$this->insecure==true ? "http": "https", $this->apiKey, $base, $method); } - - protected function execute($url, $data = null) - { - $headers = array( + + protected function get_path($method, $base = 'sms') { + return sprintf(self::APIPATH, $this->insecure == true ? "http" : "https", $this->apiKey, $base, $method); + } + + protected function execute($url, $data = null) { + $headers = [ 'Accept: application/json', 'Content-Type: application/x-www-form-urlencoded', 'charset: utf-8' - ); - $fields_string = ""; - if (!is_null($data)) { - $fields_string = http_build_query($data); - } + ]; + $fields_string = !is_null($data) ? http_build_query($data) : ""; + $handle = curl_init(); curl_setopt($handle, CURLOPT_URL, $url); curl_setopt($handle, CURLOPT_HTTPHEADER, $headers); @@ -50,59 +44,49 @@ protected function execute($url, $data = null) curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_POSTFIELDS, $fields_string); - - $response = curl_exec($handle); - $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); - $content_type = curl_getinfo($handle, CURLINFO_CONTENT_TYPE); - $curl_errno = curl_errno($handle); - $curl_error = curl_error($handle); + + $response = curl_exec($handle); + $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); + $curl_errno = curl_errno($handle); + $curl_error = curl_error($handle); if ($curl_errno) { throw new HttpException($curl_error, $curl_errno); } $json_response = json_decode($response); if ($code != 200 && is_null($json_response)) { throw new HttpException("Request have errors", $code); - } else { - $json_return = $json_response->return; - if ($json_return->status != 200) { - throw new ApiException($json_return->message, $json_return->status); - } - return $json_response->entries; - } - - } - - public function Send($sender, $receptor, $message, $date = null, $type = null, $localid = null) - { - if (is_array($receptor)) { - $receptor = implode(",", $receptor); } - if (is_array($localid)) { - $localid = implode(",", $localid); + $json_return = $json_response->return; + if ($json_return->status != 200) { + throw new ApiException($json_return->message, $json_return->status); } - $path = $this->get_path("send"); - $params = array( + return $json_response->entries; + + } + + public function Send($sender, $receptor, $message, $date = null, $type = null, $localid = null) { + + $receptor = is_array($receptor) ? implode(",", $receptor) : $receptor; + $localid = is_array($localid) ? implode(",", $localid) : $localid; + + $path = $this->get_path("send"); + $params = [ "receptor" => $receptor, "sender" => $sender, "message" => $message, "date" => $date, "type" => $type, "localid" => $localid - ); + ]; return $this->execute($path, $params); } - - public function SendArray($sender, $receptor, $message, $date = null, $type = null, $localmessageid = null) - { - if (!is_array($receptor)) { - $receptor = (array) $receptor; - } - if (!is_array($sender)) { - $sender = (array) $sender; - } - if (!is_array($message)) { - $message = (array) $message; - } + + public function SendArray($sender, $receptor, $message, $date = null, $type = null, $localmessageid = null) { + + $receptor = is_array($receptor) ? (array)$receptor : $receptor; + $sender = is_array($sender) ? (array)$sender : $sender; + $message = is_array($message) ? (array)$message : $message; + $repeat = count($receptor); if (!is_null($type) && !is_array($type)) { $type = array_fill(0, $repeat, $type); @@ -110,122 +94,111 @@ public function SendArray($sender, $receptor, $message, $date = null, $type = nu if (!is_null($localmessageid) && !is_array($localmessageid)) { $localmessageid = array_fill(0, $repeat, $localmessageid); } - $path = $this->get_path("sendarray"); - $params = array( + $path = $this->get_path("sendarray"); + $params = [ "receptor" => json_encode($receptor), "sender" => json_encode($sender), "message" => json_encode($message), "date" => $date, "type" => json_encode($type), "localmessageid" => json_encode($localmessageid) - ); + ]; return $this->execute($path, $params); } - - public function Status($messageid) - { + + public function Status($messageid) { $path = $this->get_path("status"); - $params = array( + $params = [ "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid - ); - return $this->execute($path,$params); + ]; + return $this->execute($path, $params); } - - public function StatusLocalMessageId($localid) - { + + public function StatusLocalMessageId($localid) { $path = $this->get_path("statuslocalmessageid"); - $params = array( + $params = [ "localid" => is_array($localid) ? implode(",", $localid) : $localid - ); + ]; return $this->execute($path, $params); } - - public function Select($messageid) - { - $params = array( + + public function Select($messageid) { + $params = [ "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid - ); + ]; $path = $this->get_path("select"); - return $this->execute($path, $params); + return $this->execute($path, $params); } - - public function SelectOutbox($startdate, $enddate, $sender) - { - $path = $this->get_path("selectoutbox"); - $params = array( + + public function SelectOutbox($startdate, $enddate, $sender) { + $path = $this->get_path("selectoutbox"); + $params = [ "startdate" => $startdate, "enddate" => $enddate, "sender" => $sender - ); + ]; return $this->execute($path, $params); } - - public function LatestOutbox($pagesize, $sender) - { - $path = $this->get_path("latestoutbox"); - $params = array( + + public function LatestOutbox($pagesize, $sender) { + $path = $this->get_path("latestoutbox"); + $params = [ "pagesize" => $pagesize, "sender" => $sender - ); + ]; return $this->execute($path, $params); } - - public function CountOutbox($startdate, $enddate, $status = 0) - { - $path = $this->get_path("countoutbox"); - $params = array( + + public function CountOutbox($startdate, $enddate, $status = 0) { + $path = $this->get_path("countoutbox"); + $params = [ "startdate" => $startdate, "enddate" => $enddate, "status" => $status - ); + ]; return $this->execute($path, $params); } - - public function Cancel($messageid) - { + + public function Cancel($messageid) { $path = $this->get_path("cancel"); - $params = array( + $params = [ "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid - ); - return $this->execute($path,$params); - + ]; + return $this->execute($path, $params); + } - - public function Receive($linenumber, $isread = 0) - { - $path = $this->get_path("receive"); - $params = array( + + public function Receive($linenumber, $isread = 0) { + $path = $this->get_path("receive"); + $params = [ "linenumber" => $linenumber, "isread" => $isread - ); + ]; return $this->execute($path, $params); } - - public function CountInbox($startdate, $enddate, $linenumber, $isread = 0) - { - $path = $this->get_path("countinbox"); - $params = array( + + public function CountInbox($startdate, $enddate, $linenumber, $isread = 0) { + $path = $this->get_path("countinbox"); + $params = [ "startdate" => $startdate, "enddate" => $enddate, "linenumber" => $linenumber, "isread" => $isread - ); + ]; return $this->execute($path, $params); } - - public function CountPostalcode($postalcode) - { - $path = $this->get_path("countpostalcode"); - $params = array( + + public function CountPostalcode($postalcode) { + $path = $this->get_path("countpostalcode"); + $params = [ "postalcode" => $postalcode - ); + ]; return $this->execute($path, $params); } - - public function SendbyPostalcode($sender,$postalcode,$message, $mcistartindex, $mcicount, $mtnstartindex, $mtncount, $date) - { - $path = $this->get_path("sendbypostalcode"); - $params = array( + + public function SendbyPostalcode($sender, $postalcode, $message, $mcistartindex, $mcicount, $mtnstartindex, $mtncount, $date) { + $path = $this->get_path("sendbypostalcode"); + $params = [ "postalcode" => $postalcode, "sender" => $sender, "message" => $message, @@ -234,61 +207,57 @@ public function SendbyPostalcode($sender,$postalcode,$message, $mcistartindex, $ "mtnstartindex" => $mtnstartindex, "mtncount" => $mtncount, "date" => $date - ); + ]; return $this->execute($path, $params); } - - public function AccountInfo() - { + + public function AccountInfo() { $path = $this->get_path("info", "account"); return $this->execute($path); - } - - public function AccountConfig($apilogs, $dailyreport, $debug, $defaultsender, $mincreditalarm, $resendfailed) - { - $path = $this->get_path("config", "account"); - $params = array( + } + + public function AccountConfig($apilogs, $dailyreport, $debug, $defaultsender, $mincreditalarm, $resendfailed) { + $path = $this->get_path("config", "account"); + $params = [ "apilogs" => $apilogs, "dailyreport" => $dailyreport, "debug" => $debug, "defaultsender" => $defaultsender, "mincreditalarm" => $mincreditalarm, "resendfailed" => $resendfailed - ); + ]; return $this->execute($path, $params); } - public function VerifyLookup($receptor, $token, $token2, $token3, $template, $type = null) - { - $path = $this->get_path("lookup", "verify"); - $params = array( + public function VerifyLookup($receptor, $token, $token2, $token3, $template, $type = null) { + $path = $this->get_path("lookup", "verify"); + $params = [ "receptor" => $receptor, "token" => $token, "token2" => $token2, "token3" => $token3, "template" => $template, "type" => $type - ); - if(func_num_args()>5){ - $arg_list = func_get_args(); - if(isset($arg_list[6])) - $params["token10"]=$arg_list[6]; - if(isset($arg_list[7])) - $params["token20"]=$arg_list[7]; - } - return $this->execute($path, $params); - } - - public function CallMakeTTS($receptor, $message, $date = null, $localid = null) - { - $path = $this->get_path("maketts", "call"); - $params = array( + ]; + + $arg_list = func_get_args(); + if ($arg_list > 5) { + if (isset($arg_list[6])) + $params["token10"] = $arg_list[6]; + if (isset($arg_list[7])) + $params["token20"] = $arg_list[7]; + } + return $this->execute($path, $params); + } + + public function CallMakeTTS($receptor, $message, $date = null, $localid = null) { + $path = $this->get_path("maketts", "call"); + $params = [ "receptor" => $receptor, "message" => $message, "date" => $date, "localid" => $localid - ); - return $this->execute($path, $params); + ]; + return $this->execute($path, $params); } -} -?> \ No newline at end of file +} \ No newline at end of file From 23c478badc2e098eb042d87976764c28cdaeae63 Mon Sep 17 00:00:00 2001 From: Ali Yousefi Date: Fri, 10 Apr 2020 12:51:18 +0430 Subject: [PATCH 2/2] remove closing tags and space --- src/Enums/ApiLogs.php | 4 +--- src/Enums/General.php | 3 +-- src/Exceptions/ApiException.php | 4 +--- src/Exceptions/BaseRuntimeException.php | 17 ++++++++--------- src/Exceptions/HttpException.php | 4 +--- src/KavenegarApi.php | 1 - 6 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/Enums/ApiLogs.php b/src/Enums/ApiLogs.php index def4e4b..3b54019 100644 --- a/src/Enums/ApiLogs.php +++ b/src/Enums/ApiLogs.php @@ -6,6 +6,4 @@ abstract class ApiLogs extends General { const Justforfault = "justforfault"; const Enabled = "enabled"; const Disabled = "disabled"; -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/src/Enums/General.php b/src/Enums/General.php index 55f6b6d..cceccfa 100644 --- a/src/Enums/General.php +++ b/src/Enums/General.php @@ -5,5 +5,4 @@ abstract class General { const Enabled = "enabled"; const Disabled = "disabled"; -} -?> \ No newline at end of file +} \ No newline at end of file diff --git a/src/Exceptions/ApiException.php b/src/Exceptions/ApiException.php index d3843b0..afc9c33 100644 --- a/src/Exceptions/ApiException.php +++ b/src/Exceptions/ApiException.php @@ -8,6 +8,4 @@ public function getName() { return 'ApiException'; } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/src/Exceptions/BaseRuntimeException.php b/src/Exceptions/BaseRuntimeException.php index 6ddd714..5e21f7a 100644 --- a/src/Exceptions/BaseRuntimeException.php +++ b/src/Exceptions/BaseRuntimeException.php @@ -2,18 +2,17 @@ namespace Kavenegar\Exceptions; -class BaseRuntimeException extends \RuntimeException +class BaseRuntimeException extends \RuntimeException { - public function getName() - { + public function getName() { return 'BaseRuntimeException'; } - public function __construct($message, $code=0) { + + public function __construct($message, $code = 0) { parent::__construct($message, $code); } - public function errorMessage(){ - return "\r\n".$this->getName() . "[{$this->code}] : {$this->message}\r\n"; - } -} -?> \ No newline at end of file + public function errorMessage() { + return "\r\n" . $this->getName() . "[{$this->code}] : {$this->message}\r\n"; + } +} \ No newline at end of file diff --git a/src/Exceptions/HttpException.php b/src/Exceptions/HttpException.php index 37b862f..fca7bc1 100644 --- a/src/Exceptions/HttpException.php +++ b/src/Exceptions/HttpException.php @@ -8,6 +8,4 @@ public function getName() { return 'HttpException'; } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/src/KavenegarApi.php b/src/KavenegarApi.php index 1a11f9a..677a095 100644 --- a/src/KavenegarApi.php +++ b/src/KavenegarApi.php @@ -165,7 +165,6 @@ public function Cancel($messageid) { "messageid" => is_array($messageid) ? implode(",", $messageid) : $messageid ]; return $this->execute($path, $params); - } public function Receive($linenumber, $isread = 0) {