. */ namespace App\Libraries; use App\Device; class FlexisipConnector { private $_socket; public function __construct() { $pid = \trim(\shell_exec('pidof flexisip')); $this->_socket = stream_socket_client('unix:///tmp/flexisip-proxy-'.$pid, $errno, $errstr); } public function __destruct() { fclose($this->_socket); } public function getDevices(string $from) { $content = $this->request('REGISTRAR_GET', [ 'sip:'.$from ]); $devices = collect(); if (isset($content->contacts)) { foreach ($content->contacts as $contact) { $device = new Device; $device->fromContact($contact); $devices->push($device); } } return $devices; } public function deleteDevice(string $from, string $uuid) { $content = $this->request('REGISTRAR_DELETE', [ 'sip:'.$from, '"<'.$uuid.'>"', ]); } private function request(string $command, array $parameters) { fwrite($this->_socket, $command.' '.\implode(' ', $parameters)); return json_decode(fread($this->_socket, 8192)); } }