Skip to content

Commit 25275f4

Browse files
committed
[UC-30] Introduce cookie queue handling and implement CookieFlusherSubscriber
1 parent 93c7545 commit 25275f4

8 files changed

Lines changed: 115 additions & 6 deletions

File tree

config/services/cookie.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
4+
<services>
5+
<defaults autowire="false" autoconfigure="false"/>
6+
<service id="bit_bag.sylius_user_com_plugin.cookie.cookie_queue" class="BitBag\SyliusUserComPlugin\Cookie\CookieQueue" />
7+
</services>
8+
</container>

config/services/event_subscriber.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,13 @@
1515
<argument type="service" id="monolog.logger.user_com"/>
1616
<tag name="kernel.event_subscriber"/>
1717
</service>
18+
19+
<service id="bit_bag.sylius_user_com_plugin.event_subscriber.cookie_flusher_subscriber" class="BitBag\SyliusUserComPlugin\EventSubscriber\CookieFlusherSubscriber">
20+
<argument type="service" id="bit_bag.sylius_user_com_plugin.cookie.cookie_queue" />
21+
<tag name="kernel.event_listener"
22+
event="kernel.response"
23+
method="onResponse"
24+
priority="0"/>
25+
</service>
1826
</services>
1927
</container>

config/services/manager.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
>
1010
<argument type="service" id="request_stack"/>
1111
<argument type="service" id="security.token_storage"/>
12+
<argument type="service" id="bit_bag.sylius_user_com_plugin.cookie.cookie_queue"/>
1213
</service>
1314

1415
<service

src/Cookie/CookieQueue.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Cookie;
13+
14+
use Symfony\Component\HttpFoundation\Cookie;
15+
16+
final class CookieQueue implements CookieQueueInterface
17+
{
18+
private array $queued = [];
19+
20+
public function queue(Cookie $cookie): void
21+
{
22+
$this->queued[] = $cookie;
23+
}
24+
25+
/** @return Cookie[] */
26+
public function pullAll(): array
27+
{
28+
$all = $this->queued;
29+
$this->queued = [];
30+
31+
return $all;
32+
}
33+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\Cookie;
13+
14+
use Symfony\Component\HttpFoundation\Cookie;
15+
16+
interface CookieQueueInterface
17+
{
18+
public function queue(Cookie $cookie): void;
19+
20+
/** @return Cookie[] */
21+
public function pullAll(): array;
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file has been created by developers from BitBag.
5+
* Feel free to contact us once you face any issues or want to start
6+
* You can find more information about us on https://bitbag.io and write us
7+
* an email on hello@bitbag.io.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace BitBag\SyliusUserComPlugin\EventSubscriber;
13+
14+
use BitBag\SyliusUserComPlugin\Cookie\CookieQueueInterface;
15+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
16+
17+
final class CookieFlusherSubscriber
18+
{
19+
public function __construct(private readonly CookieQueueInterface $queue)
20+
{
21+
}
22+
23+
public function onResponse(ResponseEvent $event): void
24+
{
25+
$response = $event->getResponse();
26+
27+
foreach ($this->queue->pullAll() as $cookie) {
28+
$response->headers->setCookie($cookie);
29+
}
30+
}
31+
}

src/Manager/CookieManager.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111

1212
namespace BitBag\SyliusUserComPlugin\Manager;
1313

14+
use BitBag\SyliusUserComPlugin\Cookie\CookieQueueInterface;
1415
use Sylius\Component\Core\Model\AdminUserInterface;
16+
use Symfony\Component\HttpFoundation\Cookie;
1517
use Symfony\Component\HttpFoundation\RequestStack;
1618
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1719

@@ -20,6 +22,7 @@ final class CookieManager implements CookieManagerInterface
2022
public function __construct(
2123
private readonly RequestStack $requestStack,
2224
private readonly TokenStorageInterface $tokenStorage,
25+
private readonly CookieQueueInterface $queue,
2326
) {
2427
}
2528

@@ -41,11 +44,14 @@ public function getUserComCookie(): ?string
4144

4245
public function setUserComCookie(string $value): void
4346
{
44-
$request = $this->requestStack->getCurrentRequest();
45-
if (null === $request) {
46-
return;
47-
}
48-
$request->cookies->set(self::CHAT_COOKIE_NAME, $value);
47+
$cookie = Cookie::create(self::CHAT_COOKIE_NAME)
48+
->withValue($value)
49+
->withPath('/')
50+
->withSecure(true)
51+
->withHttpOnly(true)
52+
->withSameSite('lax');
53+
54+
$this->queue->queue($cookie);
4955
}
5056

5157
private function isShopUser(): bool

src/Updater/CustomerWithKeyUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private function updateForUserWithoutEmail(
154154
$this->userApi->mergeUsers($apiAwareResource, $customerFoundByEmail['id'], [$userFromUserKey['id']]);
155155
}
156156

157-
$this->sendEvent($apiAwareResource, $email, $eventName, $payload);
157+
$this->changeCookieWithEvent($user, $apiAwareResource, $eventName);
158158

159159
return $user;
160160
}

0 commit comments

Comments
 (0)