1 | <?php |
||
43 | class AriClient |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * @var WebSocket |
||
48 | */ |
||
49 | protected $wsClient; |
||
50 | |||
51 | /** |
||
52 | * @var LoopInterface |
||
53 | */ |
||
54 | protected $eventLoop; |
||
55 | |||
56 | /** |
||
57 | * @var LoggerInterface |
||
58 | */ |
||
59 | protected $logger; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | protected $stasisApplicationName; |
||
65 | |||
66 | /** |
||
67 | * @var \PestJSON |
||
68 | */ |
||
69 | protected $endpoint; |
||
70 | |||
71 | /** |
||
72 | * @var Applications |
||
73 | */ |
||
74 | protected $applications; |
||
75 | |||
76 | /** |
||
77 | * @var Asterisk |
||
78 | */ |
||
79 | protected $asterisk; |
||
80 | |||
81 | /** |
||
82 | * @var Bridges |
||
83 | */ |
||
84 | protected $bridges; |
||
85 | |||
86 | /** |
||
87 | * @var Channels |
||
88 | */ |
||
89 | protected $channels; |
||
90 | |||
91 | /** |
||
92 | * @var DeviceStates |
||
93 | */ |
||
94 | protected $deviceStates; |
||
95 | |||
96 | /** |
||
97 | * @var Endpoints |
||
98 | */ |
||
99 | protected $endPoints; |
||
100 | |||
101 | /** |
||
102 | * @var Events |
||
103 | */ |
||
104 | protected $events; |
||
105 | |||
106 | /** |
||
107 | * @var Mailboxes |
||
108 | */ |
||
109 | protected $mailboxes; |
||
110 | |||
111 | /** |
||
112 | * @var Playbacks |
||
113 | */ |
||
114 | protected $playbacks; |
||
115 | |||
116 | /** |
||
117 | * @var Recordings |
||
118 | */ |
||
119 | protected $recordings; |
||
120 | |||
121 | /** |
||
122 | * @var Sounds |
||
123 | */ |
||
124 | protected $sounds; |
||
125 | |||
126 | 98 | public function __construct(LoopInterface $eventLoop, LoggerInterface $logger) |
|
127 | { |
||
128 | 98 | $this->eventLoop = $eventLoop; |
|
129 | 98 | $this->logger = $logger; |
|
130 | 98 | } |
|
131 | |||
132 | /** |
||
133 | * Connect to ARI. |
||
134 | * |
||
135 | * @param string $address Example ws://localhost:8088/ari/events?api_key=username:password&app=stasis_app_name |
||
136 | */ |
||
137 | 98 | public function connect($address) |
|
138 | { |
||
139 | 98 | $components = parse_url($address); |
|
140 | 98 | $host = $components['host']; |
|
141 | 98 | $port = $components['port']; |
|
142 | 98 | $path = $components['path']; |
|
143 | 98 | $query = $components['query']; |
|
144 | 98 | $queryParts = []; |
|
145 | 98 | parse_str($query, $queryParts); |
|
146 | |||
147 | 98 | $this->stasisApplicationName = $queryParts['app']; |
|
148 | 98 | $apiKey = $queryParts['api_key']; |
|
149 | 98 | list($username, $password) = explode(':', $apiKey); |
|
150 | |||
151 | 98 | $this->endpoint = new PestJSON('http://'.$host.':'.$port.dirname($path)); |
|
152 | 98 | $this->endpoint->setupAuth($username, $password, 'basic'); |
|
153 | |||
154 | 98 | $this->wsClient = new WebSocket($address, $this->eventLoop, $this->logger); |
|
155 | |||
156 | 98 | $this->wsClient->on("message", function (WebSocketMessage $rawMessage) { |
|
157 | 37 | $message = new Message($rawMessage->getData()); |
|
158 | |||
159 | 37 | $eventType = '\\phparia\\Events\\'.$message->getType(); |
|
160 | 37 | if (class_exists($eventType)) { |
|
161 | 37 | $event = new $eventType($this, $rawMessage->getData()); |
|
162 | 37 | } else { |
|
163 | $this->logger->warn("Event: '$eventType' not implemented"); |
||
164 | |||
165 | // @todo Create a generic event for any that are not implemented |
||
166 | |||
167 | return; |
||
168 | } |
||
169 | |||
170 | // Emit the specific event (just to get it back to where it came from) |
||
171 | 37 | if ($event instanceof IdentifiableEventInterface) { |
|
172 | 37 | $this->logger->notice("Emitting ID event: {$event->getEventId()}"); |
|
173 | 37 | $this->wsClient->emit($event->getEventId(), array('event' => $event)); |
|
174 | 37 | } |
|
175 | |||
176 | // Emit the general event |
||
177 | 37 | $this->logger->notice("Emitting event: {$message->getType()}"); |
|
178 | 37 | $this->wsClient->emit($message->getType(), array('event' => $event)); |
|
179 | 98 | }); |
|
180 | 98 | } |
|
181 | |||
182 | /** |
||
183 | * Headers will be passed to the provided callback on request |
||
184 | * |
||
185 | * @param callable $callback |
||
186 | */ |
||
187 | 1 | public function onRequest(callable $callback) |
|
191 | |||
192 | /** |
||
193 | * Handshake will be passed to the provide callback on handshake |
||
194 | * |
||
195 | * @param callable $callback |
||
196 | */ |
||
197 | 1 | public function onHandshake(callable $callback) |
|
201 | |||
202 | /** |
||
203 | * @param callable|callable $callback |
||
204 | */ |
||
205 | 38 | public function onConnect(callable $callback) |
|
209 | |||
210 | /** |
||
211 | * @param callable|callable $callback |
||
212 | */ |
||
213 | 23 | public function onClose(callable $callback) |
|
217 | |||
218 | /** |
||
219 | * @return WebSocket |
||
220 | */ |
||
221 | 98 | public function getWsClient() |
|
225 | |||
226 | /** |
||
227 | * @return LoopInterface |
||
228 | */ |
||
229 | 1 | public function getEventLoop() |
|
233 | |||
234 | /** |
||
235 | * @return LoggerInterface |
||
236 | */ |
||
237 | 1 | public function getLogger() |
|
241 | |||
242 | /** |
||
243 | * @return string |
||
244 | */ |
||
245 | 98 | public function getStasisApplicationName() |
|
249 | |||
250 | /** |
||
251 | * @return PestJSON |
||
252 | */ |
||
253 | 78 | public function getEndpoint() |
|
257 | |||
258 | /** |
||
259 | * @return Applications |
||
260 | */ |
||
261 | 12 | public function applications() |
|
269 | |||
270 | /** |
||
271 | * @return Asterisk |
||
272 | */ |
||
273 | 7 | public function asterisk() |
|
281 | |||
282 | /** |
||
283 | * @return Bridges |
||
284 | */ |
||
285 | 29 | public function bridges() |
|
293 | |||
294 | /** |
||
295 | * @return Channels |
||
296 | */ |
||
297 | 39 | public function channels() |
|
305 | |||
306 | /** |
||
307 | * @return DeviceStates |
||
308 | */ |
||
309 | 7 | public function deviceStates() |
|
317 | |||
318 | /** |
||
319 | * @return Endpoints |
||
320 | */ |
||
321 | 2 | public function endPoints() |
|
329 | |||
330 | /** |
||
331 | * @return Events |
||
332 | */ |
||
333 | 2 | public function events() |
|
341 | |||
342 | /** |
||
343 | * @return Mailboxes |
||
344 | */ |
||
345 | 6 | public function mailboxes() |
|
353 | |||
354 | /** |
||
355 | * @return Playbacks |
||
356 | */ |
||
357 | 5 | public function playbacks() |
|
365 | |||
366 | /** |
||
367 | * @return Recordings |
||
368 | */ |
||
369 | 5 | public function recordings() |
|
377 | |||
378 | /** |
||
379 | * @return Sounds |
||
380 | */ |
||
381 | 3 | public function sounds() |
|
389 | |||
390 | } |
||
391 |