From 98c3e0804b9bc0d255bfc1f7b4a50dedec7cbdd2 Mon Sep 17 00:00:00 2001 From: token Date: Wed, 23 Aug 2023 10:20:25 +0200 Subject: [PATCH 1/4] Fix compatibility for Symfony 5.x --- src/CommandHandler/Wishlist/CreateNewWishlistHandler.php | 5 ++++- src/CommandHandler/Wishlist/CreateWishlistHandler.php | 2 +- src/Context/WishlistContext.php | 2 +- src/Controller/Action/AddWishlistToUserAction.php | 5 ++++- src/Controller/Action/ShowChosenWishlistAction.php | 5 ++++- src/EventSubscriber/CreateNewWishlistSubscriber.php | 4 +++- src/Resolver/WishlistsResolver.php | 5 ++++- 7 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php b/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php index 6f3501b5..01c0c884 100644 --- a/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php +++ b/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php @@ -53,7 +53,10 @@ public function __construct( public function __invoke(CreateNewWishlist $createNewWishlist): void { - $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + $token = $this->tokenStorage->getToken(); + + $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); if ($user instanceof ShopUserInterface) { diff --git a/src/CommandHandler/Wishlist/CreateWishlistHandler.php b/src/CommandHandler/Wishlist/CreateWishlistHandler.php index ff4fd82f..008492fe 100644 --- a/src/CommandHandler/Wishlist/CreateWishlistHandler.php +++ b/src/CommandHandler/Wishlist/CreateWishlistHandler.php @@ -52,7 +52,7 @@ public function __invoke(CreateWishlist $createWishlist): WishlistInterface /** @var ?TokenInterface $token */ $token = $this->tokenStorage->getToken(); - $user = null !== $token ? $token->getUser() : null; + $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); /** @var WishlistInterface $wishlist */ $wishlist = $this->wishlistFactory->createNew(); diff --git a/src/Context/WishlistContext.php b/src/Context/WishlistContext.php index 90b2890f..79c02f6c 100644 --- a/src/Context/WishlistContext.php +++ b/src/Context/WishlistContext.php @@ -57,7 +57,7 @@ public function getWishlist(Request $request): WishlistInterface /** @var WishlistInterface $wishlist */ $wishlist = $this->wishlistFactory->createNew(); - $user = null !== $token ? $token->getUser() : null; + $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); if (null === $cookieWishlistToken && null === $user) { return $wishlist; diff --git a/src/Controller/Action/AddWishlistToUserAction.php b/src/Controller/Action/AddWishlistToUserAction.php index 732564ee..90a9e89b 100644 --- a/src/Controller/Action/AddWishlistToUserAction.php +++ b/src/Controller/Action/AddWishlistToUserAction.php @@ -56,8 +56,11 @@ public function __construct( public function __invoke(Request $request): Response { + $token = $this->tokenStorage->getToken(); + /** @var ShopUserInterface $shopUser */ - $shopUser = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + $shopUser = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $wishlistId = $request->attributes->getInt('id'); $wishlist = $this->wishlistRepository->find($wishlistId); diff --git a/src/Controller/Action/ShowChosenWishlistAction.php b/src/Controller/Action/ShowChosenWishlistAction.php index 96750bae..7b7d06ef 100644 --- a/src/Controller/Action/ShowChosenWishlistAction.php +++ b/src/Controller/Action/ShowChosenWishlistAction.php @@ -66,10 +66,13 @@ public function __construct( public function __invoke(string $wishlistId, Request $request): Response { + $token = $this->tokenStorage->getToken(); + /** @var WishlistInterface $wishlist */ $wishlist = $this->wishlistRepository->find((int)$wishlistId); $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); - $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); if ($wishlist instanceof WishlistInterface && $user instanceof ShopUserInterface || $wishlist instanceof WishlistInterface && $wishlist->getToken() === $wishlistCookieToken diff --git a/src/EventSubscriber/CreateNewWishlistSubscriber.php b/src/EventSubscriber/CreateNewWishlistSubscriber.php index 7c7d0974..e15e7eee 100644 --- a/src/EventSubscriber/CreateNewWishlistSubscriber.php +++ b/src/EventSubscriber/CreateNewWishlistSubscriber.php @@ -120,7 +120,9 @@ public function onKernelResponse(ResponseEvent $event): void private function createNewWishlist(?string $wishlistCookieToken): WishlistInterface { - $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + $token = $this->tokenStorage->getToken(); + + $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); $wishlist = $this->wishlistFactory->createNew(); diff --git a/src/Resolver/WishlistsResolver.php b/src/Resolver/WishlistsResolver.php index a544f19f..22fb68e1 100644 --- a/src/Resolver/WishlistsResolver.php +++ b/src/Resolver/WishlistsResolver.php @@ -51,7 +51,10 @@ public function __construct( public function resolve(): array { - $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + $token = $this->tokenStorage->getToken(); + + $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); try { From 99f8a7ec227fd2f0d707d658538231112c35332f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Kukli=C5=84ski?= Date: Sat, 9 Sep 2023 19:54:01 +0200 Subject: [PATCH 2/4] Putting all token checking conditions to TokenUserResolver class --- spec/Context/WishlistContextSpec.php | 31 ++++++++++------ spec/Resolver/TokenUserResolverSpec.php | 37 +++++++++++++++++++ .../Wishlist/CreateNewWishlistHandler.php | 10 +++-- src/Context/WishlistContext.php | 9 ++++- .../Action/AddWishlistToUserAction.php | 9 ++++- .../Action/ShowChosenWishlistAction.php | 9 ++++- .../CreateNewWishlistSubscriber.php | 10 +++-- src/Resolver/TokenUserResolver.php | 25 +++++++++++++ src/Resolver/TokenUserResolverInterface.php | 13 +++++++ src/Resolver/WishlistsResolver.php | 9 +++-- src/Resources/config/services.yml | 4 ++ .../config/services/message_handler.yml | 1 + src/Resources/config/services/resolver.yml | 4 ++ 13 files changed, 145 insertions(+), 26 deletions(-) create mode 100644 spec/Resolver/TokenUserResolverSpec.php create mode 100644 src/Resolver/TokenUserResolver.php create mode 100644 src/Resolver/TokenUserResolverInterface.php diff --git a/spec/Context/WishlistContextSpec.php b/spec/Context/WishlistContextSpec.php index efb0785a..81b76cf2 100644 --- a/spec/Context/WishlistContextSpec.php +++ b/spec/Context/WishlistContextSpec.php @@ -15,6 +15,7 @@ use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use PhpSpec\ObjectBehavior; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Core\Model\ChannelInterface; @@ -30,7 +31,8 @@ function let( TokenStorageInterface $tokenStorage, WishlistRepositoryInterface $wishlistRepository, WishlistFactoryInterface $wishlistFactory, - ChannelContextInterface $channelContext + ChannelContextInterface $channelContext, + TokenUserResolverInterface $tokenUserResolver, ) { $this->beConstructedWith( $tokenStorage, @@ -38,6 +40,7 @@ function let( $wishlistFactory, 'bitbag_sylius_wishlist', $channelContext, + $tokenUserResolver, ); } @@ -57,12 +60,14 @@ function it_creates_new_wishlist_if_no_cookie_and_user( TokenStorageInterface $tokenStorage, TokenInterface $token, WishlistFactoryInterface $wishlistFactory, - WishlistInterface $wishlist + WishlistInterface $wishlist, + TokenUserResolverInterface $tokenUserResolver, ): void { $request->cookies = $parameterBag; $parameterBag->get('bitbag_sylius_wishlist')->willReturn(null); $tokenStorage->getToken()->willReturn($token); - $token->getUser()->willReturn(null); + $tokenUserResolver->resolve($token)->willReturn(null); + $wishlistFactory->createNew()->willReturn($wishlist); $this->getWishlist($request)->shouldReturn($wishlist); @@ -74,12 +79,13 @@ function it_returns_cookie_wishlist_if_cookie_and_no_user( TokenStorageInterface $tokenStorage, TokenInterface $token, WishlistRepositoryInterface $wishlistRepository, - WishlistInterface $wishlist + WishlistInterface $wishlist, + TokenUserResolverInterface $tokenUserResolver, ): void { $request->cookies = $parameterBag; $parameterBag->get('bitbag_sylius_wishlist')->willReturn('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId'); $tokenStorage->getToken()->willReturn($token); - $token->getUser()->willReturn(null); + $tokenUserResolver->resolve($token)->willReturn(null); $wishlistRepository->findByToken('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId')->willReturn($wishlist); $this->getWishlist($request)->shouldReturn($wishlist); @@ -92,12 +98,13 @@ function it_returns_new_wishlist_if_cookie_not_found_and_no_user( TokenInterface $token, WishlistRepositoryInterface $wishlistRepository, WishlistFactoryInterface $wishlistFactory, - WishlistInterface $wishlist + WishlistInterface $wishlist, + TokenUserResolverInterface $tokenUserResolver, ): void { $request->cookies = $parameterBag; $parameterBag->get('bitbag_sylius_wishlist')->willReturn('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId'); $tokenStorage->getToken()->willReturn($token); - $token->getUser()->willReturn(null); + $tokenUserResolver->resolve($token)->willReturn(null); $wishlistRepository->findByToken('Fq8N4W6mk12i9J2HX0U60POGG5UEzSgGW37OWd6sv2dd8FlBId')->willReturn(null); $wishlistFactory->createNew()->willReturn($wishlist); @@ -113,12 +120,13 @@ function it_returns_user_wishlist_if_found_and_user_logged_in( WishlistRepositoryInterface $wishlistRepository, WishlistInterface $wishlist, ChannelContextInterface $channelContext, - ChannelInterface $channel + ChannelInterface $channel, + TokenUserResolverInterface $tokenUserResolver, ): void { $request->cookies = $parameterBag; $parameterBag->get('bitbag_sylius_wishlist')->willReturn(null); $tokenStorage->getToken()->willReturn($token); - $token->getUser()->willReturn($shopUser); + $tokenUserResolver->resolve($token)->willReturn($shopUser); $channelContext->getChannel()->willReturn($channel); $wishlistRepository->findOneByShopUserAndChannel($shopUser,$channel)->willReturn($wishlist); @@ -136,13 +144,14 @@ function it_returns_new_wishlist_if_not_found_and_user_logged_in( WishlistFactoryInterface $wishlistFactory, WishlistInterface $wishlist, ChannelContextInterface $channelContext, - ChannelInterface $channel + ChannelInterface $channel, + TokenUserResolverInterface $tokenUserResolver, ): void { $request->cookies = $parameterBag; $parameterBag->get('bitbag_sylius_wishlist')->willReturn(null); $wishlistFactory->createNew()->willReturn($wishlist); $tokenStorage->getToken()->willReturn($token); - $token->getUser()->willReturn($shopUser); + $tokenUserResolver->resolve($token)->willReturn($shopUser); $channelContext->getChannel()->willReturn($channel); $wishlistRepository->findOneByShopUserAndChannel($shopUser,$channel)->willReturn(null); $wishlistFactory->createForUserAndChannel($shopUser,$channel)->willReturn($wishlist); diff --git a/spec/Resolver/TokenUserResolverSpec.php b/spec/Resolver/TokenUserResolverSpec.php new file mode 100644 index 00000000..f820a33f --- /dev/null +++ b/spec/Resolver/TokenUserResolverSpec.php @@ -0,0 +1,37 @@ +shouldHaveType(TokenUserResolver::class); + $this->shouldImplement(TokenUserResolverInterface::class); + } + + public function it_returns_null_for_null_token(): void + { + $this->resolve(null) + ->shouldReturn(null); + } + + public function it_returns_user_for_non_anonymous_token( + TokenInterface $token, + UserInterface $user, + ): void { + $token->getUser()->willReturn($user); + + $this->resolve($token) + ->shouldReturn($user); + } +} diff --git a/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php b/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php index 01c0c884..6b225251 100644 --- a/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php +++ b/src/CommandHandler/Wishlist/CreateNewWishlistHandler.php @@ -15,6 +15,7 @@ use BitBag\SyliusWishlistPlugin\Exception\WishlistNameIsTakenException; use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ShopUserInterface; @@ -35,13 +36,16 @@ final class CreateNewWishlistHandler implements MessageHandlerInterface private WishlistNameCheckerInterface $wishlistNameChecker; + private TokenUserResolverInterface $tokenUserResolver; + public function __construct( WishlistRepositoryInterface $wishlistRepository, TokenStorageInterface $tokenStorage, WishlistFactoryInterface $wishlistFactory, WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver, ChannelRepositoryInterface $channelRepository, - WishlistNameCheckerInterface $wishlistNameChecker + WishlistNameCheckerInterface $wishlistNameChecker, + TokenUserResolverInterface $tokenUserResolver, ) { $this->wishlistRepository = $wishlistRepository; $this->tokenStorage = $tokenStorage; @@ -49,13 +53,13 @@ public function __construct( $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver; $this->channelRepository = $channelRepository; $this->wishlistNameChecker = $wishlistNameChecker; + $this->tokenUserResolver = $tokenUserResolver; } public function __invoke(CreateNewWishlist $createNewWishlist): void { $token = $this->tokenStorage->getToken(); - - $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $user = $this->tokenUserResolver->resolve($token); $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); diff --git a/src/Context/WishlistContext.php b/src/Context/WishlistContext.php index 79c02f6c..e7c1b921 100644 --- a/src/Context/WishlistContext.php +++ b/src/Context/WishlistContext.php @@ -13,6 +13,7 @@ use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Channel\Context\ChannelNotFoundException; use Sylius\Component\Core\Model\ShopUserInterface; @@ -32,18 +33,22 @@ final class WishlistContext implements WishlistContextInterface private ChannelContextInterface $channelContext; + private TokenUserResolverInterface $tokenUserResolver; + public function __construct( TokenStorageInterface $tokenStorage, WishlistRepositoryInterface $wishlistRepository, WishlistFactoryInterface $wishlistFactory, string $wishlistCookieToken, - ChannelContextInterface $channelContext + ChannelContextInterface $channelContext, + TokenUserResolverInterface $tokenUserResolver, ) { $this->tokenStorage = $tokenStorage; $this->wishlistRepository = $wishlistRepository; $this->wishlistFactory = $wishlistFactory; $this->wishlistCookieToken = $wishlistCookieToken; $this->channelContext = $channelContext; + $this->tokenUserResolver = $tokenUserResolver; } public function getWishlist(Request $request): WishlistInterface @@ -57,7 +62,7 @@ public function getWishlist(Request $request): WishlistInterface /** @var WishlistInterface $wishlist */ $wishlist = $this->wishlistFactory->createNew(); - $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $user = $this->tokenUserResolver->resolve($token); if (null === $cookieWishlistToken && null === $user) { return $wishlist; diff --git a/src/Controller/Action/AddWishlistToUserAction.php b/src/Controller/Action/AddWishlistToUserAction.php index 90a9e89b..86c7fc1d 100644 --- a/src/Controller/Action/AddWishlistToUserAction.php +++ b/src/Controller/Action/AddWishlistToUserAction.php @@ -12,6 +12,7 @@ use BitBag\SyliusWishlistPlugin\Command\Wishlist\AddWishlistToUser; use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use Sylius\Component\Core\Model\ShopUserInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; @@ -38,13 +39,16 @@ final class AddWishlistToUserAction private TokenStorageInterface $tokenStorage; + private TokenUserResolverInterface $tokenUserResolver; + public function __construct( MessageBusInterface $commandBus, RequestStack $requestStack, TranslatorInterface $translator, WishlistRepositoryInterface $wishlistRepository, UrlGeneratorInterface $urlGenerator, - TokenStorageInterface $tokenStorage + TokenStorageInterface $tokenStorage, + TokenUserResolverInterface $tokenUserResolver, ) { $this->commandBus = $commandBus; $this->requestStack = $requestStack; @@ -52,6 +56,7 @@ public function __construct( $this->wishlistRepository = $wishlistRepository; $this->urlGenerator = $urlGenerator; $this->tokenStorage = $tokenStorage; + $this->tokenUserResolver = $tokenUserResolver; } public function __invoke(Request $request): Response @@ -59,7 +64,7 @@ public function __invoke(Request $request): Response $token = $this->tokenStorage->getToken(); /** @var ShopUserInterface $shopUser */ - $shopUser = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $shopUser = $this->tokenUserResolver->resolve($token); $wishlistId = $request->attributes->getInt('id'); $wishlist = $this->wishlistRepository->find($wishlistId); diff --git a/src/Controller/Action/ShowChosenWishlistAction.php b/src/Controller/Action/ShowChosenWishlistAction.php index 7b7d06ef..918f0095 100644 --- a/src/Controller/Action/ShowChosenWishlistAction.php +++ b/src/Controller/Action/ShowChosenWishlistAction.php @@ -14,6 +14,7 @@ use BitBag\SyliusWishlistPlugin\Form\Type\WishlistCollectionType; use BitBag\SyliusWishlistPlugin\Processor\WishlistCommandProcessorInterface; use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface; use Sylius\Component\Core\Model\ShopUserInterface; use Sylius\Component\Order\Context\CartContextInterface; @@ -44,6 +45,8 @@ final class ShowChosenWishlistAction private TokenStorageInterface $tokenStorage; + private TokenUserResolverInterface $tokenUserResolver; + public function __construct( WishlistRepositoryInterface $wishlistRepository, CartContextInterface $cartContext, @@ -52,7 +55,8 @@ public function __construct( WishlistCommandProcessorInterface $wishlistCommandProcessor, UrlGeneratorInterface $urlGenerator, WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver, - TokenStorageInterface $tokenStorage + TokenStorageInterface $tokenStorage, + TokenUserResolverInterface $tokenUserResolver, ) { $this->wishlistRepository = $wishlistRepository; $this->cartContext = $cartContext; @@ -62,6 +66,7 @@ public function __construct( $this->urlGenerator = $urlGenerator; $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver; $this->tokenStorage = $tokenStorage; + $this->tokenUserResolver = $tokenUserResolver; } public function __invoke(string $wishlistId, Request $request): Response @@ -72,7 +77,7 @@ public function __invoke(string $wishlistId, Request $request): Response $wishlist = $this->wishlistRepository->find((int)$wishlistId); $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); - $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $user = $this->tokenUserResolver->resolve($token); if ($wishlist instanceof WishlistInterface && $user instanceof ShopUserInterface || $wishlist instanceof WishlistInterface && $wishlist->getToken() === $wishlistCookieToken diff --git a/src/EventSubscriber/CreateNewWishlistSubscriber.php b/src/EventSubscriber/CreateNewWishlistSubscriber.php index e15e7eee..2e26ac17 100644 --- a/src/EventSubscriber/CreateNewWishlistSubscriber.php +++ b/src/EventSubscriber/CreateNewWishlistSubscriber.php @@ -14,6 +14,7 @@ use BitBag\SyliusWishlistPlugin\Entity\WishlistToken; use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface; use BitBag\SyliusWishlistPlugin\Resolver\WishlistsResolverInterface; use Sylius\Component\Channel\Context\ChannelContextInterface; @@ -44,6 +45,8 @@ final class CreateNewWishlistSubscriber implements EventSubscriberInterface private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver; + private TokenUserResolverInterface $tokenUserResolver; + public function __construct( string $wishlistCookieToken, WishlistsResolverInterface $wishlistsResolver, @@ -51,7 +54,8 @@ public function __construct( WishlistRepositoryInterface $wishlistRepository, TokenStorageInterface $tokenStorage, ChannelContextInterface $channelContext, - WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver + WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver, + TokenUserResolverInterface $tokenUserResolver, ) { $this->wishlistCookieToken = $wishlistCookieToken; $this->wishlistsResolver = $wishlistsResolver; @@ -60,6 +64,7 @@ public function __construct( $this->tokenStorage = $tokenStorage; $this->channelContext = $channelContext; $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver; + $this->tokenUserResolver = $tokenUserResolver; } public static function getSubscribedEvents(): array @@ -121,8 +126,7 @@ public function onKernelResponse(ResponseEvent $event): void private function createNewWishlist(?string $wishlistCookieToken): WishlistInterface { $token = $this->tokenStorage->getToken(); - - $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $user = $this->tokenUserResolver->resolve($token); $wishlist = $this->wishlistFactory->createNew(); diff --git a/src/Resolver/TokenUserResolver.php b/src/Resolver/TokenUserResolver.php new file mode 100644 index 00000000..7961a1ed --- /dev/null +++ b/src/Resolver/TokenUserResolver.php @@ -0,0 +1,25 @@ +getUser(); + if (is_string($user) && $user === 'anon.') { + return null; + } + + return $user; + } +} diff --git a/src/Resolver/TokenUserResolverInterface.php b/src/Resolver/TokenUserResolverInterface.php new file mode 100644 index 00000000..d505a623 --- /dev/null +++ b/src/Resolver/TokenUserResolverInterface.php @@ -0,0 +1,13 @@ +wishlistRepository = $wishlistRepository; $this->tokenStorage = $tokenStorage; $this->wishlistCookieTokenResolver = $wishlistCookieTokenResolver; $this->channelContext = $channelContext; $this->messageBus = $messageBus; + $this->tokenUserResolver = $tokenUserResolver; } public function resolve(): array { $token = $this->tokenStorage->getToken(); - - $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $user = $this->tokenUserResolver->resolve($token); $wishlistCookieToken = $this->wishlistCookieTokenResolver->resolve(); diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index d1bb85f1..5ef8199c 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -166,6 +166,7 @@ services: - "@bitbag_sylius_wishlist_plugin.factory.wishlist" - "%bitbag_sylius_wishlist_plugin.parameters.wishlist_cookie_token%" - "@sylius.context.channel" + - "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver" bitbag_sylius_wishlist_plugin.custom_factory.wishlist: class: BitBag\SyliusWishlistPlugin\Factory\WishlistFactory @@ -205,6 +206,7 @@ services: - "@security.token_storage" - "@sylius.context.channel" - '@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver' + - '@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver' tags: - { name: kernel.event_subscriber, event: kernel.exception } @@ -301,6 +303,7 @@ services: - "@router.default" - "@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver" - "@security.token_storage" + - "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver" tags: - { name: controller.service_arguments } @@ -374,5 +377,6 @@ services: - "@bitbag_sylius_wishlist_plugin.repository.wishlist" - "@router" - "@security.token_storage" + - "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver" tags: - { name: controller.service_arguments } diff --git a/src/Resources/config/services/message_handler.yml b/src/Resources/config/services/message_handler.yml index de74d7df..3fd05ecd 100644 --- a/src/Resources/config/services/message_handler.yml +++ b/src/Resources/config/services/message_handler.yml @@ -131,6 +131,7 @@ services: - "@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver" - "@sylius.repository.channel" - "@bitbag_sylius_wishlist_plugin.checker.wishlist_name_checker" + - "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver" tags: - { name: messenger.message_handler } diff --git a/src/Resources/config/services/resolver.yml b/src/Resources/config/services/resolver.yml index 7c706f9b..713faf07 100644 --- a/src/Resources/config/services/resolver.yml +++ b/src/Resources/config/services/resolver.yml @@ -14,6 +14,7 @@ services: - "@bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver" - "@sylius.context.channel" - "@sylius.command_bus" + - "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver" bitbag_sylius_wishlist_plugin.resolver.wishlist_cookie_token_resolver: class: BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolver @@ -23,3 +24,6 @@ services: - "@bitbag_sylius_wishlist_plugin.repository.wishlist" - "@security.token_storage" - "@sylius.context.channel" + + bitbag_sylius_wishlist_plugin.resolver.token_user_resolver: + class: BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolver From 769705f7b8000ed6b31b37f646b8b58568e74232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Kukli=C5=84ski?= Date: Sat, 9 Sep 2023 20:10:06 +0200 Subject: [PATCH 3/4] Add BitBag OS header to newly created classes --- spec/Resolver/TokenUserResolverSpec.php | 6 ++++++ src/Resolver/TokenUserResolver.php | 6 ++++++ src/Resolver/TokenUserResolverInterface.php | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/spec/Resolver/TokenUserResolverSpec.php b/spec/Resolver/TokenUserResolverSpec.php index f820a33f..df30c18a 100644 --- a/spec/Resolver/TokenUserResolverSpec.php +++ b/spec/Resolver/TokenUserResolverSpec.php @@ -1,5 +1,11 @@ Date: Sat, 9 Sep 2023 20:19:40 +0200 Subject: [PATCH 4/4] Add and use TokenUserResolver dependency in missed CreateWishlistHandler class --- src/CommandHandler/Wishlist/CreateWishlistHandler.php | 10 +++++++--- src/Resources/config/services/message_handler.yml | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/CommandHandler/Wishlist/CreateWishlistHandler.php b/src/CommandHandler/Wishlist/CreateWishlistHandler.php index 008492fe..1e29cb5f 100644 --- a/src/CommandHandler/Wishlist/CreateWishlistHandler.php +++ b/src/CommandHandler/Wishlist/CreateWishlistHandler.php @@ -14,6 +14,7 @@ use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface; use BitBag\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use BitBag\SyliusWishlistPlugin\Resolver\ShopUserWishlistResolverInterface; +use BitBag\SyliusWishlistPlugin\Resolver\TokenUserResolverInterface; use Doctrine\Persistence\ObjectManager; use Sylius\Component\Channel\Repository\ChannelRepositoryInterface; use Sylius\Component\Core\Model\ShopUserInterface; @@ -33,26 +34,29 @@ final class CreateWishlistHandler implements MessageHandlerInterface private ChannelRepositoryInterface $channelRepository; + private TokenUserResolverInterface $tokenUserResolver; + public function __construct( TokenStorageInterface $tokenStorage, WishlistFactoryInterface $wishlistFactory, ShopUserWishlistResolverInterface $shopUserWishlistResolver, ObjectManager $wishlistManager, - ChannelRepositoryInterface $channelRepository + ChannelRepositoryInterface $channelRepository, + TokenUserResolverInterface $tokenUserResolver, ) { $this->tokenStorage = $tokenStorage; $this->wishlistFactory = $wishlistFactory; $this->shopUserWishlistResolver = $shopUserWishlistResolver; $this->wishlistManager = $wishlistManager; $this->channelRepository = $channelRepository; + $this->tokenUserResolver = $tokenUserResolver; } public function __invoke(CreateWishlist $createWishlist): WishlistInterface { /** @var ?TokenInterface $token */ $token = $this->tokenStorage->getToken(); - - $user = (null === $token || 'anon.' === $token->getUser()) ? null : $token->getUser(); + $user = $this->tokenUserResolver->resolve($token); /** @var WishlistInterface $wishlist */ $wishlist = $this->wishlistFactory->createNew(); diff --git a/src/Resources/config/services/message_handler.yml b/src/Resources/config/services/message_handler.yml index 3fd05ecd..b10b55ce 100644 --- a/src/Resources/config/services/message_handler.yml +++ b/src/Resources/config/services/message_handler.yml @@ -56,6 +56,7 @@ services: - "@bitbag_sylius_wishlist_plugin.resolver.shop_user_wishlist_resolver" - "@bitbag_sylius_wishlist_plugin.manager.wishlist" - "@sylius.repository.channel" + - "@bitbag_sylius_wishlist_plugin.resolver.token_user_resolver" tags: - { name: messenger.message_handler }