Completed
Push — master ( fe47f8...2aa7aa )
by Brian
09:43
created

Phparia::mailboxes()   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

Importance

Changes 0
Metric Value
c 0
b 0
f 0
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 GuzzleHttp\Promise\FulfilledPromise;
23
use phparia\Events\Event;
24
use React\EventLoop;
25
use React\Promise\Deferred;
26
use Zend\Log\LoggerInterface;
27
28
class Phparia extends PhpariaApi
29
{
30
    /**
31
     * @var WebSocket
32
     */
33
    protected $wsClient;
34
35
    /**
36
     * @var EventLoop\LoopInterface
37
     */
38
    protected $eventLoop;
39
40
    /**
41
     * @var LoggerInterface
42
     */
43
    protected $logger;
44
45
    /**
46
     * @var AmiClient
47
     */
48
    protected $amiClient;
49
50
    /**
51
     * @var string
52
     */
53
    protected $stasisApplicationName;
54
55
    /**
56
     * @var callable
57
     */
58
    protected $onStop;
59
60
    /**
61
     * @param LoggerInterface $logger
62
     */
63
    public function __construct(LoggerInterface $logger)
64
    {
65
        $this->logger = $logger;
66
        $this->eventLoop = EventLoop\Factory::create();
67
        $ariClient = new AriClient($this->eventLoop, $this->logger);
68
        $this->onStop = function() {
69
            return new FulfilledPromise(null);
70
        };
71
72 98
        parent::__construct($ariClient);
73
    }
74 98
75 98
    /**
76
     * Connect to ARI and optionally AMI
77
     *
78
     * @param string $ariAddress
79
     * @param string|null $amiAddress
80
     * @param array $streamOptions Example: ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]];
81
     * @param array $clientOptions Example: ['verify' => false];
82
     */
83 98
    public function connect($ariAddress, $amiAddress = null, $streamOptions = [], $clientOptions = [])
84
    {
85 98
        $this->ariClient->connect($ariAddress, $streamOptions, $clientOptions);
86 98
        $this->wsClient = $this->ariClient->getWsClient();
87 98
        $this->stasisApplicationName = $this->ariClient->getStasisApplicationName();
88 98
89 98
        if ($amiAddress !== null) {
90
            $this->amiClient = new AmiClient($this->ariClient->getWsClient(), $this->eventLoop, $this->logger);
91 98
            $this->amiClient
92 98
                ->connect($amiAddress)
93 98
                ->done();
94 98
        }
95 98
    }
96 98
97 98
    /**
98
     * Connect and start the event loop
99
     */
100
    public function run()
101
    {
102 38
        $this->wsClient->open();
103
        $this->eventLoop->run();
104 38
    }
105 38
106 24
    /**
107
     * Disconnect and stop the event loop
108
     * @return \React\Promise\Promise|\React\Promise\PromiseInterface
109
     */
110
    public function stop()
111 24
    {
112
        $deferred = new Deferred();
113 24
114 24
        $onStop = $this->onStop;
115 24
        $onStop()
116 24
            ->then(function () use (&$deferred) {
117 24
                $this->ariClient->onClose(function () use (&$deferred) {
118
                    $this->eventLoop->stop();
119
                    $deferred->resolve();
120
                });
121
                $this->wsClient->close();
122 38
            });
123
124 38
        return $deferred->promise();
125 38
    }
126
127
    /**
128
     * @param callable|callback $callback Must return a promise
129
     */
130 1
    public function onStop($callback)
131
    {
132 1
        $this->onStop = $callback;
133 1
    }
134
135
    /**
136
     * @param callable|callable $callback
137
     */
138 3
    public function onStasisStart(callable $callback)
139
    {
140 3
        $this->wsClient->on(Event::STASIS_START, $callback);
141
    }
142
143
    /**
144
     * @param callable|callable $callback
145
     */
146 4
    public function onStasisEnd(callable $callback)
147
    {
148 4
        $this->wsClient->on(Event::STASIS_END, $callback);
149
    }
150
151
    /**
152
     * @return WebSocket
153
     */
154 1
    public function getWsClient()
155
    {
156 1
        return $this->wsClient;
157
    }
158
159
    /**
160
     * @return EventLoop\LoopInterface
161
     */
162 41
    public function getEventLoop()
163
    {
164 41
        return $this->eventLoop;
165
    }
166
167
    /**
168
     * @return LoggerInterface
169
     */
170 2
    public function getLogger()
171
    {
172 2
        return $this->logger;
173
    }
174
175
    /**
176
     * @return AmiClient
177
     */
178 45
    public function getAmiClient()
179
    {
180 45
        return $this->amiClient;
181
    }
182
183
    /**
184
     * @return string
185
     */
186 12
    public function getStasisApplicationName()
187
    {
188 12
        return $this->stasisApplicationName;
189
    }
190
}