Completed
Branch 3.0 (5a53f1)
by Brian
04:39
created

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