Phparia   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 60.38%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 9
dl 0
loc 163
ccs 32
cts 53
cp 0.6038
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A connect() 0 13 2
A run() 0 5 1
A stop() 0 16 1
A onStop() 0 4 1
A onStasisStart() 0 4 1
A onStasisEnd() 0 4 1
A getWsClient() 0 4 1
A getEventLoop() 0 4 1
A getLogger() 0 4 1
A getAmiClient() 0 4 1
A getStasisApplicationName() 0 4 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 88
    public function __construct(LoggerInterface $logger)
64
    {
65 88
        $this->logger = $logger;
66 88
        $this->eventLoop = EventLoop\Factory::create();
67 88
        $ariClient = new AriClient($this->eventLoop, $this->logger);
68
        $this->onStop = function() {
69
            return new FulfilledPromise(null);
70
        };
71
72 88
        parent::__construct($ariClient);
73 88
    }
74
75
    /**
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 88
    public function connect($ariAddress, $amiAddress = null, $streamOptions = [], $clientOptions = [])
84
    {
85 88
        $this->ariClient->connect($ariAddress, $streamOptions, $clientOptions);
86 88
        $this->wsClient = $this->ariClient->getWsClient();
87 88
        $this->stasisApplicationName = $this->ariClient->getStasisApplicationName();
88
89 88
        if ($amiAddress !== null) {
90 88
            $this->amiClient = new AmiClient($this->ariClient->getWsClient(), $this->eventLoop, $this->logger);
91 88
            $this->amiClient
92 88
                ->connect($amiAddress)
93 88
                ->done();
94 88
        }
95 88
    }
96
97
    /**
98
     * Connect and start the event loop
99
     */
100 40
    public function run()
101
    {
102 40
        $this->wsClient->open();
103 40
        $this->eventLoop->run();
104 40
    }
105
106
    /**
107
     * Disconnect and stop the event loop
108
     * @return \React\Promise\Promise|\React\Promise\PromiseInterface
109
     */
110
    public function stop()
111
    {
112
        $deferred = new Deferred();
113
114
        $onStop = $this->onStop;
115
        $onStop()
116
            ->then(function () use (&$deferred) {
117
                $this->ariClient->onClose(function () use (&$deferred) {
118
                    $this->eventLoop->stop();
119
                    $deferred->resolve();
120
                });
121
                $this->wsClient->close();
122
            });
123
124
        return $deferred->promise();
125
    }
126
127
    /**
128
     * @param callable|callback $callback Must return a promise
129
     */
130
    public function onStop($callback)
131
    {
132
        $this->onStop = $callback;
133
    }
134
135
    /**
136
     * @param callable|callable $callback
137
     */
138 35
    public function onStasisStart(callable $callback)
139
    {
140 35
        $this->wsClient->on(Event::STASIS_START, $callback);
141 35
    }
142
143
    /**
144
     * @param callable|callable $callback
145
     */
146
    public function onStasisEnd(callable $callback)
147
    {
148
        $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 1
    public function getEventLoop()
163
    {
164 1
        return $this->eventLoop;
165
    }
166
167
    /**
168
     * @return LoggerInterface
169
     */
170 1
    public function getLogger()
171
    {
172 1
        return $this->logger;
173
    }
174
175
    /**
176
     * @return AmiClient
177
     */
178 1
    public function getAmiClient()
179
    {
180 1
        return $this->amiClient;
181
    }
182
183
    /**
184
     * @return string
185
     */
186
    public function getStasisApplicationName()
187
    {
188
        return $this->stasisApplicationName;
189
    }
190
}