src/Manager/WishlistManager.php line 74

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use App\Entity\Customer;
  5. use App\Entity\WishlistProduct;
  6. class WishlistManager {
  7.     
  8.     private $em;
  9.     private $session;
  10.     private $customerMgr;
  11.     public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \App\Manager\CustomerManager $customerMgr)
  12.     {
  13.         $this->em $em;
  14.         $this->session $session;
  15.         $this->customerMgr $customerMgr;
  16.     }
  17.     
  18.     public function add($productId$customer)
  19.     {
  20.         $product $this->em->getRepository('App:Product')->find($productId);
  21.         if(empty($product)){
  22.             return false;
  23.         }
  24.         $length 0;
  25.         if(!empty($customer)){
  26.             $products $this->getProducts($customer);
  27.             if(!$this->productExists($product$products)){
  28.                 $wishlistProduct = new WishlistProduct();
  29.                 $wishlistProduct->setCreatedAt(new \DateTime());
  30.                 $wishlistProduct->setProduct($product);
  31.                 $wishlistProduct->setCustomer($customer);
  32.                 $this->em->persist($wishlistProduct);
  33.                 $this->em->flush();
  34.             }
  35.             $length $this->getCount($customer);
  36.         }else{
  37.             $products $this->getSessionProductIds();
  38.             if(!in_array($product->getId(), $products)){
  39.                 $products[] = $product->getId();
  40.                 $this->session->set('wishlist_products',$products);
  41.             }
  42.             $length $this->getCountFromSession();
  43.         }
  44.         return $length;
  45.     }
  46.     
  47.     public function addFromSession($customer)
  48.     {
  49.         $sessionProducts $this->session->get('wishlist_products',[]);
  50.         if(!empty($sessionProducts)){
  51.             $products $this->getProducts($customer);
  52.             foreach($sessionProducts as $p){
  53.                 $product $this->em->getRepository('App:Product')->find($p);
  54.                 if(!empty($product) && !$this->productExists($product$products)){
  55.                     $wishlistProduct = new WishlistProduct();
  56.                     $wishlistProduct->setCreatedAt(new \DateTime());
  57.                     $wishlistProduct->setProduct($product);
  58.                     $wishlistProduct->setCustomer($customer);
  59.                     $this->em->persist($wishlistProduct);
  60.                 }
  61.             }
  62.             $this->em->flush();
  63.             $this->session->set('wishlist_products',[]);
  64.         }
  65.     }
  66.     
  67.     public function getCountFromSession()
  68.     {
  69.         $idcreas $this->session->get('wishlist_idcreas',[]);
  70.         $products $this->session->get('wishlist_products',[]);
  71.         $creations $this->session->get('wishlist_creations',[]);
  72.         return count($products)+count($idcreas)+count($creations);
  73.     }
  74.     
  75.     public function getCount(Customer $customer)
  76.     {
  77.         $counter $this->em->getRepository('App:WishlistProduct')->getCountByCustomer($customer);
  78.         return $counter;
  79.     }
  80.     
  81.     public function remove($productId$customer)
  82.     {
  83.         $product $this->em->getRepository('App:Product')->find($productId);
  84.         if(empty($product)){
  85.             return false;
  86.         }
  87.         $cpt 0;
  88.         if(!empty($customer)){
  89.             $wishlistProduct $this->em->getRepository('App:WishlistProduct')->findOneBy(array('customer'=>$customer,'product'=>$product));
  90.             if(!empty($wishlistProduct)){
  91.                 $this->em->remove($wishlistProduct);
  92.                 $this->em->flush();
  93.             }
  94.             $products $this->getProducts($customer);
  95.             $cpt $this->getCount($customer);
  96.         }else{
  97.             $products $this->getSessionProductIds();
  98.             $newProducts = [];
  99.             foreach($products as $id){
  100.                 if($id!=$product->getId())
  101.                     $newProducts[] = $id;
  102.             }
  103.             $this->session->set('wishlist_products',$newProducts);
  104.             $cpt $this->getCountFromSession();
  105.         }
  106.         return $cpt;
  107.     }
  108.     
  109.     public function getProducts(Customer $customer$sort 'createdAt'$onlyActive true)
  110.     {
  111.         $products = [];
  112.         if($onlyActive){
  113.             $wishlistProducts $this->em->getRepository('App:WishlistProduct')->findByCustomer($customer$sort);
  114.         }else{
  115.             $wishlistProducts $this->em->getRepository('App:WishlistProduct')->findByCustomer($customer);
  116.         }
  117.         foreach ($wishlistProducts as $p){
  118.             $tmp $p->getProduct();
  119.             if(!empty($tmp))
  120.                 $products[] = $tmp;
  121.         }
  122.         return $products;
  123.     }
  124.     
  125.     public function getSessionProducts()
  126.     {
  127.         $products = [];
  128.         $ids $this->session->get('wishlist_products',[]);
  129.         foreach($ids as $id){
  130.             $tmp $this->em->getRepository('App:Product')->find($id);
  131.             if(!empty($tmp))
  132.                 $products[] = $tmp;
  133.         }
  134.         return $products;
  135.     }
  136.     
  137.     public function getSessionProductIds()
  138.     {
  139.         $products = [];
  140.         $ids $this->session->get('wishlist_products',[]);
  141.         foreach($ids as $id){
  142.             if(is_object($id)){
  143.                 $products[] = $id->getId();
  144.             }elseif(is_int($id)){
  145.                 $products[] = $id;
  146.             }
  147.         }
  148.         return $products;
  149.     }
  150.     public function productExists($newProduct,$products)
  151.     {
  152.         foreach($products as $p){
  153.             if($p->getId() == $newProduct->getId())
  154.                 return true;
  155.         }
  156.         return false;
  157.     }
  158.     
  159.     public function check() {
  160.         $customer $this->customerMgr->getCustomer();
  161.         $this->checkProducts($customer);
  162.     }
  163.     
  164.     public function checkProducts($customer) {
  165.         if($customer){
  166.             $products $this->getProducts($customer,null,false);
  167.         }else{
  168.             $products $this->getSessionProducts();
  169.         }
  170.         foreach($products as $product){
  171.             if(empty($product) || !$product->isActive()){
  172.                 if(!$this->remove($product->getId(), $customer)){
  173.                     print 'erreur de suppression';
  174.                 }
  175.             }
  176.         }
  177.     }
  178.     
  179.     public function isProductInList($product) {
  180.         $customer $this->customerMgr->getCustomer();
  181.         if($customer){
  182.             $currentProducts $this->getProducts($customer);
  183.         }else{
  184.             $currentProducts $this->getSessionProducts();
  185.         }
  186.         return $this->productExists($product$currentProducts);
  187.     }
  188.     
  189. }