|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PamiModule\Service; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayObject; |
|
6
|
|
|
use PAMI\Client\Impl\ClientImpl; |
|
7
|
|
|
use PAMI\Message\OutgoingMessage; |
|
8
|
|
|
use PAMI\Message\Response\ResponseMessage; |
|
9
|
|
|
use Zend\EventManager\Event; |
|
10
|
|
|
use Zend\EventManager\EventManagerAwareTrait; |
|
11
|
|
|
use Zend\EventManager\EventsCapableInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Client. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Client implements EventsCapableInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use EventManagerAwareTrait; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* PAMI client. |
|
22
|
|
|
* |
|
23
|
|
|
* @var ClientImpl |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $connection; |
|
26
|
|
|
/** |
|
27
|
|
|
* Custom parameters. |
|
28
|
|
|
* |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $params = []; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $host; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Client constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $host |
|
41
|
|
|
* @param ClientImpl $pami PAMI client |
|
42
|
|
|
*/ |
|
43
|
10 |
|
public function __construct($host, ClientImpl $pami) |
|
44
|
|
|
{ |
|
45
|
10 |
|
$this->host = $host; |
|
46
|
10 |
|
$this->connection = $pami; |
|
47
|
10 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Return the hostname of the connection. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function getHost() |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->host; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Return the PAMI client. |
|
61
|
|
|
* |
|
62
|
|
|
* @return ClientImpl |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function getConnection() |
|
65
|
|
|
{ |
|
66
|
2 |
|
return $this->connection; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Return the custom parameters. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public function getParams() |
|
75
|
|
|
{ |
|
76
|
2 |
|
return $this->params; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the custom parameters. |
|
81
|
|
|
* |
|
82
|
|
|
* @param array $params Parameters |
|
83
|
|
|
* |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function setParams(array $params) |
|
87
|
|
|
{ |
|
88
|
2 |
|
$this->params = $params; |
|
89
|
|
|
|
|
90
|
2 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Connect to the Asterisk Manager Interface. |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \PAMI\Client\Exception\ClientException |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
2 |
|
public function connect() |
|
101
|
|
|
{ |
|
102
|
2 |
|
$results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this); |
|
103
|
2 |
|
if ($results->stopped()) { |
|
104
|
1 |
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
$this->connection->open(); |
|
108
|
|
|
|
|
109
|
1 |
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this); |
|
110
|
|
|
|
|
111
|
1 |
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Disconnect from the Asterisk Manager Interface. |
|
116
|
|
|
* |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function disconnect() |
|
120
|
|
|
{ |
|
121
|
2 |
|
$results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this); |
|
122
|
2 |
|
if ($results->stopped()) { |
|
123
|
1 |
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
$this->connection->close(); |
|
127
|
|
|
|
|
128
|
1 |
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this); |
|
129
|
|
|
|
|
130
|
1 |
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Main processing loop. Also called from send(), you should call this in |
|
135
|
|
|
* your own application in order to continue reading events and responses |
|
136
|
|
|
* from ami. |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function process() |
|
141
|
|
|
{ |
|
142
|
2 |
|
$results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this); |
|
143
|
|
|
|
|
144
|
2 |
|
if ($results->stopped()) { |
|
145
|
1 |
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
$this->connection->process(); |
|
149
|
|
|
|
|
150
|
1 |
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this); |
|
151
|
|
|
|
|
152
|
1 |
|
return $this; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Sends a message to AMI. |
|
157
|
|
|
* |
|
158
|
|
|
* @param OutgoingMessage $action Action to send |
|
159
|
|
|
* |
|
160
|
|
|
* @throws \PAMI\Client\Exception\ClientException |
|
161
|
|
|
* |
|
162
|
|
|
* @return \PAMI\Message\Response\ResponseMessage |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public function sendAction(OutgoingMessage $action) |
|
165
|
|
|
{ |
|
166
|
2 |
|
$params = new ArrayObject(['action' => $action]); |
|
167
|
2 |
|
$event = new Event(__FUNCTION__ . '.pre', $this, $params); |
|
168
|
2 |
|
$results = $this->getEventManager()->triggerEventUntil( |
|
169
|
2 |
|
function ($response) { |
|
170
|
1 |
|
return $response instanceof ResponseMessage; |
|
171
|
2 |
|
}, |
|
172
|
|
|
$event |
|
173
|
2 |
|
); |
|
174
|
2 |
|
if ($results->stopped()) { |
|
175
|
1 |
|
return $results->last(); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
1 |
|
$response = $this->connection->send($action); |
|
179
|
|
|
|
|
180
|
1 |
|
$params['response'] = $response; |
|
181
|
1 |
|
$this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $params); |
|
182
|
|
|
|
|
183
|
1 |
|
return $response; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|