Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
julianmejio committed Jun 24, 2020
2 parents 0fff6bc + 2d336f2 commit 3d2cfbd
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 22 deletions.
5 changes: 4 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ REMOTE_CONTROL_ADDRESS=
REMOTE_CONTROL_PORT=
REMOTE_CONTROL_USERNAME=
REMOTE_CONTROL_PASSWORD=
SLAVE_NAME=
SLAVE_NAME=
# Form: server:port, e.g.: localhost:11211
# Leave blank to disable "locking" functionality
MEMCACHED_LOCK_STORE=
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ Add the repository manually and require the dependency in *composer.json*.
* The method `\GoIP\Sms\SmsGateway::sendSmsAndWaitResponse()` only can handle
one response per line simultaneously. If two o more SMS are sent through the
line at the same time, it will break the response functionality due race
conditions.
conditions. It is blocked using locks to allow only one send-and-response
call per line simultaneously, but it requires a working Memcached server.

## Next features

Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
"require-dev": {
"phpunit/phpunit": "^9.2",
"friendsofphp/php-cs-fixer": "^2.16",
"symfony/var-dumper": "^5.1",
"symfony/dotenv": "^5.1"
"symfony/var-dumper": "^3.4",
"symfony/dotenv": "^3.4",
"symfony/lock": "^3.4"
},
"scripts": {
"test": "./vendor/bin/phpunit"
Expand Down
7 changes: 7 additions & 0 deletions src/Exception/AntoherSmsResponseRunningException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace GoIP\Exception;

class AntoherSmsResponseRunningException extends \LogicException
{
}
7 changes: 7 additions & 0 deletions src/Exception/MissingSmsResponseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace GoIP\Exception;

class MissingSmsResponseException extends \RuntimeException
{
}
80 changes: 62 additions & 18 deletions src/Sms/SmsGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace GoIP\Sms;

use GoIP\Exception\MissingSmsResponseException;
use GoIP\Exception\SmsNotSentException;
use GoIP\Exception\SmsResponseTimeoutException;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\Store\MemcachedStore;
use Symfony\Component\Stopwatch\Stopwatch;

/**
Expand All @@ -17,11 +20,17 @@ class SmsGateway
* Time in seconds to check the response for a sent SMS.
*/
const CHECK_RESPONSE_INTERVAL = 1;

/**
* @var \GoIP\Sms
*/
private $sms;

/**
* @var MemcachedStore
*/
private $lockStore;

public function __construct(\GoIP\Sms $sms)
{
$this->sms = $sms;
Expand Down Expand Up @@ -74,28 +83,63 @@ public function getSms(int $line = 1): array
*/
public function sendSmsAndWaitResponse(string $addressee, string $message, int $line = 1, int $responseTimeout = 60): Sms
{
$sendResult = $this->sendSms($addressee, $message, $line);
$lock = null;
if (null !== $this->lockStore) {
$lockFactory = new Factory($this->lockStore);
$lock = $lockFactory->createLock('goip-sms-send-receive-' . $line, $responseTimeout);

if (!$sendResult) {
throw new SmsNotSentException($line, $addressee);
if (!$lock->acquire()) {
throw new \Exception('There is another wait-for-response SMS in progress');
}
}

$mapper = new SmsMapper(new SmsAdapter($this->sms));
/** @var Sms $latestMessage */
$latestMessage = ($mapper->findByLine($line))[0];
$stopwatch = new Stopwatch();
$stopwatch->start('check_response');
do {
sleep(self::CHECK_RESPONSE_INTERVAL);
/** @var Sms $responseCheckMessage */
$responseCheckMessage = ($mapper->findByLine($line))[0];
if ($responseCheckMessage->getDate()->format('Y-m-d H:i:s') === $latestMessage->getDate()->format('Y-m-d H:i:s')) {
continue;
try {
$sendResult = $this->sendSms($addressee, $message, $line);

if (!$sendResult) {
throw new SmsNotSentException($line, $addressee);
}

$mapper = new SmsMapper(new SmsAdapter($this->sms));
/** @var Sms $latestMessage */
$latestMessage = ($mapper->findByLine($line))[0];
$stopwatch = new Stopwatch();
$stopwatch->start('check_response');
do {
sleep(self::CHECK_RESPONSE_INTERVAL);
/** @var Sms $responseCheckMessage */
$responseCheckMessage = ($mapper->findByLine($line))[0];
if ($responseCheckMessage->getDate()->format('Y-m-d H:i:s') === $latestMessage->getDate()->format('Y-m-d H:i:s')) {
continue;
}
$stopwatch->stop('check_response');
if (null !== $lock) {
$lock->release();
}
return $responseCheckMessage;
} while ($stopwatch->lap('check_response')->getDuration() > $responseTimeout);

if (null !== $lock) {
$lock->release();
}

throw new SmsResponseTimeoutException($line);
} catch (\Throwable $th) {
if (null !== $lock) {
$lock->release();
}
$stopwatch->stop('check_response');
return $responseCheckMessage;
} while ($stopwatch->lap('check_response')->getDuration() > $responseTimeout);
throw new MissingSmsResponseException('Could not retrieve the SMS response. ' . $th->getMessage());
}
}

throw new SmsResponseTimeoutException($line);
public function getLockStore(): MemcachedStore
{
return $this->lockStore;
}

public function setLockStore(MemcachedStore $lockStore): SmsGateway
{
$this->lockStore = $lockStore;
return $this;
}
}

0 comments on commit 3d2cfbd

Please sign in to comment.