From aae240ba345b7ad9ff77d4b76138c61d3bbaff81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 3 Dec 2024 09:19:44 +0100 Subject: [PATCH] Added some optimizations --- src/Checker/WishlistChecker.php | 28 ++++++++++++++ src/Checker/WishlistCheckerInterface.php | 16 ++++++++ .../FirstWishlistRedirectAction.php | 1 - src/Provider/CachedWishlistProvider.php | 38 +++++++++++++++++++ src/Resources/config/services.xml | 1 + src/Resources/config/services/checker.xml | 12 ++++++ src/Resources/config/services/provider.xml | 5 +++ src/Resources/config/services/twig.xml | 2 +- src/Twig/Runtime.php | 22 ++--------- 9 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 src/Checker/WishlistChecker.php create mode 100644 src/Checker/WishlistCheckerInterface.php create mode 100644 src/Provider/CachedWishlistProvider.php create mode 100644 src/Resources/config/services/checker.xml diff --git a/src/Checker/WishlistChecker.php b/src/Checker/WishlistChecker.php new file mode 100644 index 0000000..62f8524 --- /dev/null +++ b/src/Checker/WishlistChecker.php @@ -0,0 +1,28 @@ +wishlistProvider->getWishlists() as $wishlist) { + $hasProduct = $product instanceof ProductInterface ? $wishlist->hasProduct($product) : $wishlist->hasProductVariant($product); + if ($hasProduct) { + return true; + } + } + + return false; + } +} diff --git a/src/Checker/WishlistCheckerInterface.php b/src/Checker/WishlistCheckerInterface.php new file mode 100644 index 0000000..46bbdb5 --- /dev/null +++ b/src/Checker/WishlistCheckerInterface.php @@ -0,0 +1,16 @@ +|null */ + private ?array $wishlists = null; + + /** @var list|null */ + private ?array $preSelectedWishlists = null; + + public function __construct(private readonly WishlistProviderInterface $decorated) + { + } + + public function getWishlists(): array + { + if (null === $this->wishlists) { + $this->wishlists = $this->decorated->getWishlists(); + } + + return $this->wishlists; + } + + public function getPreSelectedWishlists(): array + { + if (null === $this->preSelectedWishlists) { + $this->preSelectedWishlists = $this->decorated->getWishlists(); + } + + return $this->preSelectedWishlists; + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index ca54dab..020f749 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -2,6 +2,7 @@ + diff --git a/src/Resources/config/services/checker.xml b/src/Resources/config/services/checker.xml new file mode 100644 index 0000000..ec6d95a --- /dev/null +++ b/src/Resources/config/services/checker.xml @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/src/Resources/config/services/provider.xml b/src/Resources/config/services/provider.xml index b88eb0f..e2d3739 100644 --- a/src/Resources/config/services/provider.xml +++ b/src/Resources/config/services/provider.xml @@ -20,5 +20,10 @@ + + + + diff --git a/src/Resources/config/services/twig.xml b/src/Resources/config/services/twig.xml index 0a7333b..09df1fa 100644 --- a/src/Resources/config/services/twig.xml +++ b/src/Resources/config/services/twig.xml @@ -8,7 +8,7 @@ - + diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php index b263113..4e95ed4 100644 --- a/src/Twig/Runtime.php +++ b/src/Twig/Runtime.php @@ -4,8 +4,7 @@ namespace Setono\SyliusWishlistPlugin\Twig; -use Doctrine\Persistence\ManagerRegistry; -use Setono\Doctrine\ORMTrait; +use Setono\SyliusWishlistPlugin\Checker\WishlistCheckerInterface; use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductVariantInterface; @@ -13,32 +12,19 @@ final class Runtime implements RuntimeExtensionInterface { - use ORMTrait; - public function __construct( private readonly WishlistProviderInterface $wishlistProvider, - ManagerRegistry $managerRegistry, + private readonly WishlistCheckerInterface $wishlistChecker, ) { - $this->managerRegistry = $managerRegistry; } public function onWishlist(ProductInterface|ProductVariantInterface $product): bool { - // todo should be optimized - foreach ($this->wishlistProvider->getWishlists() as $wishlist) { - $hasProduct = $product instanceof ProductInterface ? $wishlist->hasProduct($product) : $wishlist->hasProductVariant($product); - if ($hasProduct) { - return true; - } - } - - return false; + return $this->wishlistChecker->onWishlist($product); } public function hasWishlist(): bool { - $wishlist = $this->wishlistProvider->getWishlists()[0]; - - return $this->getManager($wishlist)->contains($wishlist); + return $this->wishlistProvider->getWishlists() !== []; } }