<?php
namespace App\Manager;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Customer;
use App\Entity\WishlistProduct;
class WishlistManager {
private $em;
private $session;
private $customerMgr;
public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session, \App\Manager\CustomerManager $customerMgr)
{
$this->em = $em;
$this->session = $session;
$this->customerMgr = $customerMgr;
}
public function add($productId, $customer)
{
$product = $this->em->getRepository('App:Product')->find($productId);
if(empty($product)){
return false;
}
$length = 0;
if(!empty($customer)){
$products = $this->getProducts($customer);
if(!$this->productExists($product, $products)){
$wishlistProduct = new WishlistProduct();
$wishlistProduct->setCreatedAt(new \DateTime());
$wishlistProduct->setProduct($product);
$wishlistProduct->setCustomer($customer);
$this->em->persist($wishlistProduct);
$this->em->flush();
}
$length = $this->getCount($customer);
}else{
$products = $this->getSessionProductIds();
if(!in_array($product->getId(), $products)){
$products[] = $product->getId();
$this->session->set('wishlist_products',$products);
}
$length = $this->getCountFromSession();
}
return $length;
}
public function addFromSession($customer)
{
$sessionProducts = $this->session->get('wishlist_products',[]);
if(!empty($sessionProducts)){
$products = $this->getProducts($customer);
foreach($sessionProducts as $p){
$product = $this->em->getRepository('App:Product')->find($p);
if(!empty($product) && !$this->productExists($product, $products)){
$wishlistProduct = new WishlistProduct();
$wishlistProduct->setCreatedAt(new \DateTime());
$wishlistProduct->setProduct($product);
$wishlistProduct->setCustomer($customer);
$this->em->persist($wishlistProduct);
}
}
$this->em->flush();
$this->session->set('wishlist_products',[]);
}
}
public function getCountFromSession()
{
$idcreas = $this->session->get('wishlist_idcreas',[]);
$products = $this->session->get('wishlist_products',[]);
$creations = $this->session->get('wishlist_creations',[]);
return count($products)+count($idcreas)+count($creations);
}
public function getCount(Customer $customer)
{
$counter = $this->em->getRepository('App:WishlistProduct')->getCountByCustomer($customer);
return $counter;
}
public function remove($productId, $customer)
{
$product = $this->em->getRepository('App:Product')->find($productId);
if(empty($product)){
return false;
}
$cpt = 0;
if(!empty($customer)){
$wishlistProduct = $this->em->getRepository('App:WishlistProduct')->findOneBy(array('customer'=>$customer,'product'=>$product));
if(!empty($wishlistProduct)){
$this->em->remove($wishlistProduct);
$this->em->flush();
}
$products = $this->getProducts($customer);
$cpt = $this->getCount($customer);
}else{
$products = $this->getSessionProductIds();
$newProducts = [];
foreach($products as $id){
if($id!=$product->getId())
$newProducts[] = $id;
}
$this->session->set('wishlist_products',$newProducts);
$cpt = $this->getCountFromSession();
}
return $cpt;
}
public function getProducts(Customer $customer, $sort = 'createdAt', $onlyActive = true)
{
$products = [];
if($onlyActive){
$wishlistProducts = $this->em->getRepository('App:WishlistProduct')->findByCustomer($customer, $sort);
}else{
$wishlistProducts = $this->em->getRepository('App:WishlistProduct')->findByCustomer($customer);
}
foreach ($wishlistProducts as $p){
$tmp = $p->getProduct();
if(!empty($tmp))
$products[] = $tmp;
}
return $products;
}
public function getSessionProducts()
{
$products = [];
$ids = $this->session->get('wishlist_products',[]);
foreach($ids as $id){
$tmp = $this->em->getRepository('App:Product')->find($id);
if(!empty($tmp))
$products[] = $tmp;
}
return $products;
}
public function getSessionProductIds()
{
$products = [];
$ids = $this->session->get('wishlist_products',[]);
foreach($ids as $id){
if(is_object($id)){
$products[] = $id->getId();
}elseif(is_int($id)){
$products[] = $id;
}
}
return $products;
}
public function productExists($newProduct,$products)
{
foreach($products as $p){
if($p->getId() == $newProduct->getId())
return true;
}
return false;
}
public function check() {
$customer = $this->customerMgr->getCustomer();
$this->checkProducts($customer);
}
public function checkProducts($customer) {
if($customer){
$products = $this->getProducts($customer,null,false);
}else{
$products = $this->getSessionProducts();
}
foreach($products as $product){
if(empty($product) || !$product->isActive()){
if(!$this->remove($product->getId(), $customer)){
print 'erreur de suppression';
}
}
}
}
public function isProductInList($product) {
$customer = $this->customerMgr->getCustomer();
if($customer){
$currentProducts = $this->getProducts($customer);
}else{
$currentProducts = $this->getSessionProducts();
}
return $this->productExists($product, $currentProducts);
}
}