Files
mercureact/src/Broker/SseSubscriber.php

41 lines
907 B
PHP
Raw Normal View History

2024-03-10 23:06:00 +01:00
<?php
namespace NoccyLabs\Mercureact\Broker;
2024-03-11 00:36:34 +01:00
use NoccyLabs\SimpleJWT\JWTToken;
use Psr\Http\Message\ServerRequestInterface;
2024-03-10 23:06:00 +01:00
use React\Stream\WritableStreamInterface;
2024-03-11 14:39:58 +01:00
use Symfony\Component\Uid\Uuid;
2024-03-10 23:06:00 +01:00
class SseSubscriber implements SubscriberInterface
{
2024-03-11 14:39:58 +01:00
private string $id;
2024-03-10 23:06:00 +01:00
public function __construct(
2024-03-11 00:36:34 +01:00
private WritableStreamInterface $stream,
private ServerRequestInterface $request,
2024-03-10 23:06:00 +01:00
)
{
2024-03-11 14:39:58 +01:00
$this->id = (string)Uuid::v7();
2024-03-10 23:06:00 +01:00
}
public function deliver(Message $message): void
{
$this->stream->write($message->toString());
}
2024-03-11 00:36:34 +01:00
public function isAuthorized(): bool
2024-03-10 23:06:00 +01:00
{
return $this->request->getAttribute('authorized');
}
public function getPayload(): array
{
return $this->request->getAttribute('mercure.payload')??[];
2024-03-10 23:06:00 +01:00
}
2024-03-11 14:39:58 +01:00
public function getId(): string
{
return "urn:uuid:".$this->id;
}
2024-03-10 23:06:00 +01:00
}