Skip to content

Commit

Permalink
Merge pull request #1 from toriqahmads/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
toriqahmads authored Oct 3, 2020
2 parents 466707d + 429e6b4 commit dc0699d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 22 deletions.
10 changes: 10 additions & 0 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
require (__DIR__."/../vendor/autoload.php");

use Toriqahmads\SmsViro\SmsViro;

$smsviro = new SmsViro('707474e01fead92a7c9421a4069f21cd-12969e36-b3ef-46d8-8e93-f78804cee22d', 'kawan kewan');

$smsviro->sendSms('089668639048', 'Your otp code is 6989');

var_dump($smsviro->isRequestSuccess());
83 changes: 61 additions & 22 deletions src/SmsViro.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php
namespace Toriqahmads\SmsViro;
require (__DIR__."/../vendor/autoload.php");
use GuzzleHttp\Client;
use Toriqahmads\SmsViro\Exceptions\SmsViroException;

class SmsViro
{
private string $apikey;
private string $from;
private string $baseuri = "https://api.smsviro.com/restapi/sms";
private string $endpoint = "/1/text/advanced";
private string $baseuri = "https://api.smsviro.com/";
private string $endpoint = "restapi/sms/1/text/single";
private $response;
private $stream;
private $body;
private $statusCode;
private $phrase;

public function __construct(string $apikey, string $from)
{
Expand Down Expand Up @@ -85,19 +89,15 @@ private function buildHttpClient(): Client
*
* @param array $to
* @param string $text
* @return object
* @return array
*/
private function buildRequestBody(array $to, string $text): object
{
return json_decode([
"message" => [
"from" => $this->from,
"destination" => [
"to" => $to
],
"text" => $text
]
]);
private function buildRequestBody(array $to, string $text): array
{
return [
"from" => $this->from,
"to" => $to,
"text" => $text
];
}

/**
Expand All @@ -108,7 +108,7 @@ private function buildRequestBody(array $to, string $text): object
*/
private function formatting(string $to): string
{
if (!preg_match("/(^(62)|^(\+62))\d{9,11}/", $to) || !preg_match("/^(0)\d{9,12}/", $to))
if (!preg_match("/(^(62)|^(\+62))\d{9,11}/", $to) && !preg_match("/^(0)\d{9,12}/", $to))
{
throw new SmsViroException("Number must be start with 62 or +62 or 0");
}
Expand All @@ -129,7 +129,7 @@ private function formatting(string $to): string
*/
private function setTo($to): array
{
if (!is_array($to) || !is_string($to)) throw new SmsViroException("Parameter $to must be string or array");
if (!is_array($to) && !is_string($to)) throw new SmsViroException("Parameter $to must be string or array");

$destination = array();

Expand All @@ -156,19 +156,58 @@ private function setTo($to): array
*
* @param string|array $to
* @param string $text
* @return void
* @return $this
*/
public function sendSms($to, string $text)
{
$httpRequest = $this->buildHttpClient();
$requestBody = $this->buildRequestBody($this->setTo($to), $text);
$response = $httpRequest->request("POST", $this->endpoint, ["body" => json_encode($requestBody)]);
$this->setResponse($response);

$httpRequest->request("POST", $this->endpoint, ["body" => $requestBody]);

if ($httpRequest->getStatusCode() !== 200) {
if ($this->getStatusCode() !== 200) {
throw new SmsViroException("Error while sending sms");
}

return true;
return $this;
}

private function setResponse($response): void
{
$this->response = $response;
$this->stream = $this->response->getBody();
$this->body = $this->stream->getContents();
$this->statusCode = $this->response->getStatusCode();
$this->phrase = $this->response->getReasonPhrase();
}

public function getResponse()
{
return $this->response;
}

public function getStream()
{
return $this->stream;
}

public function getContents()
{
return $this->body;
}

public function getStatusCode()
{
return $this->statusCode;
}

public function getPhrase()
{
return $this->phrase;
}

public function isRequestSuccess()
{
return $this->getStatusCode() != 200 ? false : true;
}
}

0 comments on commit dc0699d

Please sign in to comment.