Refactoring, logging improvements
This commit is contained in:
@ -9,4 +9,6 @@ interface SubscriberInterface
|
||||
public function isAuthorized(): bool;
|
||||
|
||||
public function getPayload(): ?array;
|
||||
|
||||
public function getId(): string;
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace NoccyLabs\Mercureact\Broker;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use SplObjectStorage;
|
||||
|
||||
class TopicManager
|
||||
@ -13,7 +14,7 @@ class TopicManager
|
||||
|
||||
private SplObjectStorage $subscribers;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(private LoggerInterface $logger)
|
||||
{
|
||||
$this->subscribers = new SplObjectStorage();
|
||||
}
|
||||
@ -21,6 +22,7 @@ class TopicManager
|
||||
public function getTopic(string $topic): Topic
|
||||
{
|
||||
if (!isset($this->topics[$topic])) {
|
||||
$this->logger->debug("Created topic: {$topic}");
|
||||
$this->topics[$topic] = new Topic($topic);
|
||||
}
|
||||
return $this->topics[$topic];
|
||||
@ -30,12 +32,14 @@ class TopicManager
|
||||
{
|
||||
$this->lastEventId = $message->id;
|
||||
foreach ($message->topic as $topic) {
|
||||
$this->logger->debug("Publish: {$message->id} → ".json_encode($message->topic,JSON_UNESCAPED_SLASHES));
|
||||
$this->getTopic($topic)->publish($message);
|
||||
}
|
||||
}
|
||||
|
||||
public function subscribe(SubscriberInterface $subscriber, array $topics): void
|
||||
{
|
||||
$this->logger->debug("Subscribed: ".$subscriber->getId()." + ".json_encode($topics,JSON_UNESCAPED_SLASHES));
|
||||
$this->subscribers->attach($subscriber);
|
||||
foreach ($topics as $topic) {
|
||||
$this->getTopic($topic)->addSubscriber($subscriber);
|
||||
@ -44,8 +48,9 @@ class TopicManager
|
||||
|
||||
public function unsubscribe(SubscriberInterface $subscriber, ?array $topics=null): void
|
||||
{
|
||||
$this->subscribers->detach($subscriber);
|
||||
$this->logger->debug("Unsubscribed: ".$subscriber->getId()." - ".json_encode($topics,JSON_UNESCAPED_SLASHES));
|
||||
if (!$topics) {
|
||||
$this->subscribers->detach($subscriber);
|
||||
foreach ($this->topics as $topic) {
|
||||
$topic->removeSubscriber($subscriber);
|
||||
}
|
||||
|
Reference in New Issue
Block a user