Client   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 170
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 170
ccs 48
cts 48
cp 1
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getHost() 0 4 1
A getConnection() 0 4 1
A getParams() 0 4 1
A setParams() 0 6 1
A connect() 0 13 2
A disconnect() 0 13 2
A process() 0 14 2
A sendAction() 0 21 2
1
<?php
2
3
namespace PamiModule\Service;
4
5
use ArrayObject;
6
use PAMI\Client\Impl\ClientImpl;
7
use PAMI\Message\OutgoingMessage;
8
use PAMI\Message\Response\ResponseMessage;
9
use Zend\EventManager\Event;
10
use Zend\EventManager\EventManagerAwareTrait;
11
use Zend\EventManager\EventsCapableInterface;
12
13
/**
14
 * Class Client.
15
 */
16
class Client implements EventsCapableInterface
17
{
18
    use EventManagerAwareTrait;
19
20
    /**
21
     * PAMI client.
22
     *
23
     * @var ClientImpl
24
     */
25
    protected $connection;
26
    /**
27
     * Custom parameters.
28
     *
29
     * @var array
30
     */
31
    protected $params = [];
32
    /**
33
     * @var string
34
     */
35
    protected $host;
36
37
    /**
38
     * Client constructor.
39
     *
40
     * @param string     $host
41
     * @param ClientImpl $pami PAMI client
42
     */
43 10
    public function __construct($host, ClientImpl $pami)
44
    {
45 10
        $this->host = $host;
46 10
        $this->connection = $pami;
47 10
    }
48
49
    /**
50
     * Return the hostname of the connection.
51
     *
52
     * @return string
53
     */
54 1
    public function getHost()
55
    {
56 1
        return $this->host;
57
    }
58
59
    /**
60
     * Return the PAMI client.
61
     *
62
     * @return ClientImpl
63
     */
64 2
    public function getConnection()
65
    {
66 2
        return $this->connection;
67
    }
68
69
    /**
70
     * Return the custom parameters.
71
     *
72
     * @return array
73
     */
74 2
    public function getParams()
75
    {
76 2
        return $this->params;
77
    }
78
79
    /**
80
     * Set the custom parameters.
81
     *
82
     * @param array $params Parameters
83
     *
84
     * @return $this
85
     */
86 2
    public function setParams(array $params)
87
    {
88 2
        $this->params = $params;
89
90 2
        return $this;
91
    }
92
93
    /**
94
     * Connect to the Asterisk Manager Interface.
95
     *
96
     * @throws \PAMI\Client\Exception\ClientException
97
     *
98
     * @return $this
99
     */
100 2
    public function connect()
101
    {
102 2
        $results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this);
103 2
        if ($results->stopped()) {
104 1
            return $this;
105
        }
106
107 1
        $this->connection->open();
108
109 1
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this);
110
111 1
        return $this;
112
    }
113
114
    /**
115
     * Disconnect from the Asterisk Manager Interface.
116
     *
117
     * @return $this
118
     */
119 2
    public function disconnect()
120
    {
121 2
        $results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this);
122 2
        if ($results->stopped()) {
123 1
            return $this;
124
        }
125
126 1
        $this->connection->close();
127
128 1
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this);
129
130 1
        return $this;
131
    }
132
133
    /**
134
     * Main processing loop. Also called from send(), you should call this in
135
     * your own application in order to continue reading events and responses
136
     * from ami.
137
     *
138
     * @return $this
139
     */
140 2
    public function process()
141
    {
142 2
        $results = $this->getEventManager()->trigger(__FUNCTION__ . '.pre', $this);
143
144 2
        if ($results->stopped()) {
145 1
            return $this;
146
        }
147
148 1
        $this->connection->process();
149
150 1
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this);
151
152 1
        return $this;
153
    }
154
155
    /**
156
     * Sends a message to AMI.
157
     *
158
     * @param OutgoingMessage $action Action to send
159
     *
160
     * @throws \PAMI\Client\Exception\ClientException
161
     *
162
     * @return \PAMI\Message\Response\ResponseMessage
163
     */
164 2
    public function sendAction(OutgoingMessage $action)
165
    {
166 2
        $params = new ArrayObject(['action' => $action]);
167 2
        $event = new Event(__FUNCTION__ . '.pre', $this, $params);
168 2
        $results = $this->getEventManager()->triggerEventUntil(
169 2
            function ($response) {
170 1
                return $response instanceof ResponseMessage;
171 2
            },
172
            $event
173 2
        );
174 2
        if ($results->stopped()) {
175 1
            return $results->last();
176
        }
177
178 1
        $response = $this->connection->send($action);
179
180 1
        $params['response'] = $response;
181 1
        $this->getEventManager()->trigger(__FUNCTION__ . '.post', $this, $params);
182
183 1
        return $response;
184
    }
185
}
186