. */ namespace App\Libraries; use App\Device; class FlexisipConnector { private $socket; public function __construct() { $pid = \file_get_contents(config('app.flexisip_proxy_pid')); $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 ($content && 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) { $this->request('REGISTRAR_DELETE', [ 'sip:'.$from, '"<'.$uuid.'>"', ]); } private function request(string $command, array $parameters): ?\stdClass { fwrite($this->socket, $command.' '.\implode(' ', $parameters)); return json_decode(fread($this->socket, 8192)); } }