<?php

declare(strict_types=1);

namespace App\Service;

use Symfony\Component\Mercure\Jwt\StaticJwtProvider;
use Symfony\Component\Mercure\Publisher;
use Symfony\Component\Mercure\Update;

final class MercureBroadcasterService implements BroadcasterInterface
{
    /**
     * @var string
     */
    private $mercureUrl;
    /**
     * @var string
     */
    private $mercureJwt;

    public function __construct(string $mercureUrl, string $mercureJwt)
    {
        $this->mercureUrl = $mercureUrl;
        $this->mercureJwt = $mercureJwt;
    }

    /**
     * @param string $message
     */
    public function broadcast(string $message): void
    {
        $update = new Update(sprintf('%s/stream', $this->mercureUrl), $message);

        (new Publisher(sprintf('%s/hub', $this->mercureUrl), new StaticJwtProvider($this->mercureJwt)))($update);
    }
}
