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.
Convert guest wishlist to user wishlist upon login
- Loading branch information
Showing
10 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/EventSubscriber/ConvertGuestWishlistToUserWishlistSubscriber.php
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,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusWishlistPlugin\EventSubscriber; | ||
|
||
use Doctrine\Persistence\ManagerRegistry; | ||
use Setono\Doctrine\ORMTrait; | ||
use Setono\SyliusWishlistPlugin\Model\GuestWishlistInterface; | ||
use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface; | ||
use Sylius\Component\User\Model\UserInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Http\Event\LoginSuccessEvent; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* When the user logs in, we want to convert the guest wishlist to a user wishlist | ||
*/ | ||
final class ConvertGuestWishlistToUserWishlistSubscriber implements EventSubscriberInterface | ||
{ | ||
use ORMTrait; | ||
|
||
public function __construct( | ||
private readonly WishlistProviderInterface $guestWishlistProvider, | ||
ManagerRegistry $managerRegistry, | ||
) { | ||
$this->managerRegistry = $managerRegistry; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
LoginSuccessEvent::class => 'onLoginSuccess', | ||
]; | ||
} | ||
|
||
public function onLoginSuccess(LoginSuccessEvent $event): void | ||
{ | ||
$user = $event->getUser(); | ||
Assert::isInstanceOf($user, UserInterface::class); | ||
|
||
foreach ($this->guestWishlistProvider->getWishlists() as $guestWishlist) { | ||
if (!$guestWishlist instanceof GuestWishlistInterface || !$guestWishlist->hasItems()) { | ||
continue; | ||
} | ||
|
||
$guestWishlist->convertToUserWishlist($user); | ||
|
||
$this->getManager($guestWishlist)->flush(); | ||
} | ||
} | ||
} |
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
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
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
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
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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<service id="Setono\SyliusWishlistPlugin\EventSubscriber\ConvertGuestWishlistToUserWishlistSubscriber"> | ||
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/> | ||
<argument type="service" id="doctrine"/> | ||
|
||
<tag name="kernel.event_subscriber"/> | ||
</service> | ||
</services> | ||
</container> |