Symfony Mercure (bundle) Cookiegenerator.php problem

71 Views Asked by At

i try to fix the Cookiegenerator.php file for generate a tokken with my JWT key but i have somes errors :

            <?php

            declare(strict_types=1);

            namespace App\Services\Mercure;

            use App\Entity\Channel;
            use Lcobucci\JWT\Token\Builder;
            use Lcobucci\JWT\Signer\Hmac\Sha256;
            use Lcobucci\JWT\Signer\Key;

            class CookieGenerator
            {
                private string $key;

                public function __construct(string $key)
                {
                    $this->key = $key;
                }

                public function __invoke(Channel $channel): string
                {
                    $signer = new Sha256();
                    return (new Builder())
                        ->withClaim('mercure', ['subscribe' => [sprintf('http://astrochat.com/channel/%s', $channel->getId())]])
                        ->getToken($signer, new Key($this->key))
                        ->__toString()
                    ;
                }
            }

Errors :

Expected 2 arguments. Found 0. (L 24) Undefined method '__toString'. (L 27)

I tried to redo the file and pass some parameters to Builder but nothing to do... Basically the file uses the use use Lcobucci\JWT\Builder; I thought I understood that it was no longer used so I use use Lcobucci\JWT\Token\Builder; Someone would have any idea ? Thanks a lot

1

There are 1 best solutions below

0
Tobias On

As discussed in the comments above, the token class no longer implements the Stringable interface and also has no __toString() method (which would implicitly implement it as of PHP 8.0), so the call to the magic string method:

 ->__toString()

now needs to be updated to this:

 ->toString()

Also the Builder instance now requires 2 parameters (docs), an encoder and a formatter, example:

$tokenBuilder = (new Builder(new \Lcobucci\JWT\Encoding\JoseEncoder(), \Lcobucci\JWT\Encoding\ChainedFormatter::default()));