Beanstalkd -> Pheanstalk
This commit is contained in:
124
cli/src/Service/MQTT.php
Normal file
124
cli/src/Service/MQTT.php
Normal file
@ -0,0 +1,124 @@
|
||||
<?php
|
||||
namespace Incoviba\Service;
|
||||
|
||||
use Incoviba\Exception\MQTT as MQTTException;
|
||||
use Incoviba\Service;
|
||||
use Incoviba\Service\MQTT\MQTTInterface;
|
||||
|
||||
class MQTT extends Service implements MQTTInterface
|
||||
{
|
||||
protected array $transports = [];
|
||||
public function register(string $name, MQTTInterface $transport): self
|
||||
{
|
||||
$this->transports[$name] = $transport;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $payload
|
||||
* @param int $delay
|
||||
* @param string|null $transportName
|
||||
* @return $this
|
||||
* @throws MQTTException\UnknownTransport
|
||||
* @throws MQTTException\Create
|
||||
*/
|
||||
public function set(string $payload, int $delay = 0, ?string $transportName = null): self
|
||||
{
|
||||
$transport = $this->getTransport($transportName);
|
||||
$transport->set($payload, $delay);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $transportName
|
||||
* @return int
|
||||
* @throws MQTTException\UnknownTransport
|
||||
* @throws MQTTException\Read
|
||||
*/
|
||||
public function pending(?string $transportName = null): int
|
||||
{
|
||||
$transport = $this->getTransport($transportName);
|
||||
return $transport->pending();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $jobId
|
||||
* @param string|null $transportName
|
||||
* @return bool
|
||||
* @throws MQTTException\UnknownTransport
|
||||
* @throws MQTTException\Read
|
||||
*/
|
||||
public function exists(?int $jobId = null, ?string $transportName = null): bool
|
||||
{
|
||||
$transport = $this->getTransport($transportName);
|
||||
return $transport->exists($jobId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $jobId
|
||||
* @param string|null $transportName
|
||||
* @return string
|
||||
* @throws MQTTException\UnknownTransport
|
||||
* @throws MQTTException\Read
|
||||
*/
|
||||
public function get(?int $jobId = null, ?string $transportName = null): string
|
||||
{
|
||||
$transport = $this->getTransport($transportName);
|
||||
return $transport->get($jobId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $newPayload
|
||||
* @param int|null $jobId
|
||||
* @param string|null $transportName
|
||||
* @return $this
|
||||
* @throws MQTTException\UnknownTransport
|
||||
* @throws MQTTException\Update
|
||||
*/
|
||||
public function update(string $newPayload, ?int $jobId = null, ?string $transportName = null): self
|
||||
{
|
||||
$transport = $this->getTransport($transportName);
|
||||
$transport->update($newPayload, $jobId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int|null $jobId
|
||||
* @param string|null $transportName
|
||||
* @return $this
|
||||
* @throws MQTTException\UnknownTransport
|
||||
* @throws MQTTException\Delete
|
||||
*/
|
||||
public function remove(?int $jobId = null, ?string $transportName = null): self
|
||||
{
|
||||
$transport = $this->getTransport($transportName);
|
||||
$transport->remove($jobId);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $transportName
|
||||
* @return mixed
|
||||
* @throws MQTTException\UnknownTransport
|
||||
*/
|
||||
protected function getTransport(?string $transportName): mixed
|
||||
{
|
||||
if (count($this->transports) === 0) {
|
||||
throw new MQTTException\UnknownTransport('');
|
||||
}
|
||||
if ($transportName === null) {
|
||||
if (array_key_exists('default', $this->transports)) {
|
||||
$transportName = 'default';
|
||||
} else {
|
||||
$transportName = array_keys($this->transports)[0];
|
||||
}
|
||||
}
|
||||
if (!array_key_exists($transportName, $this->transports)) {
|
||||
if ($transportName === null) {
|
||||
$transportName = '';
|
||||
}
|
||||
throw new MQTTException\UnknownTransport($transportName);
|
||||
}
|
||||
return $this->transports[$transportName];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user