1 | <?php |
||
34 | class AmiClient |
||
35 | { |
||
36 | /** |
||
37 | * @var Client |
||
38 | */ |
||
39 | protected $amiClient = null; |
||
40 | |||
41 | /** |
||
42 | * @var WebSocket |
||
43 | */ |
||
44 | protected $wsClient; |
||
45 | |||
46 | /** |
||
47 | * @var LoopInterface |
||
48 | */ |
||
49 | protected $eventLoop; |
||
50 | |||
51 | /** |
||
52 | * @var LoggerInterface |
||
53 | */ |
||
54 | protected $logger; |
||
55 | |||
56 | /** |
||
57 | * @var ActionSender |
||
58 | */ |
||
59 | protected $actionSender = null; |
||
60 | |||
61 | 97 | public function __construct(WebSocket $wsClient, LoopInterface $eventLoop, LoggerInterface $logger) |
|
62 | { |
||
63 | 97 | $this->wsClient = $wsClient; |
|
64 | 97 | $this->eventLoop = $eventLoop; |
|
65 | 97 | $this->logger = $logger; |
|
66 | 97 | } |
|
67 | |||
68 | /** |
||
69 | * Connect to AMI and start emitting events. |
||
70 | * |
||
71 | * @param string $address Example uaername:password@localhost:5038 |
||
72 | * @return \React\Promise\Promise |
||
73 | */ |
||
74 | 97 | public function connect($address) |
|
75 | { |
||
76 | 97 | $factory = new Factory($this->eventLoop); |
|
77 | |||
78 | 97 | return $factory->createClient($address) |
|
79 | ->then(function (Client $client) { |
||
80 | $this->amiClient = $client; |
||
81 | $this->actionSender = new ActionSender($client); |
||
82 | $this->actionSender->events(true); |
||
83 | $client->on('close', function () { |
||
84 | $this->logger->debug('AMI connection closed'); |
||
85 | }); |
||
86 | $client->on('event', function (Event $event) { |
||
87 | $this->wsClient->emit($event->getName(), (array)$event); |
||
88 | }); |
||
89 | 97 | }, function (\Exception $e) { |
|
90 | $this->logger->err('Connection error: '.$e->getMessage()); |
||
91 | 97 | }); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return Client |
||
96 | */ |
||
97 | public function getClient() |
||
101 | |||
102 | /** |
||
103 | * @return ActionSender |
||
104 | */ |
||
105 | public function getActionSender() |
||
109 | |||
110 | } |
||
111 |