Skip to content

Commit 4bbc95b

Browse files
authored
Merge pull request #117 from thomasvargiu/feature/events-on-login
EventMask parameter on login action
2 parents 3a15c47 + 7c23db6 commit 4bbc95b

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

src/PAMI/Client/Impl/ClientImpl.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@
3434
use PAMI\Message\Message;
3535
use PAMI\Message\IncomingMessage;
3636
use PAMI\Message\Action\LoginAction;
37-
use PAMI\Message\Action\LogoffAction;
3837
use PAMI\Message\Response\ResponseMessage;
39-
use PAMI\Message\Event\EventMessage;
4038
use PAMI\Message\Event\Factory\Impl\EventFactoryImpl;
4139
use PAMI\Listener\IEventListener;
4240
use PAMI\Client\Exception\ClientException;
@@ -60,7 +58,7 @@ class ClientImpl implements IClient
6058
{
6159
/**
6260
* PSR-3 logger.
63-
* @var Logger
61+
* @var LoggerInterface
6462
*/
6563
private $logger;
6664

@@ -150,6 +148,12 @@ class ClientImpl implements IClient
150148
*/
151149
private $lastActionId;
152150

151+
/**
152+
* Event mask to apply on login action.
153+
* @var string|null
154+
*/
155+
private $eventMask;
156+
153157
/**
154158
* Opens a tcp connection to ami.
155159
*
@@ -173,7 +177,7 @@ public function open()
173177
if ($this->socket === false) {
174178
throw new ClientException('Error connecting to ami: ' . $errstr);
175179
}
176-
$msg = new LoginAction($this->user, $this->pass);
180+
$msg = new LoginAction($this->user, $this->pass, $this->eventMask);
177181
$asteriskId = @stream_get_line($this->socket, 1024, Message::EOL);
178182
if (strstr($asteriskId, 'Asterisk') === false) {
179183
throw new ClientException(
@@ -434,7 +438,7 @@ public function close()
434438
/**
435439
* Sets the logger implementation.
436440
*
437-
* @param Psr\Log\LoggerInterface $logger The PSR3-Logger
441+
* @param LoggerInterface $logger The PSR3-Logger
438442
*
439443
* @return void
440444
*/
@@ -453,12 +457,13 @@ public function __construct(array $options)
453457
{
454458
$this->logger = new NullLogger;
455459
$this->host = $options['host'];
456-
$this->port = intval($options['port']);
460+
$this->port = (int) $options['port'];
457461
$this->user = $options['username'];
458462
$this->pass = $options['secret'];
459463
$this->cTimeout = $options['connect_timeout'];
460464
$this->rTimeout = $options['read_timeout'];
461465
$this->scheme = isset($options['scheme']) ? $options['scheme'] : 'tcp://';
466+
$this->eventMask = isset($options['event_mask']) ? $options['event_mask'] : null;
462467
$this->eventListeners = array();
463468
$this->eventFactory = new EventFactoryImpl();
464469
$this->incomingQueue = array();

src/PAMI/Message/Action/LoginAction.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,20 @@ class LoginAction extends ActionMessage
4646
/**
4747
* Constructor.
4848
*
49-
* @param string $user AMI username.
49+
* @param string $user AMI username.
5050
* @param string $password AMI password.
51+
* @param string|null $eventMask
5152
*
5253
* @return void
5354
*/
54-
public function __construct($user, $password)
55+
public function __construct($user, $password, $eventMask = null)
5556
{
5657
parent::__construct('Login');
5758
$this->setKey('Username', $user);
5859
$this->setKey('Secret', $password);
60+
61+
if (null !== $eventMask) {
62+
$this->setKey('Events', $eventMask);
63+
}
5964
}
6065
}

test/actions/Test_Actions.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,37 @@ public function can_absolute_timeout()
108108
$action = new \PAMI\Message\Action\AbsoluteTimeoutAction('SIP/asd', 10);
109109
$client = $this->_start($write, $action);
110110
}
111+
/**
112+
* @test
113+
*/
114+
public function can_login()
115+
{
116+
$write = array(implode("\r\n", array(
117+
'action: Login',
118+
'actionid: 1432.123',
119+
'username: foo',
120+
'secret: bar',
121+
''
122+
)));
123+
$action = new \PAMI\Message\Action\LoginAction('foo', 'bar');
124+
$client = $this->_start($write, $action);
125+
}
126+
/**
127+
* @test
128+
*/
129+
public function can_login_with_events()
130+
{
131+
$write = array(implode("\r\n", array(
132+
'action: Login',
133+
'actionid: 1432.123',
134+
'username: foo',
135+
'secret: bar',
136+
'events: all',
137+
''
138+
)));
139+
$action = new \PAMI\Message\Action\LoginAction('foo', 'bar', 'all');
140+
$client = $this->_start($write, $action);
141+
}
111142
/**
112143
* @test
113144
*/

0 commit comments

Comments
 (0)