Skip to content

Commit

Permalink
Create a cached wishlist checker
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 3, 2024
1 parent e1b21a0 commit 0cf0831
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/Checker/CachedWishlistChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusWishlistPlugin\Checker;

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

final class CachedWishlistChecker implements WishlistCheckerInterface
{
/** @var array<string, bool> */
private array $cache = [];

public function __construct(private readonly WishlistCheckerInterface $decorated)
{
}

public function onWishlist(ProductInterface|ProductVariantInterface $product): bool
{
$key = spl_object_hash($product);

if (!array_key_exists($key, $this->cache)) {
$this->cache[$key] = $this->decorated->onWishlist($product);
}

return $this->cache[$key];
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services/checker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
<service id="Setono\SyliusWishlistPlugin\Checker\WishlistChecker">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
</service>

<service id="Setono\SyliusWishlistPlugin\Checker\CachedWishlistChecker"
decorates="Setono\SyliusWishlistPlugin\Checker\WishlistCheckerInterface" decoration-priority="64">
<argument type="service" id="Setono\SyliusWishlistPlugin\Checker\CachedWishlistChecker.inner"/>
</service>
</services>
</container>

0 comments on commit 0cf0831

Please sign in to comment.