Skip to content

Commit

Permalink
Merge branch 'hotfix-1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmejio committed Jun 29, 2020
2 parents 3d2cfbd + fe457a5 commit 11369c8
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 69 deletions.
7 changes: 6 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ REMOTE_CONTROL_PASSWORD=
SLAVE_NAME=
# Form: server:port, e.g.: localhost:11211
# Leave blank to disable "locking" functionality
MEMCACHED_LOCK_STORE=
MEMCACHED_LOCK_STORE=
# Timeout for network operations
CONNECT_TIMEOUT=5000
TIMEOUT=60000
TEST_ADDRESSEE=22741
TEST_MESSAGE=R
3 changes: 0 additions & 3 deletions .github/scripts/decrypt_env_test.sh

This file was deleted.

9 changes: 0 additions & 9 deletions .github/secrets/.env.asc

This file was deleted.

39 changes: 0 additions & 39 deletions .github/workflows/php.yml

This file was deleted.

4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
{
"name": "April Sacil",
"email": "aprilvsacil@gmail.com"
},
{
"name": "Julián Mejio",
"email": "julianmejio@gmail.com"
}
],
"require": {
Expand Down
35 changes: 35 additions & 0 deletions examples/SmsGatewayByRemoteServer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

require_once(__DIR__ . '/bootstrap.php');

// Define the server
$server = new \GoIP\RemoteControl\Server(
$_ENV['REMOTE_CONTROL_ADDRESS'],
\GoIP\RemoteControl\Server::DEFAULT_WEB_PORT,
$_ENV['REMOTE_CONTROL_USERNAME'],
$_ENV['REMOTE_CONTROL_PASSWORD']
);

// Get the default slave
$slave = $server->findSlave($_ENV['SLAVE_NAME']);

// Create the SMS gateway
$slaveGateway = $slave->getSmsGateway(
$_ENV['GOIP_CLIENT_USERNAME'],
$_ENV['GOIP_CLIENT_PASSWORD']
)
// And set the timeout configuration
->setConnectTimeout(intval($_ENV['CONNECT_TIMEOUT']))
->setTimeout(intval($_ENV['TIMEOUT']))
;

// Make a ping request
$sms = $slaveGateway->sendSmsAndWaitResponse(
$_ENV['TEST_ADDRESSEE'],
$_ENV['TEST_MESSAGE'],
intval($_ENV['GOIP_CLIENT_DEFAULT_LINE']),
intval($_ENV['TIMEOUT'])
);

// And dump it
dump($sms);
71 changes: 59 additions & 12 deletions src/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,34 @@
*/
class Base
{
const DEFAULT_CONNECT_TIMEOUT = 5000;
const DEFAULT_TIMEOUT = 60000;

/**
* @var int
*/
private $connectTimeout;

/**
* @var int
*/
private $timeout;

public $goip;

/**
* Initialize the client connection
* given the host, port, username and password
*
* @param GoIp|null $goip
*/
public function __construct(GoipClient $goip)
{
public function __construct(
GoipClient $goip,
int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT,
int $timeout = self::DEFAULT_TIMEOUT
) {
$this->goip = $goip;
$this->connectTimeout = $connectTimeout;
$this->timeout = $timeout;
}

/**
Expand All @@ -48,23 +65,53 @@ public function connect($route, array $params = [], array $data = [])
$url = "http://" . $this->goip->host . '/default/en_US';
$url .= $route . '?' . http_build_query($params);
$user = $this->goip->username . ":" . $this->goip->password;
$curl = curl_init();
$curl = curl_init($url);

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERPWD, $user);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_PORT, $this->goip->port);
curl_setopt_array(
$curl,
[
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERPWD => $user,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_PORT => $this->goip->port,
CURLOPT_CONNECTTIMEOUT_MS => $this->connectTimeout,
CURLOPT_TIMEOUT_MS => $this->timeout,
]
);

if ($data) {
curl_setopt($curl, CURLOPT_POST, count($data));
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
]);
}

$results = curl_exec($curl);
curl_close($curl);

return $results;
}

public function getConnectTimeout(): int
{
return $this->connectTimeout;
}

public function setConnectTimeout(int $connectTimeout): Base
{
$this->connectTimeout = $connectTimeout;
return $this;
}

public function getTimeout(): int
{
return $this->timeout;
}

public function setTimeout(int $timeout): Base
{
$this->timeout = $timeout;
return $this;
}
}
29 changes: 24 additions & 5 deletions src/Sms/SmsGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class SmsGateway
* Time in seconds to check the response for a sent SMS.
*/
const CHECK_RESPONSE_INTERVAL = 1;
const DEFAULT_CONNECT_TIMEOUT = 5000;
const DEFAULT_TIMEOUT = 60000;

/**
* @var \GoIP\Sms
Expand All @@ -31,9 +33,14 @@ class SmsGateway
*/
private $lockStore;

public function __construct(\GoIP\Sms $sms)
{
public function __construct(
\GoIP\Sms $sms,
int $connectTimeout = self::DEFAULT_CONNECT_TIMEOUT,
int $timeout = self::DEFAULT_TIMEOUT
) {
$this->sms = $sms;
$this->sms->setConnectTimeout($connectTimeout);
$this->sms->setTimeout($timeout);
}

/**
Expand Down Expand Up @@ -81,7 +88,7 @@ public function getSms(int $line = 1): array
* @throws \Exception Thrown when the initial SMS is not sent or when first
* occurs the timeout before receiving the response.
*/
public function sendSmsAndWaitResponse(string $addressee, string $message, int $line = 1, int $responseTimeout = 60): Sms
public function sendSmsAndWaitResponse(string $addressee, string $message, int $line = 1, int $responseTimeout = self::DEFAULT_TIMEOUT): Sms
{
$lock = null;
if (null !== $this->lockStore) {
Expand Down Expand Up @@ -117,7 +124,7 @@ public function sendSmsAndWaitResponse(string $addressee, string $message, int $
$lock->release();
}
return $responseCheckMessage;
} while ($stopwatch->lap('check_response')->getDuration() > $responseTimeout);
} while ($stopwatch->lap('check_response')->getDuration() <= $responseTimeout);

if (null !== $lock) {
$lock->release();
Expand All @@ -128,7 +135,7 @@ public function sendSmsAndWaitResponse(string $addressee, string $message, int $
if (null !== $lock) {
$lock->release();
}
throw new MissingSmsResponseException('Could not retrieve the SMS response. ' . $th->getMessage());
throw new MissingSmsResponseException('Could not retrieve the SMS response. ' . $th->getMessage(), $th->getCode(), $th);
}
}

Expand All @@ -142,4 +149,16 @@ public function setLockStore(MemcachedStore $lockStore): SmsGateway
$this->lockStore = $lockStore;
return $this;
}

public function setConnectTimeout(int $connectTimeout): SmsGateway
{
$this->sms->setConnectTimeout($connectTimeout);
return $this;
}

public function setTimeout(int $timeout): SmsGateway
{
$this->sms->setTimeout($timeout);
return $this;
}
}

0 comments on commit 11369c8

Please sign in to comment.