Completed
Push — master ( 68d8d1...ccbd05 )
by Brian
02:42
created

AriClient::mailboxes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * Copyright 2015 Brian Smith <[email protected]>.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *      http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace phparia\Client;
20
21
use Devristo\Phpws\Client\WebSocket;
22
use Devristo\Phpws\Messaging\WebSocketMessage;
23
use phparia\Api\Applications;
24
use phparia\Api\Asterisk;
25
use phparia\Api\Bridges;
26
use phparia\Api\Channels;
27
use phparia\Api\DeviceStates;
28
use phparia\Api\Endpoints;
29
use phparia\Api\Events;
30
use phparia\Api\Mailboxes;
31
use phparia\Api\Playbacks;
32
use phparia\Api\Recordings;
33
use phparia\Api\Sounds;
34
use phparia\Events\IdentifiableEventInterface;
35
use phparia\Events\Message;
36
use React\EventLoop\LoopInterface;
37
use Zend\Log\LoggerInterface;
38
use PestJSON;
39
40
/**
41
 * @author Brian Smith <[email protected]>
42
 */
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)
188
    {
189 1
        $this->wsClient->on("request", $callback);
190 1
    }
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)
198
    {
199 1
        $this->wsClient->on("handshake", $callback);
200 1
    }
201
202
    /**
203
     * @param callable|callable $callback
204
     */
205 38
    public function onConnect(callable $callback)
206
    {
207 38
        $this->wsClient->on("connect", $callback);
208 38
    }
209
210
    /**
211
     * @param callable|callable $callback
212
     */
213 23
    public function onClose(callable $callback)
214
    {
215 23
        $this->wsClient->on("close", $callback);
216 23
    }
217
218
    /**
219
     * @return WebSocket
220
     */
221 98
    public function getWsClient()
222
    {
223 98
        return $this->wsClient;
224
    }
225
226
    /**
227
     * @return LoopInterface
228
     */
229 1
    public function getEventLoop()
230
    {
231 1
        return $this->eventLoop;
232
    }
233
234
    /**
235
     * @return LoggerInterface
236
     */
237 1
    public function getLogger()
238
    {
239 1
        return $this->logger;
240
    }
241
242
    /**
243
     * @return string
244
     */
245 98
    public function getStasisApplicationName()
246
    {
247 98
        return $this->stasisApplicationName;
248
    }
249
250
    /**
251
     * @return PestJSON
252
     */
253 78
    public function getEndpoint()
254
    {
255 78
        return $this->endpoint;
256
    }
257
258
    /**
259
     * @return Applications
260
     */
261 12
    public function applications()
262
    {
263 12
        if (!$this->applications instanceof Applications) {
264 12
            $this->applications = new Applications($this);
265 12
        }
266
267 12
        return $this->applications;
268
    }
269
270
    /**
271
     * @return Asterisk
272
     */
273 7
    public function asterisk()
274
    {
275 7
        if (!$this->asterisk instanceof Asterisk) {
276 7
            $this->asterisk = new Asterisk($this);
277 7
        }
278
279 7
        return $this->asterisk;
280
    }
281
282
    /**
283
     * @return Bridges
284
     */
285 29
    public function bridges()
286
    {
287 29
        if (!$this->bridges instanceof Bridges) {
288 29
            $this->bridges = new Bridges($this);
289 29
        }
290
291 29
        return $this->bridges;
292
    }
293
294
    /**
295
     * @return Channels
296
     */
297 39
    public function channels()
298
    {
299 39
        if (!$this->channels instanceof Channels) {
300 39
            $this->channels = new Channels($this);
301 39
        }
302
303 39
        return $this->channels;
304
    }
305
306
    /**
307
     * @return DeviceStates
308
     */
309 7
    public function deviceStates()
310
    {
311 7
        if (!$this->deviceStates instanceof DeviceStates) {
312 7
            $this->deviceStates = new DeviceStates($this);
313 7
        }
314
315 7
        return $this->deviceStates;
316
    }
317
318
    /**
319
     * @return Endpoints
320
     */
321 2
    public function endPoints()
322
    {
323 2
        if (!$this->endPoints instanceof Endpoints) {
324 2
            $this->endPoints = new Endpoints($this);
325 2
        }
326
327 2
        return $this->endPoints;
328
    }
329
330
    /**
331
     * @return Events
332
     */
333 2
    public function events()
334
    {
335 2
        if (!$this->events instanceof Events) {
336 2
            $this->events = new Events($this);
337 2
        }
338
339 2
        return $this->events;
340
    }
341
342
    /**
343
     * @return Mailboxes
344
     */
345 6
    public function mailboxes()
346
    {
347 6
        if (!$this->mailboxes instanceof Mailboxes) {
348 6
            $this->mailboxes = new Mailboxes($this);
349 6
        }
350
351 6
        return $this->mailboxes;
352
    }
353
354
    /**
355
     * @return Playbacks
356
     */
357 5
    public function playbacks()
358
    {
359 5
        if (!$this->playbacks instanceof Playbacks) {
360 5
            $this->playbacks = new Playbacks($this);
361 5
        }
362
363 5
        return $this->playbacks;
364
    }
365
366
    /**
367
     * @return Recordings
368
     */
369 5
    public function recordings()
370
    {
371 5
        if (!$this->recordings instanceof Recordings) {
372 5
            $this->recordings = new Recordings($this);
373 5
        }
374
375 5
        return $this->recordings;
376
    }
377
378
    /**
379
     * @return Sounds
380
     */
381 3
    public function sounds()
382
    {
383 3
        if (!$this->sounds instanceof Sounds) {
384 3
            $this->sounds = new Sounds($this);
385 3
        }
386
387 3
        return $this->sounds;
388
    }
389
390
}
391