src/Business/Cart/Session/CartSessionHandler.php line 64

Open in your IDE?
  1. <?php
  2. namespace App\Business\Cart\Session;
  3. use App\Business\Cart\Transfer\CartAreaTransfer;
  4. use App\Entity\Customer;
  5. use App\Entity\CustomerAdvertisingArea;
  6. use App\Repository\CustomerAdvertisingAreaRepository;
  7. use App\Service\Utils\CustomerServiceInterface;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. class CartSessionHandler implements CartSessionHandlerInterface
  14. {
  15.     private const CART_SESSION_NAME 'CartSessionHandler:CART';
  16.     private SessionInterface $session;
  17.     private CustomerAdvertisingAreaRepository $customerAdvertisingAreaRepository;
  18.     private Customer $customer;
  19.     private CustomerServiceInterface $customerService;
  20.     private EntityManagerInterface $entityManager;
  21.     /**
  22.      * @param EntityManagerInterface $entityManager
  23.      * @param CustomerAdvertisingAreaRepository $customerAdvertisingAreaRepository
  24.      * @param CustomerServiceInterface $customerService
  25.      * @param RequestStack $request
  26.      */
  27.     public function __construct(
  28.         EntityManagerInterface $entityManager,
  29.         CustomerAdvertisingAreaRepository $customerAdvertisingAreaRepository,
  30.         CustomerServiceInterface $customerService,
  31.         RequestStack $request
  32.     ) {
  33.         $this->session $request->getSession();
  34.         $this->customerAdvertisingAreaRepository $customerAdvertisingAreaRepository;
  35.         $this->customerService $customerService;
  36.         $this->entityManager $entityManager;
  37.         if ($this->customerService->getCustomerByHost() !== null) {
  38.             $this->customer $this->customerService->getCustomerByHost();
  39.         } else {
  40.             $this->customer $this->customerService->getCustomer();
  41.         }
  42.         $this->init();
  43.     }
  44.     /**
  45.      * @return void
  46.      */
  47.     protected function init(): void
  48.     {
  49.         /** @var ArrayCollection|null $items */
  50.         $items $this->session->get(static::CART_SESSION_NAME);
  51.         if ($items === null) {
  52.             $items = new ArrayCollection();
  53.         }
  54.         $this->session->set(static::CART_SESSION_NAME$items);
  55.     }
  56.     /**
  57.      * @return void
  58.      */
  59.     public function clear(): void
  60.     {
  61.         $this->session->remove(static::CART_SESSION_NAME);
  62.     }
  63.     /**
  64.      * @param string $areaUuid
  65.      *
  66.      * @return CartAreaTransfer|null
  67.      */
  68.     public function getItem(string $areaUuid): ?CartAreaTransfer
  69.     {
  70.         $items $this->session->get(static::CART_SESSION_NAME);
  71.         /** @var CartAreaTransfer $item */
  72.         foreach ($items as $item) {
  73.             if ($areaUuid === $item->getUuidCustomerAdvertisingArea()) {
  74.                 return $item;
  75.             }
  76.         }
  77.         return null;
  78.     }
  79.     /**
  80.      * @param bool $entity
  81.      *
  82.      * @return Collection|null
  83.      */
  84.     public function getItems(bool $entity false): ?Collection
  85.     {
  86.         if ($entity === true) {
  87.             $collection = new ArrayCollection();
  88.             /** @var CartAreaTransfer $item */
  89.             foreach ($this->session->get(static::CART_SESSION_NAME) as $item) {
  90.                 $area $this->entityManager->getRepository(CustomerAdvertisingArea::class)
  91.                     ->findOneBy([
  92.                         'uuid' => $item->getUuidCustomerAdvertisingArea(),
  93.                         'customer' => $this->customer->getId()
  94.                     ]);
  95.                 if ($area === null) {
  96.                     continue;
  97.                 }
  98.                 $area->setWithinSetupPrice($item->isWithinSetupPrice());
  99.                 $collection->add($area);
  100.             }
  101.             return $collection;
  102.         }
  103.         return $this->session->get(static::CART_SESSION_NAME);
  104.     }
  105.     /**
  106.      * @param CartAreaTransfer $cartAreaTransfer
  107.      *
  108.      * @return bool
  109.      */
  110.     public function addToCart(CartAreaTransfer $cartAreaTransfer): bool
  111.     {
  112.         if ($this->isInCart($cartAreaTransfer) === true) {
  113.             return false;
  114.         }
  115.         /** @var ArrayCollection<CartAreaTransfer> $items */
  116.         $items $this->session->get(static::CART_SESSION_NAME);
  117.         $items->add($cartAreaTransfer);
  118.         $this->session->set(static::CART_SESSION_NAME$items);
  119.         return true;
  120.     }
  121.     /**
  122.      * @param string $uuidCustomerAdvertisingArea
  123.      *
  124.      * @return bool
  125.      */
  126.     public function removeFromCart(string $uuidCustomerAdvertisingArea): bool
  127.     {
  128.         $cartAreaTransfer = (new CartAreaTransfer())
  129.             ->setUuidCustomerAdvertisingArea($uuidCustomerAdvertisingArea);
  130.         if ($this->isInCart($cartAreaTransfer) === false) {
  131.             return false;
  132.         }
  133.         /** @var ArrayCollection<CartAreaTransfer> $items */
  134.         $items $this->session->get(static::CART_SESSION_NAME);
  135.         $rebuildCollection = new ArrayCollection();
  136.         /** @var CartAreaTransfer $item */
  137.         foreach ($items as $item) {
  138.             if ($item->getUuidCustomerAdvertisingArea() === $uuidCustomerAdvertisingArea) {
  139.                 continue;
  140.             }
  141.             $rebuildCollection->add($item);
  142.         }
  143.         $this->session->set(static::CART_SESSION_NAME$rebuildCollection);
  144.         return true;
  145.     }
  146.     /**
  147.      * @param CartAreaTransfer $cartAreaTransfer
  148.      *
  149.      * @return bool
  150.      */
  151.     public function isInCart(CartAreaTransfer $cartAreaTransfer): bool
  152.     {
  153.         /** @var ArrayCollection<CartAreaTransfer> $items */
  154.         $items $this->session->get(static::CART_SESSION_NAME);
  155.         if ($items === null) {
  156.             return false;
  157.         }
  158.         $customerAdvertisingArea $this->customerAdvertisingAreaRepository->findOneBy([
  159.             'uuid' => $cartAreaTransfer->getUuidCustomerAdvertisingArea(),
  160.             'customer' => $this->customer->getId(),
  161.         ]);
  162.         /** @var CartAreaTransfer $item */
  163.         return $items->exists(function ($key$item) use ($customerAdvertisingArea) {
  164.             return $item->getUuidCustomerAdvertisingArea() === $customerAdvertisingArea->getUuid();
  165.         });
  166.     }
  167.     /**
  168.      * @param string $areaUuid
  169.      *
  170.      * @return bool
  171.      */
  172.     public function toggleAreaProductionAndSetup(string $areaUuid): bool
  173.     {
  174.         $selectedItem null;
  175.         /** @var ArrayCollection<CartAreaTransfer> $items */
  176.         $items $this->getItems();
  177.         foreach ($items as $item) {
  178.             if ($areaUuid !== $item->getUuidCustomerAdvertisingArea()) {
  179.                 continue;
  180.             }
  181.             $selectedItem $item;
  182.             break;
  183.         }
  184.         if ($selectedItem === null) {
  185.             return false;
  186.         }
  187.         $selectedItem->setWithinSetupPrice(!$selectedItem->isWithinSetupPrice());
  188.         return true;
  189.     }
  190. }