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;
|
|
|
|
|
|
|
|
class SseSubscriber implements SubscriberInterface
|
|
|
|
{
|
|
|
|
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
|
|
|
)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2024-03-11 00:36:34 +01:00
|
|
|
return $this->request->getAttribute('authorization') instanceof JWTToken;
|
2024-03-10 23:06:00 +01:00
|
|
|
}
|
|
|
|
}
|