Skip to content

Commit

Permalink
Оптимизация
Browse files Browse the repository at this point in the history
  • Loading branch information
zerox-k committed Oct 21, 2020
1 parent 3935fa1 commit c5d4101
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
class Client {
private $resource = null;
private $error_func = null;
private $is_init = false;
private $id_resource = null;

public function __construct($id_resource) {
$this->resource = msg_get_queue($id_resource);
$this->id_resource = $id_resource;
}

public static function create($id_resource) {
Expand All @@ -20,15 +22,27 @@ public function errorHandler($func) {
$this->error_func = $func;
}

private function error($error) {
if (is_callable($this->error_func))
call_user_func($this->error_func, $error);
else
throw new \Exception($error);
}

public function send($module, $msg) {
if (!$this->is_init) {
try {
$this->resource = msg_get_queue($this->id_resource);
} catch (\Throwable $e) {
$this->error($e);
return $this;
}
$this->is_init = true;
}
if (!is_array($msg))
$msg = [$msg];
if (!msg_send($this->resource, 1, [$module, $msg], true, true, $error)) {
if (is_callable($this->error_func))
call_user_func($this->error_func, $error);
else
throw new \Exception($error);
}
if (!msg_send($this->resource, 1, [$module, $msg], true, true, $error))
$this->error($error);
return $this;
}

Expand Down

0 comments on commit c5d4101

Please sign in to comment.