src/Manager/WishlistManager.php line 73

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