Updated SenderInterface. Finished GnokiiSender
Testing is still needed.
This commit is contained in:
parent
7937536989
commit
883bff620b
@ -51,7 +51,7 @@ class FileSender implements SenderInterface
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function send($username, $recipient, $message, $passwordLocations)
|
||||
public function send($username, $recipient, $message, array $passwordLocations)
|
||||
{
|
||||
$rcptDir = $this->messageDir . '/' . $recipient;
|
||||
|
||||
|
@ -2,21 +2,70 @@
|
||||
namespace SmsGateway\Sender;
|
||||
|
||||
use SmsGateway\SenderInterface;
|
||||
use SmsGateway\LoggerInterface;
|
||||
|
||||
class GnokiiSender implements SenderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var SmsGateway\LoggerInterface $logger
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
private $gnokiiPath;
|
||||
|
||||
public function __construct($gnokiiPath)
|
||||
{
|
||||
$this->gnokiiPath = $gnokiiPath;
|
||||
|
||||
if (!is_executable($this->gnokiiPath)) {
|
||||
throw new \Exception('Specified Gnokii executable is not executable by me!');
|
||||
}
|
||||
|
||||
public function send($recipient, $message)
|
||||
$this->gnokiiPath = $gnokiiPath;
|
||||
}
|
||||
|
||||
public function send($username, $recipient, $message, array $passwordLocations)
|
||||
{
|
||||
if ($this->logger === null) {
|
||||
throw new \LogicException('Logger is not defined!');
|
||||
}
|
||||
|
||||
$descriptors = array(
|
||||
0 => array('pipe', 'r'),
|
||||
1 => array('pipe', 'w'),
|
||||
2 => array('pipe', 'w'),
|
||||
);
|
||||
$cmd = escapeshellcmd($this->gnokiiPath . ' --sendsms ' . escapeshellarg($recipient));
|
||||
$pipes = array();
|
||||
$process = proc_open($cmd, $descriptors, $pipes, null, array('LANG' => 'en_US.UTF-8'));
|
||||
if (is_resource($process)) {
|
||||
fwrite($pipes[0], $message);
|
||||
fclose($pipes[0]);
|
||||
|
||||
$stdout = stream_get_contents($pipes[1]);
|
||||
$stderr = stream_get_contents($pipes[2]);
|
||||
$returnValue = proc_close($process);
|
||||
|
||||
if ($returnValue != 0) {
|
||||
throw new \RuntimeException('Unable to send SMS: ' . $stderr . '; ' . $stdout);
|
||||
}
|
||||
|
||||
$this->logger->messageLog($username, $recipient, $message, $passwordLocations);
|
||||
} else {
|
||||
throw new \RuntimeException('Unable to start Gnokii.');
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function setLogger(LoggerInterface $logger) {
|
||||
if ($logger === null) {
|
||||
throw new \RuntimeException('A logger interface must be specified!');
|
||||
}
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
public function getLogger() {
|
||||
return $this->logger;
|
||||
}
|
||||
}
|
||||
|
@ -15,5 +15,5 @@ interface SenderInterface
|
||||
* @throws Exception Upon sending error. Gnokii output will be
|
||||
* stored in $e->message
|
||||
*/
|
||||
public function send($username, $recipient, $message, $passwordLocations);
|
||||
public function send($username, $recipient, $message, array $passwordLocations);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user