vendor/bitbag/wishlist-plugin/src/Twig/WishlistExtension.php line 97

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file was created by developers working at BitBag
  4.  * Do you need more information about us and what we do? Visit our https://bitbag.io website!
  5.  * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career
  6. */
  7. declare(strict_types=1);
  8. namespace BitBag\SyliusWishlistPlugin\Twig;
  9. use BitBag\SyliusWishlistPlugin\Entity\WishlistInterface;
  10. use BitBag\SyliusWishlistPlugin\Repository\WishlistRepositoryInterface;
  11. use BitBag\SyliusWishlistPlugin\Resolver\WishlistCookieTokenResolverInterface;
  12. use Sylius\Component\Channel\Context\ChannelNotFoundException;
  13. use Sylius\Component\Core\Model\ChannelInterface;
  14. use Sylius\Component\Core\Model\ShopUserInterface;
  15. use Sylius\Component\User\Model\UserInterface;
  16. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  17. use Twig\Extension\AbstractExtension;
  18. use Twig\TwigFunction;
  19. class WishlistExtension extends AbstractExtension
  20. {
  21.     private WishlistRepositoryInterface $wishlistRepository;
  22.     private WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver;
  23.     public function __construct(
  24.         WishlistRepositoryInterface $wishlistRepository,
  25.         WishlistCookieTokenResolverInterface $wishlistCookieTokenResolver
  26.     ) {
  27.         $this->wishlistRepository $wishlistRepository;
  28.         $this->wishlistCookieTokenResolver $wishlistCookieTokenResolver;
  29.     }
  30.     public function getFunctions(): array
  31.     {
  32.         return [
  33.             new TwigFunction('getWishlists', [$this'getWishlists']),
  34.             new TwigFunction('findAllByShopUser', [$this'findAllByShopUser']),
  35.             new TwigFunction('findAllByAnonymous', [$this'findAllByAnonymous']),
  36.             new TwigFunction('findAllByShopUserAndToken', [$this'findAllByShopUserAndToken']),
  37.             new TwigFunction('findAllByShopUserAndChannel', [$this'findAllByShopUserAndChannel']),
  38.             new TwigFunction('findAllByAnonymousAndChannel', [$this'findAllByAnonymousAndChannel']),
  39.         ];
  40.     }
  41.     public function getWishlists(): ?array
  42.     {
  43.         /** @var WishlistInterface[] $wishlists */
  44.         $wishlists $this->wishlistRepository->findAll();
  45.         return $wishlists;
  46.     }
  47.     public function findAllByShopUser(UserInterface $user null): ?array
  48.     {
  49.         if (!$user instanceof ShopUserInterface) {
  50.             throw new UnsupportedUserException();
  51.         }
  52.         return $this->wishlistRepository->findAllByShopUser($user->getId());
  53.     }
  54.     public function findAllByShopUserAndToken(UserInterface $user null): ?array
  55.     {
  56.         $wishlistCookieToken $this->wishlistCookieTokenResolver->resolve();
  57.         if (!$user instanceof ShopUserInterface) {
  58.             throw new UnsupportedUserException();
  59.         }
  60.         return $this->wishlistRepository->findAllByShopUserAndToken($user->getId(), $wishlistCookieToken);
  61.     }
  62.     public function findAllByAnonymous(): ?array
  63.     {
  64.         $wishlistCookieToken $this->wishlistCookieTokenResolver->resolve();
  65.         return $this->wishlistRepository->findAllByAnonymous($wishlistCookieToken);
  66.     }
  67.     public function findAllByShopUserAndChannel(UserInterface $user nullChannelInterface $channel null): ?array
  68.     {
  69.         if (!$user instanceof ShopUserInterface) {
  70.             throw new UnsupportedUserException();
  71.         }
  72.         if (!$channel instanceof ChannelInterface) {
  73.             throw new ChannelNotFoundException();
  74.         }
  75.         return $this->wishlistRepository->findAllByShopUser($user->getId());
  76.     }
  77.     public function findAllByAnonymousAndChannel(ChannelInterface $channel): ?array
  78.     {
  79.         $wishlistCookieToken $this->wishlistCookieTokenResolver->resolve();
  80.         return $this->wishlistRepository->findAllByAnonymousAndChannel($wishlistCookieToken$channel);
  81.     }
  82. }