Skip to content

Commit

Permalink
Fixed the 'first wishlist redirect action' to only redirect if a wish…
Browse files Browse the repository at this point in the history
…list exists
  • Loading branch information
loevgaard committed Dec 3, 2024
1 parent 21e5420 commit 1007b8c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
36 changes: 22 additions & 14 deletions src/Controller/FirstWishlistRedirectAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
}
2 changes: 0 additions & 2 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

<service id="Setono\SyliusWishlistPlugin\Controller\FirstWishlistRedirectAction" public="true">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
<argument type="service" id="Setono\SyliusWishlistPlugin\Factory\WishlistFactoryInterface"/>
<argument type="service" id="doctrine"/>
<argument type="service" id="router"/>
</service>

Expand Down
2 changes: 2 additions & 0 deletions src/Resources/translations/flashes.da.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
setono_sylius_wishlist:
no_wishlists: Du har ikke tilføjet noget til din ønskeliste endnu
2 changes: 2 additions & 0 deletions src/Resources/translations/flashes.en.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
setono_sylius_wishlist:
no_wishlists: You haven't added anything to your wishlist yet
2 changes: 2 additions & 0 deletions src/Resources/translations/flashes.no.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
setono_sylius_wishlist:
no_wishlists: Du har ikke lagt til noe på ønskelisten din ennå

0 comments on commit 1007b8c

Please sign in to comment.