Completed
Branch 3.0 (ea0c88)
by Brian
11:51
created

Phparia::sounds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 extends PhpariaApi
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 89
    public function __construct(LoggerInterface $logger)
73
    {
74 89
        $this->logger = $logger;
75 89
    }
76
77
    /**
78
     * Connect to ARI and optionally AMI
79
     *
80
     * @param string $ariAddress
81
     * @param string|null $amiAddress
82
     * @param array $streamOptions Example: ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]];
83
     * @param array $clientOptions Example: ['verify' => false];
84
     */
85 89
    public function connect($ariAddress, $amiAddress = null, $streamOptions = [], $clientOptions = [])
86 1
    {
87 89
        $this->eventLoop = EventLoop\Factory::create();
88 89
        $this->ariClient = new AriClient($this->eventLoop, $this->logger);
89 89
        $this->ariClient->connect($ariAddress, $streamOptions, $clientOptions);
90 89
        $this->wsClient = $this->ariClient->getWsClient();
91 89
        $this->stasisApplicationName = $this->ariClient->getStasisApplicationName();
92
93 89
        if ($amiAddress !== null) {
94 89
            $this->amiClient = new AmiClient($this->ariClient->getWsClient(), $this->eventLoop, $this->logger);
95 89
            $this->amiClient
96 89
                ->connect($amiAddress)
97 89
                ->done();
98 89
        }
99 89
    }
100
101
    /**
102
     * Connect and start the event loop
103
     */
104 40
    public function run()
105
    {
106 40
        $this->wsClient->open();
107 40
        $this->eventLoop->run();
108 24
    }
109
110
    /**
111
     * Disconnect and stop the event loop
112
     */
113 25
    public function stop()
114
    {
115 25
        $this->wsClient->close();
116 25
        $this->ariClient->onClose(function() {
117 24
            $this->eventLoop->stop();
118 25
        });
119 25
    }
120
121
    /**
122
     * @param callable|callable $callback
123
     */
124 35
    public function onStasisStart(callable $callback)
125
    {
126 35
        $this->wsClient->on(Event::STASIS_START, $callback);
127 35
    }
128
129
    /**
130
     * @param callable|callable $callback
131
     */
132
    public function onStasisEnd(callable $callback)
133
    {
134
        $this->wsClient->on(Event::STASIS_END, $callback);
135
    }
136
137
    /**
138
     * @return WebSocket
139
     */
140 3
    public function getWsClient()
141
    {
142 3
        return $this->wsClient;
143
    }
144
145
    /**
146
     * @return EventLoop\LoopInterface
147
     */
148 3
    public function getEventLoop()
149
    {
150 3
        return $this->eventLoop;
151
    }
152
153
    /**
154
     * @return LoggerInterface
155
     */
156 1
    public function getLogger()
157
    {
158 1
        return $this->logger;
159
    }
160
161
    /**
162
     * @return AriClient
163
     */
164 83
    public function getAriClient()
165
    {
166 83
        return $this->ariClient;
167
    }
168
169
    /**
170
     * @return AmiClient
171
     */
172 1
    public function getAmiClient()
173
    {
174 1
        return $this->amiClient;
175
    }
176
177
    /**
178
     * @return string
179
     */
180 38
    public function getStasisApplicationName()
181
    {
182 38
        return $this->stasisApplicationName;
183
    }
184
}