You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserUtils.php 3.9KB

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