From 9ba4ea538be1d1689495acdb4d0fee87ba516edd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 26 Nov 2024 14:27:14 +0100 Subject: [PATCH] Change the behaviour of wishlist provider --- src/Controller/AddToWishlistAction.php | 8 +++-- src/Factory/WishlistFactory.php | 14 ++++++-- src/Provider/CompositeWishlistProvider.php | 9 +++-- src/Provider/GuestWishlistProvider.php | 2 +- src/Provider/NewWishlistProvider.php | 36 -------------------- src/Provider/UserWishlistProvider.php | 10 ++---- src/Provider/WishlistProviderInterface.php | 8 ++--- src/Resources/config/services/controller.xml | 1 + src/Resources/config/services/factory.xml | 2 ++ src/Resources/config/services/provider.xml | 8 ----- 10 files changed, 30 insertions(+), 68 deletions(-) delete mode 100644 src/Provider/NewWishlistProvider.php diff --git a/src/Controller/AddToWishlistAction.php b/src/Controller/AddToWishlistAction.php index 82c5070..80ef505 100644 --- a/src/Controller/AddToWishlistAction.php +++ b/src/Controller/AddToWishlistAction.php @@ -6,12 +6,12 @@ use Doctrine\Persistence\ManagerRegistry; use Setono\Doctrine\ORMTrait; +use Setono\SyliusWishlistPlugin\Factory\WishlistFactoryInterface; use Setono\SyliusWishlistPlugin\Factory\WishlistItemFactoryInterface; use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductVariantInterface; use Symfony\Component\HttpFoundation\JsonResponse; -use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Twig\Environment; @@ -27,13 +27,14 @@ public function __construct( private readonly WishlistItemFactoryInterface $wishlistItemFactory, private readonly Environment $twig, ManagerRegistry $managerRegistry, + private readonly WishlistFactoryInterface $wishlistFactory, /** @var class-string $className */ private readonly string $className, ) { $this->managerRegistry = $managerRegistry; } - public function __invoke(int $id): Response + public function __invoke(int $id): JsonResponse { $entity = $this->getManager($this->className)->find($this->className, $id); @@ -44,6 +45,9 @@ public function __invoke(int $id): Response $wishlistItem = $entity instanceof ProductInterface ? $this->wishlistItemFactory->createWithProduct($entity) : $this->wishlistItemFactory->createWithProductVariant($entity); $preSelectedWishlists = $this->wishlistProvider->getPreSelectedWishlists(); + if ([] === $preSelectedWishlists) { + $preSelectedWishlists = [$this->wishlistFactory->createNew()]; + } foreach ($preSelectedWishlists as $wishlist) { $manager = $this->getManager($wishlist); $manager->persist($wishlist); diff --git a/src/Factory/WishlistFactory.php b/src/Factory/WishlistFactory.php index f2ac061..0ed4c0e 100644 --- a/src/Factory/WishlistFactory.php +++ b/src/Factory/WishlistFactory.php @@ -4,14 +4,19 @@ namespace Setono\SyliusWishlistPlugin\Factory; +use Setono\ClientBundle\Context\ClientContextInterface; use Setono\SyliusWishlistPlugin\Model\GuestWishlistInterface; use Setono\SyliusWishlistPlugin\Model\UserWishlistInterface; +use Setono\SyliusWishlistPlugin\Model\WishlistInterface; use Sylius\Component\User\Model\UserInterface; +use Symfony\Bundle\SecurityBundle\Security; use Webmozart\Assert\Assert; final class WishlistFactory implements WishlistFactoryInterface { public function __construct( + private readonly Security $security, + private readonly ClientContextInterface $clientContext, /** @var class-string $guestWishlistClass */ private readonly string $guestWishlistClass, @@ -20,9 +25,14 @@ public function __construct( ) { } - public function createNew(): never + public function createNew(): WishlistInterface { - throw new \RuntimeException('Use createForGuest or createForUser instead'); + $user = $this->security->getUser(); + if ($user instanceof UserInterface) { + return $this->createForUser($user); + } + + return $this->createForGuest($this->clientContext->getClient()->id); } public function createForGuest(string $clientId): GuestWishlistInterface diff --git a/src/Provider/CompositeWishlistProvider.php b/src/Provider/CompositeWishlistProvider.php index 82e3ee4..91a4feb 100644 --- a/src/Provider/CompositeWishlistProvider.php +++ b/src/Provider/CompositeWishlistProvider.php @@ -14,14 +14,13 @@ final class CompositeWishlistProvider extends CompositeService implements Wishli public function getWishlists(): array { foreach ($this->services as $service) { - try { - return $service->getWishlists(); - } catch (\RuntimeException) { - continue; + $wishlists = $service->getWishlists(); + if ([] !== $wishlists) { + return $wishlists; } } - throw new \RuntimeException('No wishlists found'); + return []; } public function getPreSelectedWishlists(): array diff --git a/src/Provider/GuestWishlistProvider.php b/src/Provider/GuestWishlistProvider.php index 4d805d1..a2e1792 100644 --- a/src/Provider/GuestWishlistProvider.php +++ b/src/Provider/GuestWishlistProvider.php @@ -20,7 +20,7 @@ public function getWishlists(): array $wishlist = $this->guestWishlistRepository->findOneByClientId($this->clientContext->getClient()->id); if (null === $wishlist) { - throw new \RuntimeException('No wishlists found'); + return []; } return [$wishlist]; diff --git a/src/Provider/NewWishlistProvider.php b/src/Provider/NewWishlistProvider.php deleted file mode 100644 index 2e2da6b..0000000 --- a/src/Provider/NewWishlistProvider.php +++ /dev/null @@ -1,36 +0,0 @@ -security->getUser(); - if ($user instanceof UserInterface) { - return [$this->wishlistFactory->createForUser($user)]; - } - - return [$this->wishlistFactory->createForGuest($this->clientContext->getClient()->id)]; - } - - public function getPreSelectedWishlists(): array - { - // todo implement - return $this->getWishlists(); - } -} diff --git a/src/Provider/UserWishlistProvider.php b/src/Provider/UserWishlistProvider.php index ab92158..9824ec8 100644 --- a/src/Provider/UserWishlistProvider.php +++ b/src/Provider/UserWishlistProvider.php @@ -20,16 +20,10 @@ public function getWishlists(): array { $user = $this->security->getUser(); if (!$user instanceof UserInterface) { - throw new \RuntimeException('No wishlists found'); + return []; } - $wishlists = $this->userWishlistRepository->findByUser($user); - - if ([] === $wishlists) { - throw new \RuntimeException('No wishlists found'); - } - - return $wishlists; + return $this->userWishlistRepository->findByUser($user); } public function getPreSelectedWishlists(): array diff --git a/src/Provider/WishlistProviderInterface.php b/src/Provider/WishlistProviderInterface.php index f054507..d91ab00 100644 --- a/src/Provider/WishlistProviderInterface.php +++ b/src/Provider/WishlistProviderInterface.php @@ -9,16 +9,12 @@ interface WishlistProviderInterface { /** - * @return non-empty-list - * - * @throws \RuntimeException if no wishlists are found + * @return list */ public function getWishlists(): array; /** - * @return non-empty-list - * - * @throws \RuntimeException if no wishlists are found + * @return list */ public function getPreSelectedWishlists(): array; } diff --git a/src/Resources/config/services/controller.xml b/src/Resources/config/services/controller.xml index 7ba32ec..1295770 100644 --- a/src/Resources/config/services/controller.xml +++ b/src/Resources/config/services/controller.xml @@ -53,6 +53,7 @@ + + + %setono_sylius_wishlist.model.guest_wishlist.class% %setono_sylius_wishlist.model.user_wishlist.class% diff --git a/src/Resources/config/services/provider.xml b/src/Resources/config/services/provider.xml index 63b13fc..b88eb0f 100644 --- a/src/Resources/config/services/provider.xml +++ b/src/Resources/config/services/provider.xml @@ -20,13 +20,5 @@ - - - - - - - -