Beanstalkd for jobs
This commit is contained in:
102
app/src/Service/MQTT.php
Normal file
102
app/src/Service/MQTT.php
Normal file
@ -0,0 +1,102 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use Incoviba\Exception\MQTT as MQTTException;
|
||||
use Incoviba\Service\MQTT\MQTTInterface;
|
||||
|
||||
class MQTT implements MQTTInterface
|
||||
{
|
||||
protected array $clients = [];
|
||||
public function register(string $name, MQTTInterface $client): self
|
||||
{
|
||||
$this->clients[$name] = $client;
|
||||
return $this;
|
||||
}
|
||||
public function clientExists(string $name): bool
|
||||
{
|
||||
return isset($this->clients[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $name
|
||||
* @return MQTTInterface
|
||||
* @throws MQTTException/MissingClient
|
||||
*/
|
||||
public function getClient(?string $name = null): MQTTInterface
|
||||
{
|
||||
if ($name === null) {
|
||||
$name = array_keys($this->clients)[0];
|
||||
}
|
||||
if (!$this->clientExists($name)) {
|
||||
throw new MQTTException\MissingClient($name);
|
||||
}
|
||||
return $this->clients[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $host
|
||||
* @return bool
|
||||
* @throws MQTTException/MissingClient
|
||||
* @throws MQTTException/MissingJob
|
||||
*/
|
||||
public function exists(?string $host = null): bool
|
||||
{
|
||||
$client = $this->getClient($host);
|
||||
return $client->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $host
|
||||
* @return string
|
||||
* @throws MQTTException/MissingClient
|
||||
* @throws MQTTException/MissingJob
|
||||
*/
|
||||
public function get(?string $host = null): string
|
||||
{
|
||||
$client = $this->getClient($host);
|
||||
return $client->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
* @param int $delay
|
||||
* @param string|null $host
|
||||
* @return $this
|
||||
* @throws MQTTException/MissingClient
|
||||
* @throws MQTTException/SetJob
|
||||
*/
|
||||
public function set(string $value, int $delay = 0, ?string $host = null): self
|
||||
{
|
||||
$client = $this->getClient($host);
|
||||
$client->set($value, $delay);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $jobId
|
||||
* @param string|null $host
|
||||
* @return $this
|
||||
* @throws MQTTException/MissingJob
|
||||
* @throws MQTTException/RemoveJob
|
||||
*/
|
||||
public function remove(?int $jobId = null, ?string $host = null): self
|
||||
{
|
||||
$this->getClient($host)->remove($jobId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $newPayload
|
||||
* @param int|null $jobId
|
||||
* @param string|null $host
|
||||
* @return $this
|
||||
* @throws MQTTException/MissingJob
|
||||
* @throws MQTTException/RemoveJob
|
||||
* @throws MQTTException/SetJob
|
||||
*/
|
||||
public function update(string $newPayload, ?int $jobId = null, ?string $host = null): self
|
||||
{
|
||||
$this->getClient($host)->update($newPayload, $jobId);
|
||||
return $this;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user