diff --git a/src/Controller/FirstWishlistRedirectAction.php b/src/Controller/FirstWishlistRedirectAction.php index 55fd197..35a1462 100644 --- a/src/Controller/FirstWishlistRedirectAction.php +++ b/src/Controller/FirstWishlistRedirectAction.php @@ -4,42 +4,50 @@ namespace Setono\SyliusWishlistPlugin\Controller; -use Doctrine\Persistence\ManagerRegistry; -use Setono\Doctrine\ORMTrait; -use Setono\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface; use Symfony\Component\HttpFoundation\RedirectResponse; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; /** * This class is used to redirect the user to the first wishlist. * This is useful if your application only needs one wishlist per user. - * - * NOTICE that it will persist the wishlist if it doesn't exist. */ final class FirstWishlistRedirectAction { - use ORMTrait; public function __construct( private readonly WishlistProviderInterface $wishlistProvider, - private readonly WishlistFactoryInterface $wishlistFactory, - ManagerRegistry $managerRegistry, private readonly UrlGeneratorInterface $urlGenerator, ) { - $this->managerRegistry = $managerRegistry; } - public function __invoke(): RedirectResponse + public function __invoke(Request $request): RedirectResponse { - $wishlist = $this->wishlistProvider->getWishlists()[0]; + $wishlists = $this->wishlistProvider->getWishlists(); + if ([] === $wishlists) { + $session = $request->getSession(); + if ($session instanceof FlashBagAwareSessionInterface) { + $session->getFlashBag()->add('info', 'setono_sylius_wishlist.no_wishlists'); + } - $this->getManager($wishlist)->persist($wishlist); - $this->getManager($wishlist)->flush(); + return $this->redirect($request); + } return new RedirectResponse($this->urlGenerator->generate( 'setono_sylius_wishlist_shop_wishlist_show', - ['uuid' => $wishlist->getUuid()], + ['uuid' => $wishlists[0]->getUuid()], )); } + + private function redirect(Request $request): RedirectResponse + { + $referrer = $request->headers->get('referer'); + if (is_string($referrer) && '' !== $referrer) { + return new RedirectResponse($referrer); + } + + return new RedirectResponse($this->urlGenerator->generate('sylius_shop_homepage')); + } } diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index 86c488e..e3ebddd 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -25,8 +25,6 @@ - - diff --git a/src/Resources/translations/flashes.da.yaml b/src/Resources/translations/flashes.da.yaml new file mode 100644 index 0000000..df53c50 --- /dev/null +++ b/src/Resources/translations/flashes.da.yaml @@ -0,0 +1,2 @@ +setono_sylius_wishlist: + no_wishlists: Du har ikke tilføjet noget til din ønskeliste endnu diff --git a/src/Resources/translations/flashes.en.yaml b/src/Resources/translations/flashes.en.yaml new file mode 100644 index 0000000..8351bf2 --- /dev/null +++ b/src/Resources/translations/flashes.en.yaml @@ -0,0 +1,2 @@ +setono_sylius_wishlist: + no_wishlists: You haven't added anything to your wishlist yet diff --git a/src/Resources/translations/flashes.no.yaml b/src/Resources/translations/flashes.no.yaml new file mode 100644 index 0000000..8c4895d --- /dev/null +++ b/src/Resources/translations/flashes.no.yaml @@ -0,0 +1,2 @@ +setono_sylius_wishlist: + no_wishlists: Du har ikke lagt til noe på ønskelisten din ennå