Skip to content

Commit a247e9f

Browse files
authored
Merge pull request #1 from Brille24/fix_cart_id_in_cookie
Cookies saving token instead of id
2 parents 6467357 + 3c0f400 commit a247e9f

18 files changed

Lines changed: 135 additions & 36 deletions

spec/Context/WishlistContextSpec.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ function it_returns_cookie_wishlist_if_cookie_and_no_user(
6767
WishlistInterface $wishlist
6868
): void {
6969
$request->cookies = $parameterBag;
70-
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
70+
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
7171
$tokenStorage->getToken()->willReturn($token);
7272
$token->getUser()->willReturn(null);
73-
$wishlistRepository->find(1)->willReturn($wishlist);
73+
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn($wishlist);
7474

7575
$this->getWishlist($request)->shouldReturn($wishlist);
7676
}
@@ -85,10 +85,10 @@ function it_returns_new_wishlist_if_cookie_not_found_and_no_user(
8585
WishlistInterface $wishlist
8686
): void {
8787
$request->cookies = $parameterBag;
88-
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
88+
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
8989
$tokenStorage->getToken()->willReturn($token);
9090
$token->getUser()->willReturn(null);
91-
$wishlistRepository->find(1)->willReturn(null);
91+
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn(null);
9292
$wishlistFactory->createNew()->willReturn($wishlist);
9393

9494
$this->getWishlist($request)->shouldReturn($wishlist);

spec/Controller/Action/AddProductToWishlistActionSpec.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function let(
3939
$flashBag,
4040
$translator,
4141
$urlGenerator,
42-
'bitbag_wishlist_id'
42+
'bitbag_wishlist_token'
4343
);
4444
}
4545

@@ -81,7 +81,7 @@ function it_handles_the_request_and_persist_new_wishlist_if_needed(
8181
$wishlistManager->persist($wishlist)->shouldBeCalled();
8282
$wishlistManager->flush()->shouldBeCalled();
8383
$flashBag->add('success', 'Product has been added to your wishlist.')->shouldBeCalled();
84-
$wishlist->getId()->shouldBeCalled();
84+
$wishlist->getToken()->shouldBeCalled();
8585

8686
$this->__invoke($request)->shouldHaveType(RedirectResponse::class);
8787
}

spec/EventListener/MergeUserWishlistItemsListenerSpec.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ function it_adds_cookie_items_to_user_items_if_both_exist(
6969
$token->getUser()->willReturn($shopUser);
7070
$interactiveLoginEvent->getRequest()->willReturn($request);
7171
$request->cookies = $parameterBag;
72-
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
73-
$wishlistRepository->find(1)->willReturn($cookieWishlist);
72+
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
73+
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn($cookieWishlist);
7474
$wishlistRepository->findByShopUser($shopUser)->willReturn($userWishlist);
7575
$cookieWishlist->getWishlistProducts()->willReturn(new ArrayCollection([$wishlistProduct->getWrappedObject()]));
7676

@@ -94,8 +94,8 @@ function it_associates_anon_wishlsit_with_a_user_if_user_does_not_have_one(
9494
$token->getUser()->willReturn($shopUser);
9595
$interactiveLoginEvent->getRequest()->willReturn($request);
9696
$request->cookies = $parameterBag;
97-
$parameterBag->get('bitbag_sylius_wishlist')->willReturn(1);
98-
$wishlistRepository->find(1)->willReturn($cookieWishlist);
97+
$parameterBag->get('bitbag_sylius_wishlist')->willReturn("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId");
98+
$wishlistRepository->findByToken("Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId")->willReturn($cookieWishlist);
9999
$wishlistRepository->findByShopUser($shopUser)->willReturn(null);
100100

101101
$cookieWishlist->setShopUser($shopUser)->shouldBeCalled();

src/Context/WishlistContext.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,33 @@ final class WishlistContext implements WishlistContextInterface
3131
private $wishlistFactory;
3232

3333
/** @var string */
34-
private $wishlistCookieId;
34+
private $wishlistCookieToken;
3535

3636
public function __construct(
3737
TokenStorageInterface $tokenStorage,
3838
WishlistRepositoryInterface $wishlistRepository,
3939
WishlistFactoryInterface $wishlistFactory,
40-
string $wishlistCookieId
40+
string $wishlistCookieToken
4141
) {
4242
$this->tokenStorage = $tokenStorage;
4343
$this->wishlistRepository = $wishlistRepository;
4444
$this->wishlistFactory = $wishlistFactory;
45-
$this->wishlistCookieId = $wishlistCookieId;
45+
$this->wishlistCookieToken = $wishlistCookieToken;
4646
}
4747

4848
public function getWishlist(Request $request): WishlistInterface
4949
{
50-
$cookieWishlistId = $request->cookies->get($this->wishlistCookieId);
50+
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken);
5151
$token = $this->tokenStorage->getToken();
5252
$user = $token ? $token->getUser() : null;
5353

54-
if (null === $cookieWishlistId && null === $user) {
54+
if (null === $cookieWishlistToken && null === $user) {
5555
return $this->wishlistFactory->createNew();
5656
}
5757

58-
if (null !== $cookieWishlistId && !$user instanceof ShopUserInterface) {
59-
return $this->wishlistRepository->find($cookieWishlistId) ?
60-
$this->wishlistRepository->find($cookieWishlistId) :
58+
if (null !== $cookieWishlistToken && !$user instanceof ShopUserInterface) {
59+
return $this->wishlistRepository->findByToken($cookieWishlistToken) ?
60+
$this->wishlistRepository->findByToken($cookieWishlistToken) :
6161
$this->wishlistFactory->createNew()
6262
;
6363
}

src/Controller/Action/AddProductToWishlistAction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ final class AddProductToWishlistAction
5151
private $urlGenerator;
5252

5353
/** @var string */
54-
private $wishlistCookieId;
54+
private $wishlistCookieToken;
5555

5656
public function __construct(
5757
ProductRepositoryInterface $productRepository,
@@ -61,14 +61,14 @@ public function __construct(
6161
FlashBagInterface $flashBag,
6262
TranslatorInterface $translator,
6363
UrlGeneratorInterface $urlGenerator,
64-
string $wishlistCookieId
64+
string $wishlistCookieToken
6565
) {
6666
$this->productRepository = $productRepository;
6767
$this->wishlistContext = $wishlistContext;
6868
$this->wishlistProductFactory = $wishlistProductFactory;
6969
$this->wishlistManager = $wishlistManager;
7070
$this->urlGenerator = $urlGenerator;
71-
$this->wishlistCookieId = $wishlistCookieId;
71+
$this->wishlistCookieToken = $wishlistCookieToken;
7272
$this->flashBag = $flashBag;
7373
$this->translator = $translator;
7474
}
@@ -95,7 +95,7 @@ public function __invoke(Request $request): Response
9595
$this->wishlistManager->flush();
9696
$this->flashBag->add('success', $this->translator->trans('bitbag_sylius_wishlist_plugin.ui.added_wishlist_item'));
9797

98-
$cookie = new Cookie($this->wishlistCookieId, $wishlist->getId(), strtotime('+1 year'));
98+
$cookie = new Cookie($this->wishlistCookieToken, $wishlist->getToken(), strtotime('+1 year'));
9999
$response = new RedirectResponse($this->urlGenerator->generate('bitbag_sylius_wishlist_plugin_shop_wishlist_list_products'));
100100
$response->headers->setCookie($cookie);
101101

src/Entity/Wishlist.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Common\Collections\Collection;
1717
use Sylius\Component\Core\Model\ProductInterface;
1818
use Sylius\Component\Core\Model\ShopUserInterface;
19+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1920

2021
class Wishlist implements WishlistInterface
2122
{
@@ -28,9 +29,13 @@ class Wishlist implements WishlistInterface
2829
/** @var ShopUserInterface|null */
2930
protected $shopUser;
3031

32+
/** @var TokenInterface|null */
33+
protected $token;
34+
3135
public function __construct()
3236
{
3337
$this->wishlistProducts = new ArrayCollection();
38+
$this->token = new WishlistToken();
3439
}
3540

3641
public function getId(): ?int
@@ -87,4 +92,14 @@ public function setShopUser(ShopUserInterface $shopUser): void
8792
{
8893
$this->shopUser = $shopUser;
8994
}
95+
96+
public function getToken(): string
97+
{
98+
return (string) $this->token;
99+
}
100+
101+
public function setToken(string $token): void
102+
{
103+
$this->token = new WishlistToken($token);
104+
}
90105
}

src/Entity/WishlistInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,8 @@ public function addWishlistProduct(WishlistProductInterface $wishlistProduct): v
3838
public function getShopUser(): ?ShopUserInterface;
3939

4040
public function setShopUser(ShopUserInterface $shopShopUser): void;
41+
42+
public function getToken(): string;
43+
44+
public function setToken(string $token): void;
4145
}

src/Entity/WishlistToken.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusWishlistPlugin\Entity;
6+
7+
class WishlistToken implements WishlistTokenInterface
8+
{
9+
protected $value;
10+
11+
public function __construct(?string $value = null)
12+
{
13+
if ($value === null) {
14+
$this->value = $this->generate(self::VALUE_LENGTH);
15+
} else {
16+
$this->setValue($value);
17+
}
18+
}
19+
20+
public function getValue(): string
21+
{
22+
return $this->value;
23+
}
24+
25+
public function setValue(string $value): void
26+
{
27+
$this->value = $value;
28+
}
29+
30+
public function __toString()
31+
{
32+
return $this->getValue();
33+
}
34+
35+
private function generate($length): string
36+
{
37+
$token = '';
38+
$codeAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
39+
$codeAlphabet .= 'abcdefghijklmnopqrstuvwxyz';
40+
$codeAlphabet .= '0123456789';
41+
$max = strlen($codeAlphabet); // edited
42+
43+
for ($i = 0; $i < $length; ++$i) {
44+
$token .= $codeAlphabet[random_int(0, $max - 1)];
45+
}
46+
47+
return $token;
48+
}
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace BitBag\SyliusWishlistPlugin\Entity;
6+
7+
interface WishlistTokenInterface
8+
{
9+
const VALUE_LENGTH = 50;
10+
11+
public function getValue(): string;
12+
13+
public function setValue(string $value): void;
14+
}

src/EventListener/MergeUserWishlistItemsListener.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ final class MergeUserWishlistItemsListener
3232
private $wishlistManager;
3333

3434
/** @var string */
35-
private $wishlistCookieId;
35+
private $wishlistCookieToken;
3636

3737
public function __construct(
3838
WishlistRepositoryInterface $wishlistRepository,
3939
WishlistFactoryInterface $wishlistFactory,
4040
EntityManagerInterface $wishlistManager,
41-
string $wishlistCookieId
41+
string $wishlistCookieToken
4242
) {
4343
$this->wishlistRepository = $wishlistRepository;
4444
$this->wishlistFactory = $wishlistFactory;
4545
$this->wishlistManager = $wishlistManager;
46-
$this->wishlistCookieId = $wishlistCookieId;
46+
$this->wishlistCookieToken = $wishlistCookieToken;
4747
}
4848

4949
public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent): void
@@ -58,9 +58,9 @@ public function onInteractiveLogin(InteractiveLoginEvent $interactiveLoginEvent)
5858

5959
private function resolveWishlist(Request $request, ShopUserInterface $shopUser): void
6060
{
61-
$cookieWishlistId = $request->cookies->get($this->wishlistCookieId);
61+
$cookieWishlistToken = $request->cookies->get($this->wishlistCookieToken);
6262
/** @var WishlistInterface|null $cookieWishlist */
63-
$cookieWishlist = $this->wishlistRepository->find($cookieWishlistId);
63+
$cookieWishlist = $this->wishlistRepository->findByToken($cookieWishlistToken);
6464
$userWishlist = $this->wishlistRepository->findByShopUser($shopUser);
6565

6666
if (null === $cookieWishlist) {

0 commit comments

Comments
 (0)