generated from Setono/SyliusPluginSkeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add action that will redirect the user to the first available wishlist
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()], | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters