Completed
Push — master ( a72aaa...aa1c76 )
by Brian
03:39
created

Phparia::onStasisEnd()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * Copyright 2015 Brian Smith <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace phparia\Client;
19
20
21
use Devristo\Phpws\Client\WebSocket;
22
use phparia\Api\Applications;
23
use phparia\Api\Asterisk;
24
use phparia\Api\Bridges;
25
use phparia\Api\Channels;
26
use phparia\Api\DeviceStates;
27
use phparia\Api\Endpoints;
28
use phparia\Api\Events;
29
use phparia\Api\Mailboxes;
30
use phparia\Api\Playbacks;
31
use phparia\Api\Recordings;
32
use phparia\Api\Sounds;
33
use phparia\Events\Event;
34
use React\EventLoop;
35
use Zend\Log\LoggerInterface;
36
37
class Phparia
38
{
39
    /**
40
     * @var WebSocket
41
     */
42
    protected $wsClient;
43
44
    /**
45
     * @var EventLoop\LoopInterface
46
     */
47
    protected $eventLoop;
48
49
    /**
50
     * @var LoggerInterface
51
     */
52
    protected $logger;
53
54
    /**
55
     * @var AriClient
56
     */
57
    protected $ariClient;
58
59
    /**
60
     * @var AmiClient
61
     */
62
    protected $amiClient;
63
64
    /**
65
     * @var string
66
     */
67
    protected $stasisApplicationName;
68
69
    /**
70
     * @param LoggerInterface $logger
71
     */
72 96
    public function __construct(LoggerInterface $logger)
73
    {
74 96
        $this->logger = $logger;
75 96
    }
76
77
    /**
78
     * Connect to ARI and optionally AMI
79
     *
80
     * @param string $ariAddress
81
     * @param string|null $amiAddress
82
     */
83 96
    public function connect($ariAddress, $amiAddress = null)
84
    {
85 96
        $this->eventLoop = EventLoop\Factory::create();
86 96
        $this->ariClient = new AriClient($this->eventLoop, $this->logger);
87 96
        $this->ariClient->connect($ariAddress);
88 96
        $this->wsClient = $this->ariClient->getWsClient();
89 96
        $this->stasisApplicationName = $this->ariClient->getStasisApplicationName();
90
91 96
        if ($amiAddress !== null) {
92 96
            $this->amiClient = new AmiClient($this->ariClient->getWsClient(), $this->eventLoop, $this->logger);
93 96
            $this->amiClient
94 96
                ->connect($amiAddress)
95 96
                ->done();
96 96
        }
97 96
    }
98
99
    /**
100
     * Connect and start the event loop
101
     */
102 36
    public function run()
103
    {
104 36
        $this->wsClient->open();
105 36
        $this->eventLoop->run();
106 22
    }
107
108
    /**
109
     * Disconnect and stop the event loop
110
     */
111 22
    public function stop()
112
    {
113 22
        $this->wsClient->close();
114 22
        $this->eventLoop->stop();
115 22
    }
116
117
    /**
118
     * @param callable|callable $callback
119
     */
120 36
    public function onStasisStart(callable $callback)
121
    {
122 36
        $this->wsClient->on(Event::STASIS_START, $callback);
123 36
    }
124
125
    /**
126
     * @param callable|callable $callback
127
     */
128 1
    public function onStasisEnd(callable $callback)
129
    {
130 1
        $this->wsClient->on(Event::STASIS_END, $callback);
131 1
    }
132
133
    /**
134
     * @return WebSocket
135
     */
136 3
    public function getWsClient()
137
    {
138 3
        return $this->wsClient;
139
    }
140
141
    /**
142
     * @return EventLoop\LoopInterface
143
     */
144 4
    public function getEventLoop()
145
    {
146 4
        return $this->eventLoop;
147
    }
148
149
    /**
150
     * @return LoggerInterface
151
     */
152 2
    public function getLogger()
153
    {
154 2
        return $this->logger;
155
    }
156
157
    /**
158
     * @return AriClient
159
     */
160 39
    public function getAriClient()
161
    {
162 39
        return $this->ariClient;
163
    }
164
165
    /**
166
     * @return AmiClient
167
     */
168 2
    public function getAmiClient()
169
    {
170 2
        return $this->amiClient;
171
    }
172
173
    /**
174
     * @return string
175
     */
176 43
    public function getStasisApplicationName()
177
    {
178 43
        return $this->stasisApplicationName;
179
    }
180
181
    /**
182
     * @return Applications
183
     */
184 12
    public function applications()
185
    {
186 12
        return $this->ariClient->applications();
187
    }
188
189
    /**
190
     * @return Asterisk
191
     */
192 7
    public function asterisk()
193
    {
194 7
        return $this->ariClient->asterisk();
195
    }
196
197
    /**
198
     * @return Bridges
199
     */
200 30
    public function bridges()
201
    {
202 30
        return $this->ariClient->bridges();
203
    }
204
205
    /**
206
     * @return Channels
207
     */
208 37
    public function channels()
209
    {
210 37
        return $this->ariClient->channels();
211
    }
212
213
    /**
214
     * @return DeviceStates
215
     */
216 7
    public function deviceStates()
217
    {
218 7
        return $this->ariClient->deviceStates();
219
    }
220
221
    /**
222
     * @return Endpoints
223
     */
224 2
    public function endPoints()
225
    {
226 2
        return $this->ariClient->endPoints();
227
    }
228
229
    /**
230
     * @return Events
231
     */
232 2
    public function events()
233
    {
234 2
        return $this->ariClient->events();
235
    }
236
237
    /**
238
     * @return Mailboxes
239
     */
240 6
    public function mailboxes()
241
    {
242 6
        return $this->ariClient->mailboxes();
243
    }
244
245
    /**
246
     * @return Playbacks
247
     */
248 5
    public function playbacks()
249
    {
250 5
        return $this->ariClient->playbacks();
251
    }
252
253
    /**
254
     * @return Recordings
255
     */
256 6
    public function recordings()
257
    {
258 6
        return $this->ariClient->recordings();
259
    }
260
261
    /**
262
     * @return Sounds
263
     */
264 3
    public function sounds()
265
    {
266 3
        return $this->ariClient->sounds();
267
    }
268
269
}