src/Services/CompareManager.php line 122

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\Category;
  4. use App\Entity\CompareProduct;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Doctrine\ORM\EntityRepository;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
  12. use Symfony\Component\HttpFoundation\Session\Session;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. class CompareManager
  15. {
  16.     /** @var CompareProduct */
  17.     protected $compareProduct;
  18.     /** @var Session Session */
  19.     protected $session;
  20.     protected $em;
  21.     protected $request;
  22.     const MAX_PRODUCTS_IN_COMPARE 5;
  23.     const MAX_PRODUCT_IN_COMPARE_ERROR 'Możesz dodać do porównania maksymalnie 5 produktów. Przejdź do porównania i usuń wybrane produkty.';
  24.     /**
  25.      * CompareManager constructor.
  26.      * @param EntityManager $em
  27.      * @param RequestStack $requestStack
  28.      * @param Session $session
  29.      */
  30.     public function __construct(EntityManagerInterface $emRequestStack $requestStackSessionInterface $session) {
  31.         $this->session $session;
  32.         $this->em $em;
  33.         $this->request $requestStack->getCurrentRequest();
  34.     }
  35.     /**
  36.      * @return CompareProduct
  37.      */
  38.     public function getCompareProduct() {
  39.         return $this->compareProduct;
  40.     }
  41.     /**
  42.      * @param CompareProduct $product
  43.      * @return $this
  44.      */
  45.     public function setCompareProduct(CompareProduct $product) {
  46.         $this->compareProduct $product;
  47.         return $this;
  48.     }
  49.     public function getRepository() {
  50.         return $this->em->getRepository(\App\Entity\CompareProduct::class);
  51.     }
  52.     /**
  53.      * @return int
  54.      */
  55.     public function checkProductsNumber() {
  56.         $products $this->getRepository()->findBy(array('session'=>$this->session->get('cart_session')));
  57.         $c 0;
  58.         foreach ($products as $p) {
  59.             $c $c 1;
  60.         }
  61.         return $c;
  62.     }
  63.     /**
  64.      * @param CompareProduct $cartItem
  65.      * @return CompareProduct
  66.      */
  67.     public function save(CompareProduct $cartItem) {
  68.         if ($this->checkProductsNumber() > self::MAX_PRODUCTS_IN_COMPARE) {
  69.             //throw new CompareException(self::MAX_PRODUCT_IN_COMPARE_ERROR);
  70.         }
  71.         $this->doSave($cartItem);
  72.         return $cartItem;
  73.     }
  74.     /**
  75.      * @return CompareProduct
  76.      */
  77.     public function create() {
  78.         /** @var $compareProduct CompareProduct */
  79.         $compareProduct = new CompareProduct();
  80.         $compareProduct->setSession($this->session->getId());
  81.         return $compareProduct;
  82.     }
  83.     /**
  84.      * Remove from database and flush
  85.      * @param CompareProduct $cartItem
  86.      */
  87.     public function remove(CompareProduct $cartItem) {
  88.         $this->em->remove($cartItem);
  89.         $this->em->flush();
  90.     }
  91.     /**
  92.      * @param CompareProduct $cartItem
  93.      * @throws \Doctrine\ORM\ORMException
  94.      * @throws \Doctrine\ORM\OptimisticLockException
  95.      */
  96.     protected function doSave(CompareProduct $cartItem) {
  97.         $this->em->persist($cartItem);
  98.         $this->em->flush();
  99.     }
  100.     /**
  101.      * @return object
  102.      */
  103.     public function getCompareProducts() {
  104.         $products $this->em->getRepository(CompareProduct::class)->findBy(array('session'=>$this->session->get('cart_session')));
  105.         return $products;
  106.     }
  107.     /**
  108.      * Gets ids of the products currently added to compare
  109.      * @return array
  110.      */
  111.     public function getIdsCompareProducts() {
  112.         $arr = array();
  113.         /** @var $compareProduct CompareProduct */
  114.         foreach ($this->getCompareProducts() as $compareProduct) {
  115.             $arr[] = $compareProduct->getProduct()->getId();
  116.         }
  117.         return $arr;
  118.     }
  119.     /**
  120.      * If compare should be available
  121.      * @param Category $category
  122.      * @return bool
  123.      */
  124.     public function ifCompareAvailable(Category $category) {
  125.         $categories[] = 118;
  126.         $categories[] = 119;
  127.         $categories[] = 133;
  128.         $categories[] = 138;
  129.         if (in_array($category->getId(), $categories) or in_array($category->getParent()->getId(), $categories)) {
  130.             return true;
  131.         }
  132.         return false;
  133.     }
  134. }