src/Manager/SeenProductManager.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Manager;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. class SeenProductManager {
  5. private $em;
  6. private $session;
  7. public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session)
  8. {
  9. $this->em = $em;
  10. $this->session = $session;
  11. }
  12. public function addToSeenProducts($id)
  13. {
  14. // die('addToSeenProducts '.$id);
  15. $ids = $this->getSeenProducts();
  16. // print_r($ids);
  17. if(ctype_digit($id) && !in_array($id, $ids)){
  18. $ids[] = $id;
  19. }
  20. // print_r($ids);
  21. // die(implode('-',$ids));
  22. $this->session->set('seen-products',implode('-',$ids));
  23. }
  24. public function getSeenProducts()
  25. {
  26. $ids = $this->session->get('seen-products','');
  27. return empty($ids)?[]:explode('-',$ids);
  28. }
  29. public function isInSeenProducts($id)
  30. {
  31. $ids = $this->getSeenProducts();
  32. return in_array($id, $ids);
  33. }
  34. }