Skip to content

Commit

Permalink
Added some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 3, 2024
1 parent 1007b8c commit aae240b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 20 deletions.
28 changes: 28 additions & 0 deletions src/Checker/WishlistChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusWishlistPlugin\Checker;

use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface;
use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;

final class WishlistChecker implements WishlistCheckerInterface
{
public function __construct(private readonly WishlistProviderInterface $wishlistProvider)
{
}

public function onWishlist(ProductInterface|ProductVariantInterface $product): bool
{
foreach ($this->wishlistProvider->getWishlists() as $wishlist) {
$hasProduct = $product instanceof ProductInterface ? $wishlist->hasProduct($product) : $wishlist->hasProductVariant($product);
if ($hasProduct) {
return true;
}
}

return false;
}
}
16 changes: 16 additions & 0 deletions src/Checker/WishlistCheckerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusWishlistPlugin\Checker;

use Sylius\Component\Core\Model\ProductInterface;
use Sylius\Component\Core\Model\ProductVariantInterface;

interface WishlistCheckerInterface
{
/**
* Returns true if the given product/variant is on _any_ wishlist
*/
public function onWishlist(ProductInterface|ProductVariantInterface $product): bool;
}
1 change: 0 additions & 1 deletion src/Controller/FirstWishlistRedirectAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
final class FirstWishlistRedirectAction
{

public function __construct(
private readonly WishlistProviderInterface $wishlistProvider,
private readonly UrlGeneratorInterface $urlGenerator,
Expand Down
38 changes: 38 additions & 0 deletions src/Provider/CachedWishlistProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusWishlistPlugin\Provider;

use Setono\SyliusWishlistPlugin\Model\WishlistInterface;

final class CachedWishlistProvider implements WishlistProviderInterface
{
/** @var list<WishlistInterface>|null */
private ?array $wishlists = null;

/** @var list<WishlistInterface>|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;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<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">
<imports>
<import resource="services/checker.xml"/>
<import resource="services/controller.xml"/>
<import resource="services/event_listener.xml"/>
<import resource="services/event_subscriber.xml"/>
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/config/services/checker.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\Checker\WishlistCheckerInterface"
alias="Setono\SyliusWishlistPlugin\Checker\WishlistChecker"/>

<service id="Setono\SyliusWishlistPlugin\Checker\WishlistChecker">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
</service>
</services>
</container>
5 changes: 5 additions & 0 deletions src/Resources/config/services/provider.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@

<tag name="setono_sylius_wishlist.wishlist_provider" priority="-50"/>
</service>

<service id="Setono\SyliusWishlistPlugin\Provider\CachedWishlistProvider"
decorates="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface" decoration-priority="64">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\CachedWishlistProvider.inner"/>
</service>
</services>
</container>
2 changes: 1 addition & 1 deletion src/Resources/config/services/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<service id="Setono\SyliusWishlistPlugin\Twig\Runtime">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
<argument type="service" id="doctrine"/>
<argument type="service" id="Setono\SyliusWishlistPlugin\Checker\WishlistCheckerInterface"/>

<tag name="twig.runtime"/>
</service>
Expand Down
22 changes: 4 additions & 18 deletions src/Twig/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,27 @@

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;
use Twig\Extension\RuntimeExtensionInterface;

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() !== [];
}
}

0 comments on commit aae240b

Please sign in to comment.