Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

105 lines
3.6KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  5. use Symfony\Component\HttpFoundation\Cookie ;
  6. use Lc\ShopBundle\Context\VisitorInterface;
  7. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class UserUtils
  10. {
  11. protected $parameterBag ;
  12. protected $em ;
  13. protected $utils ;
  14. protected $requestStack ;
  15. protected $visitorRepository ;
  16. protected $merchantUtils ;
  17. public function __construct(ParameterBagInterface $parameterBag, EntityManagerInterface $em, Utils $utils,
  18. RequestStack $requestStack, MerchantUtilsInterface $merchantUtils)
  19. {
  20. $this->em = $em ;
  21. $this->parameterBag = $parameterBag ;
  22. $this->utils = $utils ;
  23. $this->requestStack = $requestStack ;
  24. $this->visitorRepository = $this->em->getRepository($this->em->getClassMetadata(VisitorInterface::class)->getName()) ;
  25. $this->merchantUtils = $merchantUtils ;
  26. }
  27. public function getCookieNameVisitor()
  28. {
  29. return $this->parameterBag->get('app.cookie_name_visitor') ;
  30. }
  31. public function cryptCookie($data)
  32. {
  33. return base64_encode($data);
  34. }
  35. public function decryptCookie($data)
  36. {
  37. return base64_decode($data);
  38. }
  39. public function setCookieVisitor($response, $cookie)
  40. {
  41. $response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), 0, '/', $this->utils->getCookieDomain()));
  42. }
  43. public function getVisitor($cookie)
  44. {
  45. return $this->visitorRepository->findOneBy(['cookie' => $cookie]) ;
  46. }
  47. public function getVisitorCurrent()
  48. {
  49. $cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor()) ;
  50. return $this->getVisitor($cookie) ;
  51. }
  52. public function addVisitor($cookie, $ip)
  53. {
  54. $classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName() ;
  55. $visitor = new $classVisitor ;
  56. $visitor->setCookie($cookie) ;
  57. $visitor->setIp($ip) ;
  58. $visitor->setTotalVisit(1) ;
  59. $visitor->setLastAccess(new \DateTime()) ;
  60. $this->em->persist($visitor);
  61. $this->em->flush() ;
  62. }
  63. public function updateVisitor($visitor)
  64. {
  65. $totalVisit = $visitor->getTotalVisit() + 1 ;
  66. $visitor->setTotalVisit($totalVisit) ;
  67. $visitor->setLastAccess(new \DateTime()) ;
  68. $this->em->persist($visitor);
  69. $this->em->flush() ;
  70. }
  71. public function setNewsletter($user, $subscribeNewsletter)
  72. {
  73. $currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
  74. $newsletters = $currentMerchant->getNewsletters() ;
  75. if(isset($newsletters[0]) && $newsletters[0]) {
  76. if($subscribeNewsletter) {
  77. $user->addNewsletter($newsletters[0]) ;
  78. }
  79. else {
  80. $user->removeNewsletter($newsletters[0]) ;
  81. }
  82. }
  83. $this->em->persist($user) ;
  84. $this->em->flush() ;
  85. }
  86. }