Skip to content

Commit

Permalink
Add action that will redirect the user to the first available wishlist
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Nov 19, 2024
1 parent c95466a commit 8288f93
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Controller/FirstWishlistRedirectAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

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\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
{
$wishlist = $this->wishlistProvider->getWishlists()[0];

$this->getManager($wishlist)->persist($wishlist);
$this->getManager($wishlist)->flush();

return new RedirectResponse($this->urlGenerator->generate(
'setono_sylius_wishlist_shop_wishlist_show',
['uuid' => $wishlist->getUuid()],
));
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/routes/shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ setono_sylius_wishlist_shop_wishlist_index:
methods: [GET]
path: /wishlists
controller: Setono\SyliusWishlistPlugin\Controller\WishlistIndexAction

setono_sylius_wishlist_shop_wishlist_first_redirect:
methods: [GET]
path: /wishlists/redirect-to-first
controller: Setono\SyliusWishlistPlugin\Controller\FirstWishlistRedirectAction

setono_sylius_wishlist_shop_wishlist_show:
methods: [GET,PATCH]
Expand Down
7 changes: 7 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
<argument type="service" id="twig"/>
</service>

<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>

<service id="Setono\SyliusWishlistPlugin\Controller\RemoveWishlistItemAction" public="true">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
<argument type="service" id="router"/>
Expand Down

0 comments on commit 8288f93

Please sign in to comment.