Completed
Push — master ( 087b38...e9a1e8 )
by Brian
03:09
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.4286
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 79
    public function __construct(LoopInterface $eventLoop, LoggerInterface $logger)
127
    {
128 79
        $this->eventLoop = $eventLoop;
129 79
        $this->logger = $logger;
130 79
    }
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 79
    public function connect($address)
138
    {
139 79
        $components = parse_url($address);
140 79
        $host = $components['host'];
141 79
        $port = $components['port'];
142 79
        $path = $components['path'];
143 79
        $query = $components['query'];
144 79
        $queryParts = [];
145 79
        parse_str($query, $queryParts);
146
147 79
        $this->stasisApplicationName = $queryParts['app'];
148 79
        $apiKey = $queryParts['api_key'];
149 79
        list($username, $password) = explode(':', $apiKey);
150
151 79
        $this->endpoint = new PestJSON('http://'.$host.':'.$port.dirname($path));
152 79
        $this->endpoint->setupAuth($username, $password, 'basic');
153
154 79
        $this->wsClient = new WebSocket($address, $this->eventLoop, $this->logger);
155
156 79
        $this->wsClient->on("message", function (WebSocketMessage $rawMessage) {
157 20
            $message = new Message($rawMessage->getData());
158
159 20
            $eventType = '\\phparia\\Events\\'.$message->getType();
160 20
            if (class_exists($eventType)) {
161 20
                $event = new $eventType($this, $rawMessage->getData());
162 20
            } else {
163
                $this->logger->warn("Event: '$eventType' not implemented");
164
                // @todo Create a generic event for any that are not implemented
165
166
                return;
167
            }
168
169
            // Emit the specific event (just to get it back to where it came from)
170 20
            if ($event instanceof IdentifiableEventInterface) {
171 20
                $this->logger->notice("Emitting ID event: {$event->getEventId()}");
172 20
                $this->wsClient->emit($event->getEventId(), array('event' => $event));
173 20
            }
174
175
            // Emit the general event
176 20
            $this->logger->notice("Emitting event: {$message->getType()}");
177 20
            $this->wsClient->emit($message->getType(), array('event' => $event));
178 79
        });
179 79
    }
180
181
    /**
182
     * Headers will be passed to the provided callback on request
183
     *
184
     * @param callable $callback
185
     */
186
    public function onRequest(callable $callback)
187
    {
188
        $this->wsClient->on("request", $callback);
189
    }
190
191
    /**
192
     * Handshake will be passed to the provide callback on handshake
193
     *
194
     * @param callable $callback
195
     */
196
    public function onHandshake(callable $callback)
197
    {
198
        $this->wsClient->on("handshake", $callback);
199
    }
200
201
    /**
202
     * @param callable|callable $callback
203
     */
204 20
    public function onConnect(callable $callback)
205
    {
206 20
        $this->wsClient->on("connect", $callback);
207 20
    }
208
209
    /**
210
     * @return WebSocket
211
     */
212 79
    public function getWsClient()
213
    {
214 79
        return $this->wsClient;
215
    }
216
217
    /**
218
     * @return LoopInterface
219
     */
220
    public function getEventLoop()
221
    {
222
        return $this->eventLoop;
223
    }
224
225
    /**
226
     * @return LoggerInterface
227
     */
228
    public function getLogger()
229
    {
230
        return $this->logger;
231
    }
232
233
    /**
234
     * @return string
235
     */
236 79
    public function getStasisApplicationName()
237
    {
238 79
        return $this->stasisApplicationName;
239
    }
240
241
    /**
242
     * @return PestJSON
243
     */
244 61
    public function getEndpoint()
245
    {
246 61
        return $this->endpoint;
247
    }
248
249
    /**
250
     * @return Applications
251
     */
252 12
    public function applications()
253
    {
254 12
        if (!$this->applications instanceof Applications) {
255 12
            $this->applications = new Applications($this);
256 12
        }
257
258 12
        return $this->applications;
259
    }
260
261
    /**
262
     * @return Asterisk
263
     */
264 7
    public function asterisk()
265
    {
266 7
        if (!$this->asterisk instanceof Asterisk) {
267 7
            $this->asterisk = new Asterisk($this);
268 7
        }
269
270 7
        return $this->asterisk;
271
    }
272
273
    /**
274
     * @return Bridges
275
     */
276 30
    public function bridges()
277
    {
278 30
        if (!$this->bridges instanceof Bridges) {
279 30
            $this->bridges = new Bridges($this);
280 30
        }
281
282 30
        return $this->bridges;
283
    }
284
285
    /**
286
     * @return Channels
287
     */
288 21
    public function channels()
289
    {
290 21
        if (!$this->channels instanceof Channels) {
291 21
            $this->channels = new Channels($this);
292 21
        }
293
294 21
        return $this->channels;
295
    }
296
297
    /**
298
     * @return DeviceStates
299
     */
300 2
    public function deviceStates()
301
    {
302 2
        if (!$this->deviceStates instanceof DeviceStates) {
303 2
            $this->deviceStates = new DeviceStates($this);
304 2
        }
305
306 2
        return $this->deviceStates;
307
    }
308
309
    /**
310
     * @return Endpoints
311
     */
312 2
    public function endPoints()
313
    {
314 2
        if (!$this->endPoints instanceof Endpoints) {
315 2
            $this->endPoints = new Endpoints($this);
316 2
        }
317
318 2
        return $this->endPoints;
319
    }
320
321
    /**
322
     * @return Events
323
     */
324 2
    public function events()
325
    {
326 2
        if (!$this->events instanceof Events) {
327 2
            $this->events = new Events($this);
328 2
        }
329
330 2
        return $this->events;
331
    }
332
333
    /**
334
     * @return Mailboxes
335
     */
336 1
    public function mailboxes()
337
    {
338 1
        if (!$this->mailboxes instanceof Mailboxes) {
339 1
            $this->mailboxes = new Mailboxes($this);
340 1
        }
341
342 1
        return $this->mailboxes;
343
    }
344
345
    /**
346
     * @return Playbacks
347
     */
348 5
    public function playbacks()
349
    {
350 5
        if (!$this->playbacks instanceof Playbacks) {
351 5
            $this->playbacks = new Playbacks($this);
352 5
        }
353
354 5
        return $this->playbacks;
355
    }
356
357
    /**
358
     * @return Recordings
359
     */
360 6
    public function recordings()
361
    {
362 6
        if (!$this->recordings instanceof Recordings) {
363 6
            $this->recordings = new Recordings($this);
364 6
        }
365
366 6
        return $this->recordings;
367
    }
368
369
    /**
370
     * @return Sounds
371
     */
372 3
    public function sounds()
373
    {
374 3
        if (!$this->sounds instanceof Sounds) {
375 3
            $this->sounds = new Sounds($this);
376 3
        }
377
378 3
        return $this->sounds;
379
    }
380
381
}
382