Skip to content

Commit

Permalink
Merge 7c23db6 into 3a15c47
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvargiu committed Nov 9, 2016
2 parents 3a15c47 + 7c23db6 commit 1795833
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
17 changes: 11 additions & 6 deletions src/PAMI/Client/Impl/ClientImpl.php
Expand Up @@ -34,9 +34,7 @@
use PAMI\Message\Message;
use PAMI\Message\IncomingMessage;
use PAMI\Message\Action\LoginAction;
use PAMI\Message\Action\LogoffAction;
use PAMI\Message\Response\ResponseMessage;
use PAMI\Message\Event\EventMessage;
use PAMI\Message\Event\Factory\Impl\EventFactoryImpl;
use PAMI\Listener\IEventListener;
use PAMI\Client\Exception\ClientException;
Expand All @@ -60,7 +58,7 @@ class ClientImpl implements IClient
{
/**
* PSR-3 logger.
* @var Logger
* @var LoggerInterface
*/
private $logger;

Expand Down Expand Up @@ -150,6 +148,12 @@ class ClientImpl implements IClient
*/
private $lastActionId;

/**
* Event mask to apply on login action.
* @var string|null
*/
private $eventMask;

/**
* Opens a tcp connection to ami.
*
Expand All @@ -173,7 +177,7 @@ public function open()
if ($this->socket === false) {
throw new ClientException('Error connecting to ami: ' . $errstr);
}
$msg = new LoginAction($this->user, $this->pass);
$msg = new LoginAction($this->user, $this->pass, $this->eventMask);
$asteriskId = @stream_get_line($this->socket, 1024, Message::EOL);
if (strstr($asteriskId, 'Asterisk') === false) {
throw new ClientException(
Expand Down Expand Up @@ -434,7 +438,7 @@ public function close()
/**
* Sets the logger implementation.
*
* @param Psr\Log\LoggerInterface $logger The PSR3-Logger
* @param LoggerInterface $logger The PSR3-Logger
*
* @return void
*/
Expand All @@ -453,12 +457,13 @@ public function __construct(array $options)
{
$this->logger = new NullLogger;
$this->host = $options['host'];
$this->port = intval($options['port']);
$this->port = (int) $options['port'];
$this->user = $options['username'];
$this->pass = $options['secret'];
$this->cTimeout = $options['connect_timeout'];
$this->rTimeout = $options['read_timeout'];
$this->scheme = isset($options['scheme']) ? $options['scheme'] : 'tcp://';
$this->eventMask = isset($options['event_mask']) ? $options['event_mask'] : null;
$this->eventListeners = array();
$this->eventFactory = new EventFactoryImpl();
$this->incomingQueue = array();
Expand Down
9 changes: 7 additions & 2 deletions src/PAMI/Message/Action/LoginAction.php
Expand Up @@ -46,15 +46,20 @@ class LoginAction extends ActionMessage
/**
* Constructor.
*
* @param string $user AMI username.
* @param string $user AMI username.
* @param string $password AMI password.
* @param string|null $eventMask
*
* @return void
*/
public function __construct($user, $password)
public function __construct($user, $password, $eventMask = null)
{
parent::__construct('Login');
$this->setKey('Username', $user);
$this->setKey('Secret', $password);

if (null !== $eventMask) {
$this->setKey('Events', $eventMask);
}
}
}
31 changes: 31 additions & 0 deletions test/actions/Test_Actions.php
Expand Up @@ -108,6 +108,37 @@ public function can_absolute_timeout()
$action = new \PAMI\Message\Action\AbsoluteTimeoutAction('SIP/asd', 10);
$client = $this->_start($write, $action);
}
/**
* @test
*/
public function can_login()
{
$write = array(implode("\r\n", array(
'action: Login',
'actionid: 1432.123',
'username: foo',
'secret: bar',
''
)));
$action = new \PAMI\Message\Action\LoginAction('foo', 'bar');
$client = $this->_start($write, $action);
}
/**
* @test
*/
public function can_login_with_events()
{
$write = array(implode("\r\n", array(
'action: Login',
'actionid: 1432.123',
'username: foo',
'secret: bar',
'events: all',
''
)));
$action = new \PAMI\Message\Action\LoginAction('foo', 'bar', 'all');
$client = $this->_start($write, $action);
}
/**
* @test
*/
Expand Down

0 comments on commit 1795833

Please sign in to comment.