Completed
Push — master ( 78e25f...1ef0da )
by Julien
05:38
created

getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\EventSubscriber;
4
5
use Lcobucci\JWT\Builder;
6
use Lcobucci\JWT\Signer\Hmac\Sha256;
7
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
9
10
class MercureCookieInjectorSubscriber implements EventSubscriberInterface
11
{
12
13
    private $mercureJwtKey;
14
15
    public static function getSubscribedEvents()
16
    {
17
        return [
18
           'kernel.response' => 'onKernelResponse',
19
        ];
20
    }
21
22
    public function __construct(string $mercureJwtKey)
23
    {
24
        $this->mercureJwtKey = $mercureJwtKey;
25
    }
26
27
    public function onKernelResponse(FilterResponseEvent $event)
28
    {
29
30
        $token = (new Builder())
31
            ->set('mercure', [
32
                'subscribe' => [
33
                    'http://twity.io/user'
34
                ]
35
            ])
36
            ->sign(new Sha256(), $this->mercureJwtKey)
37
            ->getToken();
38
39
40
        $event->getResponse()->headers->set(
41
            'set-cookie',
42
            sprintf('mercureAuthorization=%s; path=/hub; httponly; ', $token)
43
44
        );
45
    }
46
47
}
48