108 lines
3.8KB

  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. $response->headers->setCookie(Cookie::create($this->getCookieNameVisitor(), $this->cryptCookie($cookie), 0, '/', $this->utils->getCookieDomain()));
  45. }
  46. public function getVisitor($cookie)
  47. {
  48. return $this->visitorRepository->findOneBy(['cookie' => $cookie]) ;
  49. }
  50. public function getVisitorCurrent()
  51. {
  52. $cookie = $this->requestStack->getCurrentRequest()->cookies->get($this->getCookieNameVisitor()) ;
  53. return $this->getVisitor($cookie) ;
  54. }
  55. public function addVisitor($cookie, $ip)
  56. {
  57. $classVisitor = $this->em->getClassMetadata(VisitorInterface::class)->getName() ;
  58. $visitor = new $classVisitor ;
  59. $visitor->setCookie($cookie) ;
  60. $visitor->setIp($ip) ;
  61. $visitor->setTotalVisit(1) ;
  62. $visitor->setLastAccess(new \DateTime()) ;
  63. $this->em->persist($visitor);
  64. $this->em->flush() ;
  65. }
  66. public function updateVisitor($visitor)
  67. {
  68. $totalVisit = $visitor->getTotalVisit() + 1 ;
  69. $visitor->setTotalVisit($totalVisit) ;
  70. $visitor->setLastAccess(new \DateTime()) ;
  71. $this->em->persist($visitor);
  72. $this->em->flush() ;
  73. }
  74. public function setNewsletter($user, $subscribeNewsletter)
  75. {
  76. $currentMerchant = $this->merchantUtils->getMerchantCurrent() ;
  77. $newsletters = $currentMerchant->getNewsletters() ;
  78. if(isset($newsletters[0]) && $newsletters[0]) {
  79. if($subscribeNewsletter) {
  80. $user->addNewsletter($newsletters[0]) ;
  81. }
  82. else {
  83. $user->removeNewsletter($newsletters[0]) ;
  84. }
  85. }
  86. $this->em->persist($user) ;
  87. $this->em->flush() ;
  88. }
  89. }