Skip to content

Commit

Permalink
Merge branch 'papac-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
angebagui committed Jun 26, 2020
2 parents 9b06e9a + 148038f commit fd313ce
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 99 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The second one is used for MTN SMS.
Be sure to check the namespace first.

```php
use MtnSmsCloud/MTNSMSApi;
use MtnSmsCloud\MTNSMSApi;

/**
* Create a new Instance
Expand Down
148 changes: 84 additions & 64 deletions src/MtnSmsCloud/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,121 +2,141 @@

namespace MtnSmsCloud;

use Exception;
use MtnSmsCloud\Exception\MtnSmsCloudConnexionException;

/**
* This class is for for performing Api call.
* Actually, only POST and GET methods are embedded for an Http request.
*
*
* @license MIT
* @author Franck BROU <[email protected]>
*/
class BaseApi {

// Base API's url
public $base_url = "";

class BaseApi
{
/**
* Inits the class
* Define the base url
*
* @param string $base_url
* @var string
*/
public function __construct($base_url){
$this->base_url = $base_url;
}
private $base_url = "";

/**
* Send an error message
* Inits the class
*
* @param int $code
* @param boolean $success
* @param string $message
*
* @param string $base_url
*/
public function sendError($code, $success, $message){
return json_encode(array('code' => $code, 'success' => $success, 'message' => $message));
public function __construct($base_url)
{
$this->base_url = $base_url;
}

/**
* Call GET request
*
* @param string $endpoint
* @param string $options
* @param array $options
* @return string
*/
public function get($endpoint, $options = null) {
return $this->apiCall("get", $endpoint, $options);
public function get($endpoint, array $options = null)
{
return $this->call("GET", $endpoint, $options);
}

/**
* Call POST request
*
* @param string $endpoint
* @param string $options
* @param array $options
*/
public function post($endpoint, $options = null) {
return $this->apiCall("post", $endpoint, $options);
public function post($endpoint, array $options = [])
{
return $this->call("POST", $endpoint, $options);
}

/**
* Create API query and execute a GET/POST request
* @param string $httpMethod GET/POST
*
* @param string $http_method GET/POST
* @param string $endpoint
* @param string $options
* @param array $options
*/
public function apiCall($httpMethod, $endpoint, $options) {

try{
public function call($http_method, $endpoint, array $options = [])
{
try {
$url = $this->base_url."/".$endpoint;

// Initialize curl session
$curl = curl_init("$url");

// Disable SSL verification
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

// Set timeout
curl_setopt($curl, CURLOPT_TIMEOUT, 80);

// Returns the data/output as a string instead of raw data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

// Check if request as options
if (!is_null($options) && is_array($options)) {
// Scafolding request's headers
if (array_key_exists("headers", $options)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $options["headers"]);
}
// Scafolding a POST request
if ($httpMethod === "post") {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
if (array_key_exists("params", $options)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($options["params"]));
}
}
// Scafolding a GET request
elseif ($httpMethod === "get") {
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 80);
if (array_key_exists("params", $options)) {
$data = http_build_query($options["params"]);
$getUrl = $url."?".$data;
echo $getUrl;
curl_setopt($curl, CURLOPT_URL, $getUrl);
}else {
curl_setopt($curl, CURLOPT_URL, $url);
}
}
if (!is_null($options) && is_array($options)) {
$curl = $this->buildRequestScafolding($curl, $http_method, $options);
}

// Performing request
$response = curl_exec($curl);

// Closing the session
curl_close($curl);

// Rendering the response
if ($response === false) {
// An error occurs when processing
echo("Oups, Cannot connect to remote server.");
} else {
return $response;
}
throw new MtnSmsCloudConnexionException("Oups, Cannot connect to remote server.");
}

}
catch (Exception $exception){
return $exception->getMessage();
return $response;
} catch (\Exception $exception) {
if ($exception instanceof MtnSmsCloudConnexionException) {
throw $exception;
}

throw new MtnSmsCloudException($exception->getMessage(), 500);
};
}

/**
* Build the curl request scafolding
*
* @param resource $curl
* @param string $http_method
* @param array $options
*/
public function buildRequestScafolding($curl, $http_method, array $options)
{
if (array_key_exists("headers", $options)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $options["headers"]);
}

switch ($http_method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
if (array_key_exists("params", $options)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($options["params"]));
}
break;

case "GET":
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
if (array_key_exists("params", $options)) {
$getUrl = $url."?".http_build_query($options["params"]);
curl_setopt($curl, CURLOPT_URL, $getUrl);
} else {
curl_setopt($curl, CURLOPT_URL, $url);
}
break;
}

return $curl;
}
}
}
8 changes: 8 additions & 0 deletions src/MtnSmsCloud/Exception/MtnSmsCloudConnexionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MtnSmsCloud\Exception;

class MtnSmsCloudConnexionException extends \Exception
{

}
8 changes: 8 additions & 0 deletions src/MtnSmsCloud/Exception/MtnSmsCloudException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace MtnSmsCloud\Exception;

class MtnSmsCloudException extends \Exception
{

}
71 changes: 37 additions & 34 deletions src/MtnSmsCloud/MTNSMSApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace MtnSmsCloud;

use MtnSmsCloud\Exception\MtnSmsCloudException;

/**
* This class is for for performing Api call on MTN SMS CLOUD server.
* You can use it for:
Expand Down Expand Up @@ -123,22 +125,17 @@ public function toArray()
* @param array $body
* @return mixed
*/
public function newCampaign($recipients, $content)
public function newCampaign(array $recipients, $content)
{
// Recipients testing
if (is_array($recipients) == false) {
$this->sendError(400, false, "The parameter `recipients` must be an array");
}

if (count($recipients) == 0) {
$this->sendError(400, false, "No phone number provided.");
throw new MtnSmsCloudException("No phone number provided.", 400);
}

// Scafolding the request's params
$params = [
"sender"=> $this->getSenderID(),
"recipients"=> $recipients,
"content"=> $content
"sender" => $this->getSenderID(),
"recipients" => $recipients,
"content" => $content
];

// Scafolding request's options
Expand All @@ -165,7 +162,7 @@ public function newCampaign($recipients, $content)
public function getCampaign($campaign_id)
{
if (is_null($campaign_id) || $campaign_id == "") {
return $this->sendError(400, false, "No campaign ID provided.");
return new MtnSmsCloudException("No campaign ID provided.", 400);
}

// Scafolding request's options
Expand All @@ -185,41 +182,47 @@ public function getCampaign($campaign_id)
/**
* Retrieves all messages associated to the provided authentification Bearer token
*
* All dispatchedAt_* format { (Datetime) 2020-02-14 14:14:00 => 20200214141400}
*
* @param string $status,
* @param string $campaign_id,
* @param string $dispatchedAt_before
* @param string $dispatchedAt_after
* @param string $updatedAt_before
* @param string $updatedAt_after
* @param int $page Page numbe
* @param int $length Nomber of messages per pages
* @param string $campaign_id
* @param string $status
* @param array $params
* @return mixed
*/
public function getMessages($status = null, $campaign_id, $dispatchedAt_before, $dispatchedAt_after, $updatedAt_before = null, $updatedAt_after = null, $page = 1, $length = 2)
public function getMessages($campaign_id, $status, $params = [])
{
if (is_null($campaign_id) || $campaign_id == "") {
return new MtnSmsCloudException("No campaign ID provided.", 400);
}

$default_params = [
'dispatchedAt_before' => null,
'dispatchedAt_after' => null,
'updatedAt_before' => null,
'updatedAt_after' => null,
'page' => 1,
'length' => 2
];

foreach ($default_params as $key => $value) {
if (!is_isset($params[$key])) {
$params[$key] = $value;
}
}

$params['campaingId'] = $campaign_id;
$params['status'] = $status;
$params['sender'] = $this->getSenderID();

$options = [
'headers'=> [
'Authorization: Bearer '.$this->getAuthHeader(),
'Accept: application/json',
'Content-Type: application/json',
'Cache-Control: no-cache'
],
'params' => [
'sender' => $this->getSenderID(),
'status' => $status,
'campaingId' => $campaign_id,
'dispatchedAt_before' => $dispatchedAt_before,
'dispatchedAt_after' => $dispatchedAt_after,
'updatedAt_before' => $updatedAt_before,
'updatedAt_after' => $updatedAt_after,
'page' => $page,
'length' => $length,
],
'params' => $params,
];

// Sending Get Request
$this->get('messages/outbox', $options);
return $this->get('messages/outbox', $options);
}
}

0 comments on commit fd313ce

Please sign in to comment.