vendor/symfony/mercure/src/Publisher.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Mercure Component project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Symfony\Component\Mercure;
  12. use Symfony\Component\HttpClient\HttpClient;
  13. use Symfony\Component\Mercure\Internal\QueryBuilder;
  14. use Symfony\Component\Mercure\Jwt\TokenProviderInterface;
  15. use Symfony\Contracts\HttpClient\HttpClientInterface;
  16. trigger_deprecation('symfony/mercure''0.5''Class "%s" is deprecated, use "%s" instead.'Publisher::class, Hub::class);
  17. /**
  18.  * Publishes an update to the hub.
  19.  *
  20.  * @author Kévin Dunglas <dunglas@gmail.com>
  21.  *
  22.  * @experimental
  23.  *
  24.  * @deprecated
  25.  */
  26. final class Publisher implements PublisherInterface
  27. {
  28.     private $hubUrl;
  29.     private $jwtProvider;
  30.     private $httpClient;
  31.     /**
  32.      * @param TokenProviderInterface|callable(Update $update):string $jwtProvider
  33.      */
  34.     public function __construct(string $hubUrl$jwtProviderHttpClientInterface $httpClient null)
  35.     {
  36.         $this->hubUrl $hubUrl;
  37.         $this->jwtProvider $jwtProvider;
  38.         $this->httpClient $httpClient ?? HttpClient::create();
  39.     }
  40.     public function __invoke(Update $update): string
  41.     {
  42.         $postData = [
  43.             'topic' => $update->getTopics(),
  44.             'data' => $update->getData(),
  45.             'private' => $update->isPrivate() ? 'on' null,
  46.             'id' => $update->getId(),
  47.             'type' => $update->getType(),
  48.             'retry' => $update->getRetry(),
  49.         ];
  50.         if ($this->jwtProvider instanceof TokenProviderInterface) {
  51.             $jwt $this->jwtProvider->getJwt();
  52.         } else {
  53.             $jwt = ($this->jwtProvider)($update);
  54.         }
  55.         $this->validateJwt($jwt);
  56.         return $this->httpClient->request('POST'$this->hubUrl, [
  57.             'auth_bearer' => $jwt,
  58.             'body' => QueryBuilder::build($postData),
  59.         ])->getContent();
  60.     }
  61.     /**
  62.      * Regex ported from Windows Azure Active Directory IdentityModel Extensions for .Net.
  63.      *
  64.      * @throws Exception\InvalidArgumentException
  65.      *
  66.      * @license MIT
  67.      * @copyright Copyright (c) Microsoft Corporation
  68.      *
  69.      * @see https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/6e7a53e241e4566998d3bf365f03acd0da699a31/src/Microsoft.IdentityModel.JsonWebTokens/JwtConstants.cs#L58
  70.      */
  71.     private function validateJwt(string $jwt): void
  72.     {
  73.         if (!preg_match('/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/'$jwt)) {
  74.             throw new Exception\InvalidArgumentException('The provided JWT is not valid.');
  75.         }
  76.     }
  77. }