app/Plugin/ProductOption/Doctrine/EventSubscriber/CartItemLogSubscriberEvent.php line 33

Open in your IDE?
  1. <?php
  2. namespace Plugin\ProductOption\Doctrine\EventSubscriber;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Eccube\Entity\CartItemLog;
  5. use Eccube\Event\CartItemChangedEvent;
  6. use Eccube\Repository\CartRepository;
  7. use Eccube\Service\CartService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CartItemLogSubscriberEvent implements EventSubscriberInterface
  10. {
  11.     protected $entityManager;
  12.     protected $cartRepository;
  13.     protected $cartService;
  14.     public function __construct(EntityManagerInterface $entityManagerCartRepository $cartRepositoryCartService $cartService)
  15.     {
  16.         $this->entityManager $entityManager;
  17.         $this->cartRepository $cartRepository;
  18.         $this->cartService $cartService;
  19.     }
  20.     public static function getSubscribedEvents()
  21.     {
  22.         return [
  23.             CartItemChangedEvent::ADDED => 'onCartItemAdded',
  24.             CartItemChangedEvent::REMOVED => 'onCartItemRemoved',
  25.         ];
  26.     }
  27.     public function onCartItemAdded(CartItemChangedEvent $event)
  28.     {
  29.         $CartItem $event->getCartItem();
  30.         $Customer $event->getCustomer();
  31.         $ProductClass $CartItem->getProductClass();
  32.         if (!$Customer || !$ProductClass) {
  33.             return;
  34.         }
  35.         $repo $this->entityManager->getRepository(CartItemLog::class);
  36.         $exists $repo->findOneBy([
  37.             'Customer' => $Customer,
  38.             'ProductClass' => $ProductClass,
  39.         ]);
  40.         if (!$exists) {
  41.             $Log = new CartItemLog();
  42.             $Log->setCustomer($Customer);
  43.             $Log->setProductClass($ProductClass);
  44.             $this->entityManager->persist($Log);
  45.             $this->entityManager->flush($Log);
  46.         }
  47.     }
  48.     public function onCartItemRemoved(CartItemChangedEvent $event)
  49.     {
  50.         $CartItem $event->getCartItem();
  51.         $Customer $event->getCustomer();
  52.         $ProductClass $CartItem->getProductClass();
  53.         if (!$Customer || !$ProductClass) {
  54.             return;
  55.         }
  56.         $carts $this->cartService->getCarts($Customer);
  57.         foreach ($carts as $cart) {
  58.             foreach ($cart->getCartItems() as $item) {
  59.                 if (
  60.                     $item->getProductClass()
  61.                     && $item->getProductClass()->getId() === $ProductClass->getId()
  62.                 ) {
  63.                     return;
  64.                 }
  65.             }
  66.         }
  67.         $repo $this->entityManager->getRepository(CartItemLog::class);
  68.         $Log $repo->findOneBy([
  69.             'Customer' => $Customer,
  70.             'ProductClass' => $ProductClass,
  71.         ]);
  72.         if ($Log) {
  73.             $this->entityManager->remove($Log);
  74.             $this->entityManager->flush($Log);
  75.         }
  76.     }
  77. }