More tests and refactoring
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2023-06-15 14:10:17 +00:00
parent 72058335ca
commit f41571cfd2
9 changed files with 92 additions and 38 deletions

View File

@ -8,27 +8,41 @@ use Ntfy\Client;
class Ntfy implements NotificationInterface
{
/** @var string[] $config */
private array $config;
private Client $client;
private string $topic = 'default';
/**
* Initialize with configuration.
*
* @param string[] $config Configuration
*/
public function __construct(array $config)
public static function factory(array $config): self
{
$this->config = $config;
$instance = new self(new Client(new Server($config['domain'])));
if (isset($config['topic'])) {
$instance->setTopic($config['topic']);
}
return $instance;
}
$this->client = new Client(new Server($config['domain']));
public function __construct(Client $client)
{
$this->client = $client;
}
public function setTopic(string $topic): void
{
$this->topic = $topic;
}
public function send(string $title, string $message): void
{
assert(strlen($title) > 0);
assert(strlen($title) < 256);
assert(strlen($message) > 0);
assert(strlen($message) < 4096);
$msg = new Message();
$msg->topic($this->config['topic']);
$msg->topic($this->topic);
$msg->title($title);
$msg->body($message);
$this->client->send($msg);