47 lines
1.1 KiB
PHP
47 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace NoccyLabs\Mercureact\Broker\Subscriber;
|
|
|
|
use NoccyLabs\Mercureact\Broker\Message;
|
|
use NoccyLabs\SimpleJWT\JWTToken;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use React\Stream\WritableStreamInterface;
|
|
use Symfony\Component\Uid\Uuid;
|
|
|
|
class SseSubscriber implements SubscriberInterface
|
|
{
|
|
private string $id;
|
|
|
|
public function __construct(
|
|
private WritableStreamInterface $stream,
|
|
private ServerRequestInterface $request,
|
|
)
|
|
{
|
|
$this->id = (string)Uuid::v7();
|
|
}
|
|
|
|
public function deliver(Message $message): void
|
|
{
|
|
$this->stream->write($message->toString());
|
|
}
|
|
|
|
public function isAuthenticated(): bool
|
|
{
|
|
return $this->request->getAttribute('authorized');
|
|
}
|
|
|
|
public function getMercureClaims(): ?array
|
|
{
|
|
return $this->request->getAttribute('mercure.claims');
|
|
}
|
|
|
|
public function getPayload(): array
|
|
{
|
|
return $this->request->getAttribute('mercure.payload')??[];
|
|
}
|
|
|
|
public function getId(): string
|
|
{
|
|
return "urn:uuid:".$this->id;
|
|
}
|
|
} |