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.     
  6.     private $em;
  7.     private $session;
  8.     public function __construct(EntityManagerInterface $em, \Symfony\Component\HttpFoundation\Session\SessionInterface $session)
  9.     {
  10.         $this->em $em;
  11.         $this->session $session;
  12.     }
  13.     
  14.     public function addToSeenProducts($id)
  15.     {
  16. //        die('addToSeenProducts '.$id);
  17.         $ids $this->getSeenProducts();
  18. //        print_r($ids);
  19.         if(ctype_digit($id) && !in_array($id$ids)){
  20.             $ids[] = $id;
  21.         }
  22. //        print_r($ids);
  23. //        die(implode('-',$ids));
  24.         $this->session->set('seen-products',implode('-',$ids));
  25.     }
  26.     
  27.     public function getSeenProducts()
  28.     {
  29.         $ids $this->session->get('seen-products','');
  30.         return empty($ids)?[]:explode('-',$ids);
  31.     }
  32.     
  33.     public function isInSeenProducts($id)
  34.     {
  35.         $ids $this->getSeenProducts();
  36.         return in_array($id$ids);
  37.     }
  38.     
  39. }