Skip to content

Commit

Permalink
Convert guest wishlist to user wishlist upon login
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Nov 5, 2024
1 parent a6c8228 commit 68616ac
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 5 deletions.
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();
}
}
}
7 changes: 7 additions & 0 deletions src/Model/GuestWishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Setono\SyliusWishlistPlugin\Model;

use Sylius\Component\User\Model\UserInterface;

class GuestWishlist extends Wishlist implements GuestWishlistInterface
{
protected ?string $clientId = null;
Expand All @@ -17,4 +19,9 @@ public function setClientId(?string $clientId): void
{
$this->clientId = $clientId;
}

public function convertToUserWishlist(UserInterface $user): void
{
$this->user = $user;
}
}
4 changes: 4 additions & 0 deletions src/Model/GuestWishlistInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

namespace Setono\SyliusWishlistPlugin\Model;

use Sylius\Component\User\Model\UserInterface;

interface GuestWishlistInterface extends WishlistInterface
{
public function getClientId(): ?string;

public function setClientId(?string $clientId): void;

public function convertToUserWishlist(UserInterface $user): void;
}
2 changes: 0 additions & 2 deletions src/Model/UserWishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

class UserWishlist extends Wishlist implements UserWishlistInterface
{
protected ?UserInterface $user = null;

public function getUser(): ?UserInterface
{
return $this->user;
Expand Down
8 changes: 8 additions & 0 deletions src/Model/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;
use Sylius\Component\User\Model\UserInterface;
use Sylius\Resource\Model\TimestampableTrait;
use Symfony\Component\Uid\Uuid;

Expand All @@ -21,6 +22,8 @@ abstract class Wishlist implements WishlistInterface

protected ?string $name = null;

protected ?UserInterface $user = null;

/** @var Collection<array-key, WishlistItemInterface> */
protected Collection $items;

Expand Down Expand Up @@ -50,6 +53,11 @@ public function setName(?string $name): void
$this->name = $name;
}

public function hasItems(): bool
{
return !$this->items->isEmpty();
}

public function addItem(WishlistItemInterface $item): void
{
if (!$this->hasItem($item)) {
Expand Down
2 changes: 2 additions & 0 deletions src/Model/WishlistInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public function getName(): ?string;

public function setName(?string $name): void;

public function hasItems(): bool;

public function addItem(WishlistItemInterface $item): void;

public function removeItem(WishlistItemInterface $item): void;
Expand Down
3 changes: 0 additions & 3 deletions src/Resources/config/doctrine/model/UserWishlist.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="Setono\SyliusWishlistPlugin\Model\UserWishlist">
<many-to-one field="user" target-entity="Sylius\Component\User\Model\UserInterface">
<join-column name="user_id" referenced-column-name="id" nullable="true" on-delete="CASCADE"/>
</many-to-one>
</entity>
</doctrine-mapping>
4 changes: 4 additions & 0 deletions src/Resources/config/doctrine/model/Wishlist.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
<field name="uuid" type="string" unique="true"/>
<field name="name" type="string"/>

<many-to-one field="user" target-entity="Sylius\Component\User\Model\UserInterface">
<join-column name="user_id" referenced-column-name="id" nullable="true" on-delete="CASCADE"/>
</many-to-one>

<one-to-many field="items" target-entity="Setono\SyliusWishlistPlugin\Model\WishlistItemInterface"
mapped-by="wishlist" orphan-removal="true" fetch="EXTRA_LAZY">
<cascade>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<imports>
<import resource="services/controller.xml"/>
<import resource="services/event_listener.xml"/>
<import resource="services/event_subscriber.xml"/>
<import resource="services/factory.xml"/>
<import resource="services/form.xml"/>
<import resource="services/provider.xml"/>
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/config/services/event_subscriber.xml
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>

0 comments on commit 68616ac

Please sign in to comment.