src/Service/Wishlist/SessionWishlistManager.php line 68

Open in your IDE?
  1. <?php
  2. namespace App\Service\Wishlist;
  3. use Doctrine\DBAL\Query\QueryBuilder;
  4. use Pimcore\Model\DataObject\CoreShopProduct;
  5. use Psr\Log\LoggerInterface;
  6. use Symfony\Component\HttpFoundation\Session\Session;
  7. class SessionWishlistManager implements WishlistManagerInterface
  8. {
  9.     private const SESSION_ID 'GUEST_WISHLIST_STORAGE';
  10.     public function __construct(
  11.         private readonly Session         $session,
  12.         private readonly LoggerInterface $logger,
  13.         private readonly bool            $wishlistLog false,
  14.     )
  15.     {
  16.     }
  17.     public function addItem(CoreShopProduct $product): void
  18.     {
  19.         if (!$this->hasItem($product)) {
  20.             $storage $this->getSessionStorage();
  21.             $storage[] = $product->getId();
  22.             $this->session->set(self::SESSION_ID$storage);
  23.             $this->log($productself::ACTION_ADD);
  24.         }
  25.     }
  26.     public function removeItem(CoreShopProduct $product): void
  27.     {
  28.         if ($this->hasItem($product)) {
  29.             $this->session->set(self::SESSION_IDarray_diff($this->getSessionStorage(), [$product->getId()]));
  30.             $this->log($productself::ACTION_REMOVE);
  31.         }
  32.     }
  33.     public function hasItem(CoreShopProduct $product): bool
  34.     {
  35.         return in_array($product->getId(), $this->getSessionStorage(), true);
  36.     }
  37.     public function getList(): CoreShopProduct\Listing
  38.     {
  39.         $storage $this->getSessionStorage();
  40.         $listing = new CoreShopProduct\Listing();
  41.         $listing->onCreateQueryBuilder(function (QueryBuilder $queryBuilder) use ($storage) {
  42.             $queryBuilder->andWhere($queryBuilder->expr()->in('oo_id'$storage));
  43.             $queryBuilder->orderBy(sprintf("FIELD(`oo_id`, %s)"implode(', '$storage)));
  44.         });
  45.         return $listing;
  46.     }
  47.     public function count(): int
  48.     {
  49.         return count($this->getSessionStorage());
  50.     }
  51.     private function getSessionStorage(): array
  52.     {
  53.         if (!$this->session->has(self::SESSION_ID) || !is_array($this->session->get(self::SESSION_ID))) {
  54.             $this->session->set(self::SESSION_ID, []);
  55.             return [];
  56.         }
  57.         return $this->session->get(self::SESSION_ID);
  58.     }
  59.     public function truncateStorage(): void
  60.     {
  61.         $this->session->remove(self::SESSION_ID);
  62.     }
  63.     private function log(CoreShopProduct $productstring $action): void
  64.     {
  65.         if (!$this->wishlistLog) {
  66.             return;
  67.         }
  68.         $this->logger->info(sprintf("Guest\nAction: %s\nProduct: %s (%d)\nDatetime: %s",
  69.             $action,
  70.             $product->getSku(),
  71.             $product->getId(),
  72.             date('Y-m-d H:i:s')
  73.         ), ['component' => 'wishlist']);
  74.     }
  75. }