src/Service/FavouritesStorage.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use MLDev\AccountBundle\Entity\Account;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use Symfony\Component\Security\Core\Security;
  8. /**
  9.  * Class FavouritesStorage
  10.  * @package App\Services
  11.  */
  12. class FavouritesStorage
  13. {
  14.     const STORAGE_KEY 'mldev-favourites-storage';
  15.     /**
  16.      * @var SessionInterface
  17.      */
  18.     private $session;
  19.     /**
  20.      * @var EntityManagerInterface
  21.      */
  22.     private $entityManager;
  23.     /**
  24.      * @var Security
  25.      */
  26.     private $security;
  27.     /**
  28.      * FavouritesStorage constructor.
  29.      */
  30.     public function __construct(Security $securityRequestStack $requestStackEntityManagerInterface $entityManager)
  31.     {
  32.         $this->session $requestStack->getSession();
  33.         $this->security $security;
  34.         $this->entityManager $entityManager;
  35.     }
  36.     /**
  37.      * Read data from session
  38.      */
  39.     public function read(): array
  40.     {
  41.         $user $this->security->getUser();
  42.         $favourites = [];
  43.         if ($user instanceof Account) {
  44.             $favourites $user->getFavourites();
  45.         }
  46.         $this->session->set(self::STORAGE_KEY$favourites);
  47.         return $favourites;
  48.     }
  49.     /**
  50.      * Write data in session
  51.      */
  52.     public function write(array $data = []): void
  53.     {
  54.         $user $this->security->getUser();
  55.         if ($user instanceof Account) {
  56.             $user->setFavourites($data);
  57.         }
  58.         $this->session->set(self::STORAGE_KEY$data);
  59.         $this->entityManager->persist($user);
  60.         $this->entityManager->flush();
  61.     }
  62.     /**
  63.      * Set element by id and count
  64.      */
  65.     public function set(int $idint $count): array
  66.     {
  67.         $storage $this->read();
  68.         // set item and count to storage
  69.         $storage[$id] = $count;
  70.         //check and remove
  71.         if (!$storage[$id]) {
  72.             // remove item from storage
  73.             unset($storage[$id]);
  74.         }
  75.         $this->write($storage);
  76.         return $storage;
  77.     }
  78.     /**
  79.      * Remove element by id
  80.      */
  81.     public function remove(int $id): array
  82.     {
  83.         $storage $this->read();
  84.         //check and remove
  85.         if ($storage[$id]) {
  86.             // remove item from storage
  87.             unset($storage[$id]);
  88.         }
  89.         $this->write($storage);
  90.         return $storage;
  91.     }
  92.     /**
  93.      * Clear session storage
  94.      */
  95.     public function clear(): void
  96.     {
  97.         $this->write();
  98.     }
  99. }